一、set方法注入
value :值类型的注入
ref :对象类型的注入
pojo
public class Person {
private String name;
private Integer age;
private Car car;
public Person() {
super();
System.out.println("构造方法调用");
}
public void init() {
System.out.println("Person被初始化了。。。");
}
public void destroy() {
System.out.println("Person被摧毁了。。。");
}
bean配置
<bean name="car" class="com.Spring.pojo.Car"> <property name="name" value="BWM"></property> <property name="color" value="red"></property> </bean> <bean name= "person1" class="com.Spring.pojo.Person"> <property name="name" value="helen"></property> <property name="age" value="18"></property> <property name="car" ref="car"></property> </bean>
二、构造方法注入
1.创建Person的有参构造方法
public Person(String name, Car car) { super(); System.out.println("调用了构造方法Person(String name, Car car)"); this.name = name; this.car = car; }
配置bean
<bean name="person2" class="com.Spring.pojo.Person"> <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="car" ref="car" index="0"></constructor-arg> </bean>
测试
@Test @SuppressWarnings("resource") public void testConstructer() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml"); Person person2=(Person) context.getBean("person2"); System.out.println(person2); }
2.使用index指定参数的位置
pojo中有如下两个构造方法
public Person(String name, Car car) { super(); System.out.println("调用了构造方法Person(String name, Car car)"); this.name = name; this.car = car; } public Person(Car car,Integer name) { super(); System.out.println("调用了构造方法Person( Car car, int name)"); this.name = name+""; this.car = car; }
配置bean
<bean name="person2" class="com.Spring.pojo.Person"> <constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="car" ref="car" index="0"></constructor-arg> </bean>
3.使用type指定参数的类型
public Person(String name, Car car) { super(); System.out.println("调用了构造方法Person(String name, Car car)"); this.name = name; this.car = car; } public Person(Car car,Integer name) { super(); System.out.println("调用了构造方法Person( Car car, int name)"); this.name = name+""; this.car = car; }
bean配置
<bean name="person2" class="com.Spring.pojo.Person">
<constructor-arg name="name" value="988" index="1" type="java.lang.Integer"></constructor-arg>
<constructor-arg name="car" ref="car" index="0"></constructor-arg>
</bean>
三、p名称空间注入
导入名称空间
xmlns:p="http://www.springframework.org/schema/p"
使用p:属性注入:
p:属性名 表示值类型
p:属性名-ref 表示应用类型
<bean name="person3" class="com.Spring.pojo.Person" p:name="helen" p:age="18" p:car-ref="car"> </bean>
典型案例:配置数据源
四、spl注入
<bean name="person4" class="com.Spring.pojo.Car"> <property name="name" value="#{person1.name}"></property> <property name="age" value="#{person1.age}"></property> <property name="car" ref="car"></property> </bean>
测试
@Test @SuppressWarnings("resource") public void testSpel() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml"); Person person4=(Person) context.getBean("person4"); System.out.println(person4); }