[6] Automatic assembly of Bean in Spring5

7, automatic assembly of Bean

  • Auto-wiring is a way for Spring to satisfy bean dependencies
  • Spring will automatically find in the context and automatically assemble properties for the bean

There are three ways of assembly in Spring:

  1. Configure explicitly in xml
  2. Explicit configuration in java
  3. Implicit autowiring bean【important】

7.1, test

Environment construction: one person has two pets

Person class, get, set and other methods omitted

public class Person {
    
    
    private String name;
    private Dog dog;
    private Cat cat;
}

dog class

public class Dog {
    
    
    public void shout(){
    
    
        System.out.println("wangwangwang!");
    }
}

cat class

public class Cat {
    
    
    public void shout(){
    
    
        System.out.println("miaomiaomiao!");
    }
}

7.2, byName automatic assembly

    <bean id="cat" class="com.kuber.pojo.Cat"/>
    <bean id="dog" class="com.kuber.pojo.Dog"/>
    <!--
    byName:会自动在容器上下文中查找,这个是将set方法的名称去掉开头的set后全部小写得到的,然后和引入的beanid进行匹配
    -->
    <bean id="person" class="com.kuber.pojo.Person" autowire="byName">
        <property name="name" value="工藤胖虎"/>
        <!--<property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>-->
    </bean>

7.3, byType automatic assembly

<bean class="com.kuber.pojo.Cat"/>
<bean class="com.kuber.pojo.Dog"/>

<!--
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean(class)进行匹配(因此可以不需要beanid)
-->
<bean id="person" class="com.kuber.pojo.Person" autowire="byType">
    <property name="name" value="工藤胖虎"/>
    <!--<property name="cat" ref="cat"/>
    <property name="dog" ref="dog"/>-->
</bean>

7.4 Summary

  • When byName, you need to ensure that the id of all beans is unique, and the bean needs to be the same as the automatically injected set method name after removing the first letter of the value with the lowercase letter
  • When byType, you need to ensure that the class of all beans is unique, and the bean needs to be the same as the automatically injected property type.

7.5. Use annotations to realize automatic assembly

  1. Import constraints

    <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">
            
    </beans>
    
  2. Use configuration

    <?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 id="cat" class="com.kuber.pojo.Cat"/>
        <bean id="dog" class="com.kuber.pojo.Dog"/>
    
        <!--
        byName:会自动在容器上下文中查找,这个是将set方法的名称去掉开头的set后全部小写得到的,然后和引入的beanid进行匹配
        byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean(class)进行匹配(因此可以不需要beanid)
        -->
        <bean id="person" class="com.kuber.pojo.Person">
            <property name="name" value="工藤胖虎"/>
            <!--<property name="cat" ref="cat"/>
            <property name="dog" ref="dog"/>-->
        </bean>
    </beans>
    

@Autowired annotation

You can use it directly on the attribute, or you can use it in the set mode

After use @Autowired, there is no need to write the set method, provided that your automatically assembled property IOC (Spring) container exists and meets the type byType (Autowired is matched by type by default, and if it is not found, press byName to match)

Expand

If the environment of @Autowired automatic assembly is more complicated and automatic assembly cannot be completed with an annotation [@Autowired], we can use @Qualifier(value = “xxx”) (matching with beanid) to cooperate with the use of @Autowired, specify Injection of a unique bean object

@Resource annotation (removed in jdk11)

public class Person {
    
    

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

@Resource is automatically assembled according to byName by default. If it is not found, it is assembled according to byType. You can also specify name and beanid to match

<bean id="cat1" class="com.kuber.pojo.Cat"/>
<bean id="cat123" class="com.kuber.pojo.Cat"/>
<bean id="dog1" class="com.kuber.pojo.Dog"/>
public class Person {
    
    

    private String name;
    @Resource
    private Dog dog;
    @Resource(name = "cat123")
    private Cat cat;
}
@Test
public void test1(){
    
    
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person.getName());
    person.getCat().shout();
    person.getDog().shout();

}

At this time, if the name is not specified, the test will report an error, because byName cannot find the id of cat, and then by Type to find there are multiple, so NoUniqueBeanDefinitionException will be thrown. It is normal if the name is "cat123".

Summary :

The difference between @Autowired and @Resource

  • All are used for automatic assembly, can be placed on the field, and the set method is no longer needed after use

  • @Autowired is implemented by byType by default, and the object must exist. The type is not found or there are multiple beans of the same type and then match byName. If it is not found, an error will be reported, but @Qualifier(value = "xxx") (and beanid) Match) to cooperate with the use of @Autowired, specify a unique bean object injection, [commonly used]

  • @Resource is automatically assembled according to byName by default by default. If it is not found, it will be assembled according to byType. You can also specify name and beanid for matching [common]

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/110353315