7.Spring中Bean的自动装配

7.Spring中Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式!
  • Spring会在上下文中自动寻找并自动给bean装配属性!
  • 在Spring中有三种装配的方式
    • 在xml中显示的配置
    • 在java中显示配置
    • 隐式的自动装配bean【重要】

7.1 测试自动装配

  • 搭建环境

​ 第一步:编写实体类

public class Cat {

    public void bark(){
        System.out.println("猫叫~喵喵喵");
    }
}
public class Dog {
    
    public void bark(){
        System.out.println("狗叫~汪汪汪");
    }
}
public class People {

    private Cat cat;
    private Dog dog;
    private String name;
}

​ 第二步:编写spring主配置文件

<?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="cat" class="com.xuan.pojo.Cat"/>
    <bean id="dog" class="com.xuan.pojo.Dog"/>

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


</beans>

​ 第四步:编写测试类

public class TestPeople {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        People people = context.getBean("people", People.class);

        people.getCat().bark();
        people.getDog().bark();

    }
}

--------------------------------测试成功---------------------------------

7.2 ByName自动装配

ByName自动装配的原理

  • ByName会在上下文查找对应的bean的id是否与set注入的属性id一致

  • ByName是通过bean标签中autowire属性决定的 下面是xml配置

    实现方式简单讲就是下面第一个bean中id就是cat 实体类中set方法属性也是cat对应上了,所以spring为我们自动装配上了

<!--byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid!-->
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>

<bean id="people" class="com.xuan.pojo.People" autowire="byName">
    <property name="name" value="jieshen"/>
</bean>

7.3 ByType自动装配

ByType自动装配的原理:

  • byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean。

  • byType是通过bean标签中autowire属性决定的 下面是xml配置

    实现方式简单点讲就是下面第一个bean中Class属性直接指定了唯一的Cat类与实体类注入的类型一致且唯一基于此spring才能我们装配上

<!--byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!-->
<bean id="cat" class="com.xuan.pojo.Cat"/>
<bean id="dog" class="com.xuan.pojo.Dog"/>

<bean id="people" class="com.xuan.pojo.People" autowire="byType">
    <property name="name" value="jieshen"/>
</bean>

7.4 使用注解实现自动装配(这才是最常用的)

首先根据挂网我们要使用注解的话得先导入context上下文约束以及开启注解支持

​ 第一步:导入context上下文约束以及开启注解支持

<?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="cat" class="com.xuan.pojo.Cat"/>
    <bean id="dog" class="com.xuan.pojo.Dog"/>
    <bean id="people" class="com.xuan.pojo.People"/>

</beans>

​ 第二步:在实体类的属性上加上@Autowired注解

public class People {

    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

----------------------------------------测试成功------------------------------------

7.4.1 @Autowired注解
  • 跟进并查看该注解
package org.springframework.beans.factory.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

   /**
    * Declares whether the annotated dependency is required.
    * <p>Defaults to {@code true}.
    * 这个required属性默认是true 如果设置位false表明允许@Autowired标注的属性等为空
    */
   boolean required() default true;

}
7.4.2 @Qualifier注解
  • 跟进并查看该注解
package org.springframework.beans.factory.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//作用可以在字段上也就是传参上
//Qualifier的作用是在多个bean对象中指定要找的那一个
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {

   String value() default "";

}

​ 测试代码

public class People {

    @Autowired
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog2")//可以指定要找确切的哪一个id
    private Dog dog;
    private String name;
}
<bean id="dog" class="com.xuan.pojo.Dog"/>
<bean id="dog1" class="com.xuan.pojo.Dog"/>
<bean id="dog2" class="com.xuan.pojo.Dog"/>
  • 还可以@Qualifier在各个构造函数参数或方法参数上指定注释
  • 下面是官网的例子
public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(@Qualifier("main") MovieCatalog movieCatalog,
            CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...
}
<?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">
        <qualifier value="main"/> 

        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean class="example.SimpleMovieCatalog">
        <qualifier value="action"/> 

        <!-- inject any dependencies required by this bean -->
    </bean>

    <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>
  • The bean with the main qualifier value is wired with the constructor argument that is qualified with the same value.
  • 具有action限定符值的Bean与限定有相同值的构造函数参数连接。
7.4.3 @Autowired和@Resource区别

spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。

  • @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了

  • @Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。

  • 所以如果使用name属性,则使用byName的自动注入策略,使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

  • @Resource装配顺序

    1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
      2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
      3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
      4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

区别:

  • @Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
  • @Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使
  • @Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

推荐使用:@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。这样代码看起就比较优雅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值