• Bean的自动装配 | 使用注解实现


    自动装配说明:

    • 自动装配是使用spring满足bean依赖的一种方法
    • spring会在上下文中为某个bean寻找其依赖的bean

    Spring中bean有三种装配机制,分别是:

    1. 在xml中显示配置;
    2. 在Java中显示配置;
    3. 隐式的bean发现机制和自动装配

    Spring的自动装配需要从两个角度来实现,或者说是两个操作:

    1. 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;

    2. 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;

    测试环境搭建

    1、新建一个项目

    2、新建两个实体类,Cat   Dog  都有一个叫的方法

    public class Cat {
       public void shout() {
           System.out.println("miao~");
      }
    }
    public class Dog {
       public void shout() {
           System.out.println("wang~");
      }
    }

    3、新建一个用户类 User

    public class User {
       private Cat cat;
       private Dog dog;
       private String str;
    }

    4、编写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="dog" class="com.w.pojo.Dog"/>
       <bean id="cat" class="com.w.pojo.Cat"/>
    
       <bean id="user" class="com.w.pojo.User">
           <property name="cat" ref="cat"/>
           <property name="dog" ref="dog"/>
           <property name="str" value="皇上"/>
       </bean>
    </beans>

    5、测试

    public class MyTest {
       @Test
       public void testMethodAutowire() {
           ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
           User user = (User) context.getBean("user");
           user.getCat().shout();
           user.getDog().shout();
      }
    }

    autowire byName (按名称自动装配)

    <bean id="user" class="com.w.pojo.User" autowire="byName">
       <property name="str" value="皇上"/>
    </bean>

    autowire byType (按类型自动装配)

    <bean id="dog" class="com.w.pojo.Dog"/>
    <bean id="cat" class="com.w.pojo.Cat"/>
    <bean id="cat2" class="com.w.pojo.Cat"/>
    
    <bean id="user" class="com.w.pojo.User" autowire="byType">
       <property name="str" value="皇上"/>
    </bean>

    使用注解:

    jdk1.5开始支持注解,spring2.5开始全面支持注解。

    准备工作:利用注解的方式注入属性。

    1、在spring配置文件中引入context文件头

    xmlns:context="http://www.springframework.org/schema/context"
    
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd

    2、开启属性注解支持!

    <context:annotation-config/>

    jdk1.5开始支持注解,spring2.5开始全面支持注解。

    准备工作:利用注解的方式注入属性。

    1、在spring配置文件中引入context文件头

    xmlns:context="http://www.springframework.org/schema/context"
    
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd

    2、开启属性注解支持!

    <context:annotation-config/>

    @Autowired

    • @Autowired是按类型自动转配的,不支持id匹配。

    测试:

    1、将User类中的set方法去掉,使用@Autowired注解

    public class User {
       @Autowired
       private Cat cat;
       @Autowired
       private Dog dog;
       private String str;
    
       public Cat getCat() {
           return cat;
      }
       public Dog getDog() {
           return dog;
      }
       public String getStr() {
           return str;
      }
    }

    2、此时配置文件内容

    <context:annotation-config/>
    
    <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="cat" class="com.kuang.pojo.Cat"/>
    <bean id="user" class="com.kuang.pojo.User"/>

    @Autowired(required=false)  说明:false,对象可以为null;true,对象必须存对象,不能为null。

    @Qualifier

    • @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配

    • @Qualifier不能单独使用。

    测试实验步骤:

    1、配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!

    <bean id="dog1" class="com.kuang.pojo.Dog"/>
    <bean id="dog2" class="com.kuang.pojo.Dog"/>
    <bean id="cat1" class="com.kuang.pojo.Cat"/>
    <bean id="cat2" class="com.kuang.pojo.Cat"/>

    2、没有加Qualifier测试,直接报错

    3、在属性上添加Qualifier注解

    @Autowired
    @Qualifier(value = "cat2")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog2")
    private Dog dog;

    测试,成功输出!

    @Resource

    • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;

    • 其次再进行默认的byName方式进行装配;

    • 如果以上都不成功,则按byType的方式自动装配。

    • 都不成功,则报异常。

    实体类:

    public class User {
       //如果允许对象为null,设置required = false,默认为true
       @Resource(name = "cat2")
       private Cat cat;
       @Resource
       private Dog dog;
       private String str;
    }

    beans.xml

    <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="cat1" class="com.kuang.pojo.Cat"/>
    <bean id="cat2" class="com.kuang.pojo.Cat"/>
    
    <bean id="user" class="com.kuang.pojo.User"/>

    测试:结果OK

    配置文件2:beans.xml , 删掉cat2

    <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="cat1" class="com.kuang.pojo.Cat"/>

    实体类上只保留注解

    @Resource
    private Cat cat;
    @Resource
    private Dog dog;

    结果:OK

    结论:先进行byName查找,失败;再进行byType查找,成功。

     

  • 相关阅读:
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_10-课程管理-教学方法说明
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_09-课程管理-需求分析
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_08-页面发布-业务完善的思考
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_07-页面发布-前后端发布页面测试
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_06-页面发布-生产方cms-发送页面发布消息测试
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_05-页面发布-生产方cms-发送页面发布消息
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_04-页面发布-消费方Cms Client-消费方法-监听队列
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_03-页面发布-消费方Cms Client-消费方法-service
    阶段5 3.微服务项目【学成在线】_day06 页面发布 课程管理_02-页面发布-消费方Cms Client-搭建工程
    2019最新 Java商城秒杀系统的设计与实战视频教程(SpringBoot版)_2-2微服务项目的搭建-SpringBoot搭建多模块项目二
  • 原文地址:https://www.cnblogs.com/IanIan/p/14333766.html
Copyright © 2020-2023  润新知