bean 的自动装配

  • 自动装配是 Spring 满足 bean 依赖的一种方式!
  • Spring 会在上下文中自动寻找,并自动给 bean 装配属性

在 Spring 中有三种装配的方式

  • xml 中显示的配置
  • 在 java 中显示配置
  • 隐式自动装配 bean [重要]

测试#

创建三个实体类 Cat,Dog,People

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;
}

原先我们配置,是在 xml 中显示的配置,即上面说的第一种方法

<?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="catt" class="com.hudu.pojo.Cat"/>
    <bean id="dogg" class="com.hudu.pojo.Dog"/>

    <bean id="people" class="com.hudu.pojo.People">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
        <property name="name" value="hudu"/>
    </bean>
</beans>

但是我们的 cat 和 dog 使用这两个实体类,怎么让 Spring 自动装配,,不需要我们手动装配呢,这里就说到了上面的第三种方法,我们可以使用 Autowire,有两种实现方式,一种是 byType,另一种是 byName,从名字就可以看出他们的区别。

    <!--
    byName:会在容器上下文中查找,和自己对象set方法后面的值对应的beanid!
    byType:会在容器上下文中查找,和自己对象属性类型相同的bean!
    -->
    <bean id="people" class="com.hudu.pojo.People" autowire="byName">
    <!--<bean id="people" class="com.hudu.pojo.People" autowire="byType">-->
        <property name="name" value="hudu"/>
    </bean>

但是两者都有弊端,对于 byName 如果 bean id 配置的名字和 set 方法后面的方法名不一致,无法自动装配。对于 byType,可以不用配置 bean id,但是如果配置多个对象属性的相同的 bean,也会无法自动装配。

小结:

  • byName 的时候,需要保证所有的 bean 的 id 唯一,并且这个 bean 需要和自动注入的属性的 set 方法的值一直
  • byType 的时候,需要保证所有的 bean 的 class 唯一,并且需要自动注入的熟属性的类型一致

使用注解实现自动装配#

上面已经说过一种实现自动装配的方式,下面是另一种,通过注解实现自动装配的方式。
jdk1.5 支持的注解,Srping 从 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 id="cat1" class="com.hudu.pojo.Cat"/>
    <bean id="cat111" class="com.hudu.pojo.Cat"/>
    <bean id="dog1" class="com.hudu.pojo.Dog"/>
    <bean id="dog222" class="com.hudu.pojo.Dog"/>
    <bean id="people" class="com.hudu.pojo.People"/>
    </beans>

实体类上的注解

public class People {
    @Autowired
    @Qualifier(value = "cat111")
    //上面两个效果等于@Resource(value = "cat111")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog222")
    private Dog dog;
    private String name;
}
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")

@Nullable   字段标记了这个注解,说明这个字段可以为null

和@Autowired类似的是Java原生注解@Resource,会先通过名字去查找,再通过类型去查找,可以指定名字@Resource(name = "cat2")

测试类

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout();
        people.getCat().shout();
  • @Autowired
    直接在属性上使用即可,也可以在 set 方式上使用
    使用 Autowired 我们可以不用编写 Set 方法了,前提是你这个自动装配的属性在 I0C (Spring) 容器中存在,且符合名字 byname!

小结: @Resource 和 @ Autowired 的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired 通过 byType 的方式实现,而且必须要求这个对象存在![常用]
  • @Resource 默认通过 byname 的方式实现,如果找不到名字,则通过 byType 实现!如果两个都找不到的情况下,就报错![常用]
  • 执行顺序不同: @Autowired 通过 byType 的方式实现。@ Resource 默认通过 byname 的方式实现。
本作品采用《CC 协议》,转载必须注明作者和本文链接
未填写
文章
247
粉丝
19
喜欢
219
收藏
63
排名:723
访问:9993
私信
所有博文
社区赞助商