自动装配说明
- 自动装配是使用spring满足bean依赖的一种方法
- spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:
- 在xml中显式配置;
- 在java中显式配置;
- 隐式的bean发现机制和自动装配。
自动装配:
after
<bean id="person" class="com.chen.pojo.Person">
<property name="name" value="杰哥"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog" />
</bean>
before
byName会自动在容器上下文中查找,和自己对象set方法后面的值相应的beanid
<bean id="dog" class="com.chen.pojo.Dog"/>
<bean id="cat" class="com.chen.pojo.Cat"/>
<--byName会自动在容器上下文中查找,和自己对象set方法后面的值相应的beanid-->
<bean id="person" class="com.chen.pojo.Person" autowire="byName">
<property name="name" value="杰哥"/>
</bean>
byType会自动在容器上下文中查找,和自己对象属性类型相同的beanid
<bean id="dog" class="com.chen.pojo.Dog"/>
<bean id="cat" class="com.chen.pojo.Cat"/>
<bean id="person" class="com.chen.pojo.Person" autowire="byType">
<property name="name" value="杰哥"/>
</bean>
小结:
byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
注解实现自动装配
jdk1.5开始支持注解,spring2.5开始全面支持注解。
准备工作:利用注解的方式注入属性。
1、在spring配置文件中引入context文件头
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
2、开启属性注解支持!
context:annotation-config/>
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解支持-->
<context:annotation-config/>
<bean id="dog" class="com.chen.pojo.Dog"/>
<bean id="cat" class="com.chen.pojo.Cat"/>
<bean id="person" class="com.chen.pojo.Person"/>
</beans>
注解:@Autowired
使用注解可以省略set方法
public class Person {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
}
@Nullable允许为null
import org.springframework.lang.Nullable;
public void setName(@Nullable String name) {
this.name = name;
}
@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。
//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;
@Qualifier
- @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
- @Qualifier不能单独使用。
@Resource
- @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
- 其次再进行默认的byName方式进行装配;
- 如果以上都不成功,则按byType的方式自动装配。
- 都不成功,则报异常。
小结
@Autowired与@Resource异同:
1、@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
2、@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
3、@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。