• 【Spring】学习笔记005Bean的自动装配


      在Spring中有三种装配bean的方式:

      1.在xml中显示的配置

      这是我们之前一直在使用的方法

    <bean id="helloBean" class="com.aircl.domain.Hello">
       <property name="str" value="Hello Spring"></property>
    </bean>

      2.在java中显示配置

      后续研究

      3.隐式的自动装配bean

      本次讨论


      自动装配是Spring满足bean依赖一种方式,Spring会在上下文中自动寻找,并自动给bean装配属性,自动装配同样拥有两种方式,xml配置和注解配置,如下:

    1.xml配置自动装配

      实体类如下

    public class Dog implements Serializable {
        public void shout(){
            System.out.println("汪汪");
        }
    }
    public class Cat implements Serializable {
        public void shout(){
            System.out.println("喵喵");
        }
    }
    public class People implements Serializable {
        private String name;
        private Dog dog;
        private Cat cat;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public Cat getCat() {
            return cat;
        }
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", dog=" + dog +
                    ", cat=" + cat +
                    '}';
        }
    }

      xml配置分两种:byName和byType

      1.1 byName

      使用byName方式,拿下面的举例,Dog和Cat的bean是要注入到People中的,并且dog和cat是people的属性,注意dog和cat的bean id值要与people中对应的set方法方法名一致,比如dog bean的id为dogId,那么people中的set方法就必须是setDogId才能够成功

        <bean id="dog" class="com.aircl.domain.Dog"></bean>
    
        <bean id="cat" class="com.aircl.domain.Cat"></bean>
    
        <bean id="people" class="com.aircl.domain.People" autowire="byName">
            <property name="name" value="aircl"></property>
        </bean>

      1.2 byType

      使用byType方式时,dog和cat的bean id可以不设置,Spring会根据People中的属性类型自动进行寻找,但是这种方式存在局限性,即当我们拥有两个同样类型的bean时,就无法成功使用

        <bean id="dog" class="com.aircl.domain.Dog"></bean>
    
        <bean class="com.aircl.domain.Cat"></bean>
    
        <bean id="people" class="com.aircl.domain.People" autowire="byType">
            <property name="name" value="aircl"></property>
        </bean>

    2.注解配置自动装配

      注解配置同样有两种方式:

      1.@Autowired与@Qualifier

      2.@Resource

      2.1 @Autowired与@Qualifier

      使用注解进行配置时我们需要提前进行一些注解依赖的导入,并开启注解配置<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
            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="dog" class="com.aircl.domain.Dog"/>
        <bean id="cat" class="com.aircl.domain.Cat"/>
        <bean id="people" class="com.aircl.domain.People">
            <property name="name" value="aircl"/>
        </bean>
    
    </beans>

      我们先不使用@Qualifier,只使用@Autowired,Dog和Cat实体类如上未变,People如下

    public class People implements Serializable {
    
        private String name;
        @Autowired
        private Dog dog;
        @Autowired
        private Cat cat;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public Cat getCat() {
            return cat;
        }
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", dog=" + dog +
                    ", cat=" + cat +
                    '}';
        }
    }

      如果我们拥有两个dog bean,即可搭配@Qualifier进行装配,@Qualifier的value为需要使用的bean id

    public class People implements Serializable {
    
        private String name;
        @Autowired
        @Qualifier(value = "dog")
        private Dog dog;
        @Autowired
        private Cat cat;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public Cat getCat() {
            return cat;
        }
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", dog=" + dog +
                    ", cat=" + cat +
                    '}';
        }
    }

      2.2 @Resource

      @Resouorce是Java带有的注解配置,我们在使用时不需要在xml中提前配置,测试时Dog和Cat实体类如上不变,People如下

    public class People implements Serializable {
    
        private String name;
        @Resource(name = "dog")
        private Dog dog;
        @Resource
        private Cat cat;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public Cat getCat() {
            return cat;
        }
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", dog=" + dog +
                    ", cat=" + cat +
                    '}';
        }
    }

      注解形式总结:

      @Resource和@ Autowired的区别:

    • 都是用来自动装配的,都可以放在属性字段上
    • @ Autowired通过byType的方式实现,而且必须要求这个对象存在
    • @ Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错
    • 执行顺序不同:@ Autowired通过byType的方式实现,@ Resource默认通过byname的方式实现
     
  • 相关阅读:
    Linux_Rsync远程同步备份服务器
    Linux_Rsync远程同步备份服务器
    Linux_VMWare12 Install RHEL7
    Linux_VMWare12 Install RHEL7
    Linux_SquidProxyServer代理服务器
    Linux_SquidProxyServer代理服务器
    Linux_LAMP 最强大的动态网站解决方案
    Ozone Native ACL的应用
    Ozone Audit Log解析工具的应用
    Ozone数据探查服务Recon2.0设计
  • 原文地址:https://www.cnblogs.com/AirCL/p/14372563.html
Copyright © 2020-2023  润新知