Spring (3)-Bean automatic assembly

1 Bean automatic assembly

Auto-wiring is a way to use spring to satisfy bean dependencies.
Spring will look for dependent beans for a bean in the application context.

There are three assembly mechanisms for beans in Spring, namely:
1 Explicit configuration in xml
2 Explicit configuration in java
3 Implicit bean discovery mechanism and automatic assembly [important]

Test environment to build
Cat.class

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

Dog.class

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

People.class

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class People {
    
    
    private String name;
    private Cat cat;
    private Dog dog;

}

beans.xml

<?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.zs.pojo.Cat"/>
    <bean id="dog" class="com.zs.pojo.Dog"/>
    <bean id="people" class="com.zs.pojo.People">
        <property name="name" value="zs"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>

</beans>

test

package com.zs.test;

import com.zs.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    

    @org.junit.Test
    public  void test() {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = (People) context.getBean("people");
        people.getDog().shout();
    }
}

ok!!!

Method 1 byName
When a bean node has the attribute of autowire byName
1 will search for all set method names in its class, such as setCat, and obtain a string with the set removed and the first letter lowercase, that is, cat.
2 Go to the spring container to find if there is an object with this string name id.
3 If there is, take out the injection; if not, report a null pointer exception.

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

Insert picture description here

Method 2 ByType
using autowire byType first needs to ensure that objects of the same type are unique in the spring container. If it is not unique, it will report a non-unique exception

    <bean id="people" class="com.zs.pojo.People" autowire="byType">
        <property name="name" value="zs"/>
    </bean>

2 Automatic assembly using annotations

1 Introduce the context file header in the spring configuration file

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

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

2Enable attribute annotation support

<context:annotation-config/>

1@Autowired
1 Automatic assembly by type
2 Direct use on attributes, automatic injection premise is that the Bean object exists in the IOC container

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class People {
    
    
    private String name;
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;

}

3 @Autowired(required=false) Description:
false, the object can be null;
true, the object must be stored in the object, it cannot be null.

2@Qualifier
@Autowired is automatically assembled according to the type, plus @Qualifier can be automatically assembled according to byName
@Qualifier cannot be used alone
Insert picture description here
3@Resource
@Resource If there is a specified name attribute , first perform byName search according to this attribute Assembly
Next, perform the default byName method for assembling.
If none of the above is successful, auto-assemble according to the byType method. If the automatic assembly is
not successful, an exception will be reported.

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.Resource;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class People {
    
    
    private String name;
    @Resource
    private Cat cat;
    @Resource
    private Dog dog;
}

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/112978750