版权声明:文章编写不易,转载请注明原链接 https://blog.csdn.net/u012184539/article/details/84833051
spring的bean创建和属性注入
1. 单例模式
使用factory-method属性设置实例化的工厂方法
<bean id="work" class="xyz.totok.Work" factory-method="getInstance"/>
2. 初始化和销毁bean
使用应用上下文配置
<bean id="user" class="xyz.totok.User" init-method="init" destroy-method="destroy"/>
3. 初始化和销毁bean
实现InitializingBean接口做初始化,实现DisposibleBean接口做销毁工作。
public class User implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
}
@Override
public void destroy() throws Exception {
}
}
4. 初始化和销毁bean
很多bean都使用了相同名字的初始化和销毁方法的时候,可以为应用上下文设置默认的初始化和销毁方法。在<beans>元素中设置。
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
default-init-method="init" default-destroy-method="destroy">
</beans>
5. 注入构造函数参数
<bean id="user" class="xyz.totok.User" scope="prototype">
<property name="name" value="小明"/>
<constructor-arg value="小明"/>
</bean>
6. 注入bean属性
简单的基本属性注入
<property name="name" value="小明"/>
对象属性注入
<property name="name" ref="bean_id"/>
7. bean的应用范围
使用scope属性配置bean的应用范围。
<bean id="user" class="xyz.totok.User" scope="prototype">
</bean>
8. autowire自动装配bean属性
在Spring中,支持 5 自动装配模式。
- no – 缺省情况下,通过“ref”或"value"属性手动设定
- byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
- byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
- constructor – 在构造函数参数的byType方式。
- autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。
<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />
9. @AutoWired注解装配bean属性
- 在beans.xml中使用注解处理器AutowiredAnnotationBeanPostProcessor
<context:annotation-config />
- 在beans.xml中使用注解处理器AutowiredAnnotationBeanPostProcessor
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
- 在 setter 方法,构造函数或字段中使用@Autowired 自动装配 bean
import org.springframework.beans.factory.annotation.Autowired;
public class Customer
{
@Autowired
private Person person;
private int type;
private String action;
//getter and setter methods
}
10. @Qualifier优化自动自动装配
在使用@AutoWired自动装配时,如果有两个bean都满足装配条件,将会报错。这时候使用@Qualifier可以指定某个bean被装配。
<bean id="customer" class="com.yiibai.common.Customer" />
<bean id="personA" class="com.yiibai.common.Person" >
<property name="name" value="yiibaiA" />
</bean>
<bean id="personB" class="com.yiibai.common.Person" >
<property name="name" value="yiibaiB" />
</bean>
public class Customer {
@Autowired
@Qualifier("personA")
private Person person;
//...
}
如上的情况,personA的bean会被装配成功。
11. 注入内部类的bean
在上下文中的bean都是共享的,而有的时候需要某些bean是类的内部使用的,不对外共享。可以注入内部bean,相当于内部类。
<bean id="customer" class="com.yiibai.common.Customer" >
<property name="person">
<bean class="xyz.totok.common.Person"/>
</property>
</bean>
也可以在构造函数中注入内部bean
<bean id="customer" class="com.yiibai.common.Customer" >
<constructor-arg>
<bean class="xyz.totok.common.Person"/>
</constructor-arg>
</bean>
12. 使用p命名空间装配属性
使用p命名空间和使用<property>是一样的效果
<bean id="user" class="xyz.totok.User" scope="prototype"
p:name="小明"
p:apple-ref="apple">
<constructor-arg value="小明"/>
</bean>
13. 装配集合属性
public class Work {
private List<String> works;
//...
}
list列表装配
<bean id="work" class="xyz.totok.Work">
<property name="works">
<list>
<value>搬砖</value>
<value>刷碗</value>
<value>送快递</value>
</list>
</property>
</bean>
set集合装配
<bean id="work" class="xyz.totok.Work">
<property name="works">
<set>
<value>搬砖</value>
<value>刷碗</value>
<value>送快递</value>
</>
</property>
</bean>
map映射装配
<bean id="work" class="xyz.totok.Work">
<property name="works">
<map>
<entry key="a" value="hello a"/>
<entry key="b" value="hello b"/>
</map>
</property>
</bean>
properties集合装配,property的key只能是字符串,而map的key可以是任意类型。
<bean id="work" class="xyz.totok.Work">
<property name="works">
<props>
<prop key="a">hello a</prop>
<prop key="b">hello b</prop>
</props>
</property>
</bean>
14. 装配null值
使用<null/>元素表示装配null值
<property name="person"><null/></property>