7. bean的自动装配
- 自动转配是spring 满足bean依赖一种方式!
- spring会在上下文自动寻找,并自动给bean装配属性!
在Spring中有三种装配方式:
- 在xml中显式配置
- 在java中显式配置
- 隐式的自动装配bean
7.1 测试
-
环境搭建
-
一个人有两个宠物
public class Cat { public void shout() { System.out.println("miao...."); } }
public class Dog { public void shout() { System.out.println("wang..."); } }
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() { final StringBuilder sb = new StringBuilder("People{"); sb.append("cat=").append(cat); sb.append(", dog=").append(dog); sb.append(", name='").append(name).append('\''); sb.append('}'); return sb.toString(); } }
-
beans.xml
<bean id="cat" class="com.javacto.pojo.Cat"/> <bean id="dog" class="com.javacto.pojo.Dog"/> <bean id="people" class="com.javacto.pojo.People"> <property name="name" value="eric"/> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> </bean>
-
测试:
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); people.getDog().shout(); people.getCat().shout(); }
-
7.2 Autowire 自动装配
-
ByName 自动装配
<bean id="cat" class="com.javacto.pojo.Cat"/> <bean id="dog" class="com.javacto.pojo.Dog"/> <!-- ByName: 会自动在上下文容器中寻找,和自己对象set方法后面的值对应的 bean id --> <bean id="people" class="com.javacto.pojo.People" autowire="byName"> <property name="name" value="eric"/> </bean>
-
ByType 自动装配
<bean id="cat" class="com.javacto.pojo.Cat"/> <bean id="dog" class="com.javacto.pojo.Dog"/> <!-- ByName: 会自动在上下文容器中寻找,和自己对象set方法后面的值对应的 bean id --> <!-- ByType: 会自动在上下文容器中寻找,和自己对象属性类型相同的bean --> <bean id="people" class="com.javacto.pojo.People" autowire="byType"> <property name="name" value="eric"/> </bean>
-
小结:
- ByName : 需要保证 bean 的 id 唯一 !
- ByType: 需要保证 bean 类型唯一 !
7.3 使用注解实现bean的自动装配
-
jdk 1.5 支持的注解, spring2.5 就支持注解了 !
-
要使用注解须知:
-
导入约束:
<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"> </beans>
-
配置注解支持
<!-- 配置注解支持 --> <context:annotation-config />
-
-
@Autowired
直接使用在属性上即可, 也可以在set方式上使用
这个注解就相当于bean标签 属性里的 autowire
科普:
@Nullable 在属性上使用了这个注解,就是说这个属可以为null
@Autowired(requied = false) false就是与@Nullable作用一样
如果Autowired 自动装配的环境较为复杂,没有与属性名对应的id,类型也不唯一,
这是后我们可以是用@Qualifier (value=“bean 的id”) 配合使用,指定唯一的对象注入
@Resource : 作用与 @Autowired 一样,但它不是spring注解,属于java 提供的注解,需要注意,JDK8后的版本需要添加依赖才能使用。