- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配方式
1.在xml中显式配置
2.在java中现实配置
3.隐式的自动装配bean(重要)
1 测试
环境搭建:一个人有两个宠物
package com.kwok.pojo;
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
package com.kwok.pojo;
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
package com.kwok.pojo;
public class People {
private Cat cat;
private Dog dog;
private String 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;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.kwok.pojo.Cat"/>
<bean id="dog" class="com.kwok.pojo.Dog"/>
<bean id="people" class="com.kwok.pojo.People">
<property name="name" value="王翠花"/>
<property name="dog" ref="dog"/>
<property name="cat" ref="cat"/>
</bean>
</beans>
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout();
people.getCat().shout();
}
2 自动装配
**byName: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
***byType: 会自动在容器上下文中查找,和自己对象属性类型相同的bean*
小结:
- byName,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。
- byType,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
3 使用注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了
使用注解须知:
1.导入约束:context约束
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
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/>
</beans>
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"
>
<!--开启注解支持-->
<context:annotation-config/>
<bean id="cat" class="com.kwok.pojo.Cat"/>
<bean id="dog" class="com.kwok.pojo.Dog"/>
<bean id="people" class="com.kwok.pojo.People"/>
</beans>
使用Autowired我们可以不用编写Set方法,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName!
科普:
@Nullable 字段标记这个注解,说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
测试代码:
@Qualifier(value=…)
既无法通过byType自动装配也无法通过byName自动装配,可以通过Qualifier直接指定。
public class Person {
private String name;
@Resource(name = "cat895")
private Cat cat;
@Autowired
@Qualifier(value = "dog222")
private Dog dog;
}
小结:
@Resourse和@Autowired区别:
-
都是用来自动装配的,都可以放在属性字段上
-
@Autowired通过byType实现,找不到再通过byName
-
@Resoutce默认通过byName方式实现,如果找不到名字,则通过byType实现
-
执行顺序不同:@Autowired