Spring注入方式(2)
3、引用其他bean
Bean经常需要相互协作完成应用程序的功能,bean之间必须能够互相访问,就必须在bean配置之间指定对bean的引用,可以通过节点<ref>或者ref来为bean属性指定对bean的引用,也可以在属性或者构造器里包含bean的声明,这样bean称为内部bean。
bean中引用其他bean,其中Car为对象。
<!-- 通过构造方法配置bean属性 -->
<bean id="car" class="hello.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double" ></constructor-arg> </bean> <bean id="person" class="hello.Person">
<property name="name" value = "Tom"></property>
<property name="age" value = "24"></property>
<property name="car" ref="car"></property>
</bean>
内部bean
<bean id="person" class="hello.Person">
<property name="name" value = "Tom"></property>
<property name="age" value = "24"></property>
<!--
<property name="car" ref="car"></property>--> <!-- 内部bean -->
<property name="car">
<!-- 内部bean不能被外部bean使用 -->
<bean id="car3" class="hello.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg> </bean>
</property>
</bean>
4、集合属性
在Spring中可以通过<list>、<set>或者<map>来配置集合属性。
通过list配置集合属性
Person.java
package com.spring; import java.util.List; public class Person {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
} }
Car.java
package com.spring; public class Car {
private String brand;
private double price;
private int maxSpeed;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
} }
beans-collection.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="car1" class="com.spring.Car">
<property name="brand" value="audi"></property>
<property name="price" value="400000"></property>
<property name="maxSpeed" value="240"></property>
</bean> <bean id="car2" class="com.spring.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="700000"></property>
<property name="maxSpeed" value="270"></property>
</bean> <bean id="person" class="com.spring.Person">
<property name="name" value="Jerry"></property>
<property name="age" value="41"></property>
<property name="cars">
<list>
<ref bean="car1" />
<ref bean="car2" />
</list>
</property>
</bean>
</beans>
结果:
通过map配置属性
Person.java
package com.spring; import java.util.Map; public class Person {
private String name;
private int age;
private Map<String, Car> carMap;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map<String, Car> getCarMap() {
return carMap;
}
public void setCarMap(Map<String, Car> carMap) {
this.carMap = carMap;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", carMap=" + carMap + "]";
}
}
beans-collection
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="car1" class="com.spring.Car">
<property name="brand" value="audi"></property>
<property name="price" value="400000"></property>
<property name="maxSpeed" value="240"></property>
</bean> <bean id="car2" class="com.spring.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="700000"></property>
<property name="maxSpeed" value="270"></property>
</bean> <bean id="person" class="com.spring.Person">
<property name="name" value="Jerry"></property>
<property name="age" value="41"></property>
<property name="carMap">
<map>
<entry key="1" value-ref="car1"></entry>
<entry key="2" value-ref="car2"></entry>
</map>
</property>
</bean>
</beans>
结果:
配置Set属性,和配置list一样。
5、p名字空间注入,也需要setter方法。
Student.java
package com.spring; public class Student {
private String name;
private String number; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
} @Override
public String toString() {
return "Student [name=" + name + ", number=" + number + "]";
} }
Main.java
package com.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans-p.xml");
Student student = (Student) ac.getBean("student");
System.out.println(student);
}
}
beans-p.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.spring.Student" p:name="Merry" p:number="1120141314"></bean> </beans>
执行结果
6、property注入
Student.java
package com.spring; import java.util.Properties; public class Student {
private String name;
private String number;
private Properties properties; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + ", properties=" + properties + "]";
} }
beans-p.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="student" class="com.spring.Student">
<property name="name" value="Merry"></property>
<property name="number" value="1120141213"></property>
<property name="properties" >
<props>
<prop key="user">root</prop>
<prop key="psw">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property> </bean>
</beans>
执行结果
7、c命名空间注入
Spring注入方式(2)的更多相关文章
- Spring注入方式(1)
Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ...
- Spring注入方式及用到的注解
注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ...
- Spring课程 Spring入门篇 2-2 Spring注入方式
课程链接: 本节主要讲了以下两块内容: 1 xml两种注入方式 2 注入方式代码实现 3 特别注意 1 xml两种注入方式 构造注入和set注入 2 注入方式代码实现 2.1 set注入方式的实现 实 ...
- 【原创】Spring 注入方式
Spring 强烈推荐注解在构造器上,且对于不能为null的字段或者属性都用断言. 1. 设值注入 原理:通过setter方法注入 XML配置方式:bean下的property标签,用value指定基 ...
- Spring注入方式及注解配置
一:基于xml的DI(Dependency Injection) 注入类型: 定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car Student ...
- Spring源码学习之:你不知道的spring注入方式
前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...
- 一个接口多个实现类的Spring注入方式
1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...
- Spring注入方式
- Spring学习三----------注入方式
© 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...
随机推荐
- 解决virtualbox共享文件夹没有访问权限的问题
Virtualbox是一款免费试用的虚拟机软件.基本功能完全可替代需要购买或crack的VMware. 在Windows主机上用Virtualbox搭建Linux虚拟机,虚拟机和主机之间传递文件最方便 ...
- loadrunner--步长(Pacing)的设置及作用
Pacing时间的设置需要根据使用您系统的用户的行为来决定. 如果您那边的用户在您的系统上做完一套操作后不会做下一套,则可能不需使用Pacing. 如果您那边用户在系统上需要不断地做同样的操作,比如他 ...
- Oracle 错误集锦
1.plsql进行更新操作时卡死的解决办法 https://blog.csdn.net/laoyingat/article/details/79379270
- Oracle学习笔记(十二)
十三.存储过程和存储函数1.掌握存储过程(相当于建立一个函数或者方法体,然后通过外部对其调用) 指存储在数据库中供所有程序调用的子程序叫做存储过程或存储函数. 相同点: 完成特定功能的程序 区别: 是 ...
- Oracle学习笔记(五)
七.查询 1.基本查询语句 select 列名字,列名字 from 表名字 例如 select user_a_id from userinfo; 2.在SQL*PLUS中设置格式 (1)设置新的字段名 ...
- UVa 11925 Generating Permutations (构造法)
题意:给定一个序列,让你从一个升序列变成该序列,并且只有两种操作,操作1:交换前两个元素,操作2:把第一个元素移动到最后. 析:一开始的时候吧,不会,还是看的题解,首先是要逆序来做,这样可能好做一点, ...
- 从《数据挖掘概念与技术》到《Web数据挖掘》
从<数据挖掘概念与技术>到<Web数据挖掘> 认真读过<数据挖掘概念与技术>的第一章后,对数据挖掘有了更加深刻的了解.数据挖掘是知识发展过程的一个步骤.知识发展的过 ...
- SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器 /** * cookie对象; * rememberMeCookie()方法是设置Cookie的生成模版,比如cookie的name,cooki ...
- 使用memset初始化C++自定义类型
当类型本身或者类型的成员变量带有虚函数以及像std::vector这类复杂数据结构的时候.就会出错,原因是memset把类型本身所带的一些隐含的信息也给置0了.如:虚表指针.std::vector的内 ...
- SQL Server关于存储过程的一点简单使用心得
===========================创建无参无返回值的存储过程===========================create proc pro_nameas--要执行的sql语句 ...