Automatic assembly (@ Autowired vs. @ Resource)

Program example (one person has two pets)

  • Cat class
    package com.jing.pojo;
    
    public class Cat {
    
        public void shout(){
            System.out.println("Meow");
        }
    }
    
  • Dog class
    package com.jing.pojo;
    
    public class Dog {
    
        public void shout(){
            System.out.println("Woof");
        }
    }
    
  • Person class
    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 + '\'' +
                    '}';
        }
    }
    

assembling

The so-called "assembly" is to inject dependencies (reference properties) into bean s. There are three assembly (dependency injection) methods in Spring:

  • Manual assembly: explicitly configure bean properties in XML configuration file (three methods of dependency injection)
    <?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"/>
            <!--Dependency injection-->
            <property name="cat" ref="cat"/>
            <property name="dog" ref="dog"/>
        </bean>
        
    </beans>

  • Automatic assembly: both XML configuration files and annotations can realize automatic assembly.
    • XML configuration file for automatic assembly:
      • autowire="byType": first match according to the data type of the attribute, and then match according to the attribute name.
        <?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: It will automatically find the object with the same attribute type as its own object in the container context bean!-->
            <bean id="person" class="com.jing.pojo.Person" autowire="byType"></bean>
        </beans>
      • autowire="byName": first match according to the attribute name, and then match according to the attribute data type.
        <?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:It will automatically find its own objects in the container context set Method beanid!-->
            <bean id="person" class="com.jing.pojo.Person" autowire="byName"></bean>
        </beans>
    • Annotation realizes automatic assembly: add annotation above the definition of the member attribute of the class. Automatic assembly is realized by directly obtaining the attribute (field) through reflection, so setter method is not required.
      • @Autowired: the annotations provided by Spring are based on the type first and then the name.
        • First, match beans (dependent beans) according to the data type of the attribute in the class corresponding to the bean:
          • If there is no bean with type matching in the IOC container, the matching fails;
          • If yes, and there is only one bean of the same type, the direct matching is successful;
          • If yes, but there is more than one bean of the same type, you need to further match according to the attribute name:
            • If a corresponding bean can be uniquely determined according to the name (type matching, name matching), the matching is successful; (the attribute name is cat, and the beans in the IOC container: cat or cat111 or both cat and cat111. The above three cases can be matched successfully.)
            • Otherwise, it will fail. (the attribute name is cat, and the bean s in the IOC container are cat111 and cat222, which will fail)
      • @Resource: annotation provided by JDK, first by name and then by type. Finally, as long as a unique bean can be determined, it can be assembled successfully; In case of ambiguity, assembly will not be possible.

Tags: Java Spring Back-end

Posted on Wed, 10 Nov 2021 22:54:52 -0500 by Newladder