C/C++教程

自动装配(@Autowired和@Resource的区别)

本文主要是介绍自动装配(@Autowired和@Resource的区别),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

程序举例(一个人有两个宠物)

  • Cat类
    package com.jing.pojo;
    
    public class Cat {
    
        public void shout(){
            System.out.println("喵");
        }
    }
    
  • Dog类
    package com.jing.pojo;
    
    public class Dog {
    
        public void shout(){
            System.out.println("汪");
        }
    }
    
  • Person类
    public class Person {
    
        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() {
            return "Person{" +
                    "cat=" + cat +
                    ", dog=" + dog +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    

装配

所谓“装配”,是为bean注入依赖引用型属性)。Spring中有三种装配(依赖注入)方式

  • 手动装配:XML配置文件中显式配置bean的属性(依赖注入的三种方式)
    <?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.jing.pojo.Cat"/>
        <bean id="dog" class="com.jing.pojo.Dog"/>
        <bean id="person" class="com.jing.pojo.Person">
            <property name="name" value="YuJing"/>
            <!--依赖注入-->
            <property name="cat" ref="cat"/>
            <property name="dog" ref="dog"/>
        </bean>
        
    </beans>

  • 自动装配:XML配置文件注解是都可以实现自动装配。
    • XML配置文件实现自动装配:
      • autowire="byType":先根据属性的数据类型匹配,再根据属性名称匹配。
        <?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.jing.pojo.Dog"></bean>
            <bean id="cat" class="com.jing.pojo.Cat"></bean>
            <!--byType:会自动在容器上下文查找,和自己对象属性类型相同的bean!-->
            <bean id="person" class="com.jing.pojo.Person" autowire="byType"></bean>
        </beans>
      • autowire="byName":先根据属性名称匹配,再根据属性的数据类型匹配。
        <?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.jing.pojo.Dog"></bean>
            <bean id="cat" class="com.jing.pojo.Cat"></bean>
            <!--byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid!-->
            <bean id="person" class="com.jing.pojo.Person" autowire="byName"></bean>
        </beans>
    • 注解实现自动装配:在类的成员属性的定义上方加上注解,自动装配是通过反射直接获取属性(field)实现的,所以不需要setter方法
      • @Autowired:Spring提供的注解,先根据类型,再根据名称。
        • 先根据bean所对应的类中的属性的数据类型进行bean(被依赖的bean)匹配:
          • 如果IOC容器中没有类型匹配的bean,则匹配失败;
          • 如果有,而且相同类型的bean只有一个,则直接匹配成功;
          • 如果有,但是相同类型的bean不止一个,则需要进一步根据属性名称进行匹配:
            • 如果可以根据名称唯一确定一个对应的bean(类型匹配,名称匹配),则匹配成功;(属性名称为cat,IOC容器中的bean:cat 或者 cat111 或者 cat和cat111都有,上述三种情况都可以匹配成功。)
            • 否则失败。(属性名称为cat,IOC容器中的bean:cat111和cat222,这种情况将失败)
      • @ResourceJDK提供的注解,先根据名称,再根据类型。最终只要能够确定一个唯一的bean,则可以成功装配;如果产生歧义,将无法装配
这篇关于自动装配(@Autowired和@Resource的区别)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!