自动装配是spring满足bean依赖的一种方式
spring 会在上下文自动寻找,并自动给bean装配属性
在spring中有三种装配方式:
- 在xml中显示的配置
- 在java中显示的配置
- 隐式的自动装配bean【重要】
例子: 环境搭建一个人有两个宠物
public class Cat {
public void shout(){
System.out.println("苗");
}
}
public class Dog {
public void shout(){
System.out.println("你的狗叫什么?");
}
}
public class People {
private Cat cat;
private Dog dog;
private String name;
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
People people = context.getBean("people",People.class);
people.getDog().shout();
people.getCat().shout();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.xh.Dog">
</bean>
<bean id="cat" class="com.xh.Cat">
</bean>
<bean id="people" class="com.xh.People">
<property name="name" value="胡小黑"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
ByName自动装配
<bean id="people" class="com.xh.People" autowire="byName">
<property name="name" value="是小胡呀"/>
</bean>
<bean id="dog" class="com.xh.Dog">
</bean>
<bean id="cat" class="com.xh.Cat">
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void setName(String name) {
this.name = name;
}
ByType
<bean id="people" class="com.xh.People" autowire="byType">
<property name="name" value="是小胡呀"/>
</bean>
<bean id="dog" class="com.xh.Dog">
</bean>
<bean id="11cat" class="com.xh.Cat">
小结:
byName的时候需要保证bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。
byType的时候需要保证bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
注解实现自动配置
JDk1.5以后支持注解,spring 2.5支持注解‘
使用注解需要注意的是:
- 导入约束context约束
- 配置注解的支持 < 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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="example.SimpleMovieCatalog" primary="true">
</bean>
<bean class="example.SimpleMovieCatalog">
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
@Autowired
<?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"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:annotation-config/>
<bean id="dog" class="com.xh.Dog">
</bean>
<bean id="cat" class="com.xh.Cat">
</bean>
<bean id="people" class="com.xh.People"></bean>
</beans>
import org.springframework.beans.factory.annotation.Autowired;
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
直接在属性上使用,也可以在set方式上使用
使用Autowired我们连set方法都不需要使用了,前提条件是你这个自动装配的属性在IOC(Spring)容器中存在,且符合byName;
注意:
@Nullable 字段标记了这个注解,说明字段可以为null
public @interface Autowired {
boolean required() default true;
}
@Autowired(required = false) //如果显示的定义了Autowired的required为false标志这个值可以为空否则不允许为空
如果@Autowired自动装配环境比较复杂的时候自动装配无法通过一个注解@Autowired完成的时候,使用@Qualifier(value=“***”)去配置@Autowired的使用,指定一个唯一的bean对象注入
java自带@Resource
@Resource注解,首先去spring容器中查找id名字,找不到查找类型,在找不到报错。唯一,不能查找重复的.
@Autowired
@Qualifier(value = "dog")
private Dog dog;
@Resource
@Resource(name="cat")
private Cat cat;
@Resource
private Dog dog;
@Resource 和 @Autowired区别
- 都是用来自动装配的,都可以放到属性字段上
- @Autowired 通过byname的方式实现,而且必须要求这个对象存在,否则报错
- @Resource首先去spring容器中查找id名字,找不到查找类型(ByType),在找不到报错。唯一,不能查找重复的.
- 执行顺序不同 @Autowired通过ByType的方式实现,@Resource默认通过byName的方式实现
|