公共属性的注入:将bean中公共的属性提取出来单独作为一个bean并设置该bean的abstract属性为true,然后再有公共属性的bean中使用parent属性继承设置了abstract属性的bean。
bean2类:
package com.jzq.spring;
public class Bean2 {
private Bean3 bean3;
private Bean4 bean4;
private Bean5 bean5;
public Bean3 getBean3() {
return bean3;
}
public void setBean3(Bean3 bean3) {
this.bean3 = bean3;
}
public Bean4 getBean4() {
return bean4;
}
public void setBean4(Bean4 bean4) {
this.bean4 = bean4;
}
public Bean5 getBean5() {
return bean5;
}
public void setBean5(Bean5 bean5) {
this.bean5 = bean5;
}
}
bean3类:
package com.jzq.spring;
public class Bean3 {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
bean4类:
package com.jzq.spring;
public class Bean4 {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
bean5类:
package com.jzq.spring;
public class Bean5 {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
配置方法一:
配置方法一:<bean id="bean2" class="com.jzq.spring.Bean2">
<property name="bean3" ref="bean3"/>
<property name="bean4">
<ref bean="bean4"/>
</property>
<property name="bean5" ref="bean5"/>
</bean>
<bean id="bean3" class="com.jzq.spring.Bean3">
<property name="id" value="100"/>
<property name="name">
<value>jack</value>
</property>
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.jzq.spring.Bean4">
<property name="id" value="1000"/>
<property name="name" value="jack"/>
</bean>
<bean id="bean5" class="com.jzq.spring.Bean5">
<property name="age" value="20"/>
</bean>
配置方法二:
提取bean中的公共属性
<!-- 提取bean中的公共部分 -->
<bean id="beanAbstract" abstract="true">
<property name="id" value="1000"/>
<property name="name" value="jack"/>
</bean>
<bean id="bean2" class="com.jzq.spring.Bean2">
<property name="bean3" ref="bean3"/>
<property name="bean4">
<ref bean="bean4"/>
</property>
<property name="bean5" ref="bean5"/>
</bean>
<bean id="bean3" class="com.jzq.spring.Bean3" parent="beanAbstract">
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.jzq.spring.Bean4" parent="beanAbstract"/>
在bean标签中有属性scope,如果其值为singleton那么每次返回相同的实例;如果其值为prototype,每次返回不同的实例。
配置方法三:按名称自动装配
开启自动装配default-autowire,然后会按照javabean中的属性名称到applicationContext配置文件中去查找与属性名称相同的id,然后注入到javabean中。
因为bean2里命名的属性为bean3,bean4,bean5,那么加载文件后会自动根据名称到配置文件中,加载注入相应的类。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName"
>
开启自动装配
<bean id="bean2" class="com.jzq.spring.Bean2"/>
<bean id="bean3" class="com.jzq.spring.Bean3" parent="beanAbstract">
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.jzq.spring.Bean4" parent="beanAbstract"/>
<bean id="bean5" class="com.jzq.spring.Bean5"/>
配置方法四:根据类型自动装配
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byType"
>
开启根据类型自动装配,那么就会根据数据的类型来装配对象。