Spring IOC 12 annotation based configuration

Annotation injection is performed prior to XML injection, so when both methods are used for injection, the XML configuration overrides the annotation configuration.

They are implicitly registered in the following way, and implemented through the post processor.

<?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/>
</beans>

(implicitly registered postprocessors include Autowired annotation beanpostprocessor, common annotation beanpostprocessor, persistence annotation beanpostprocessor, required annotation beanpostprocessor)

@Required

On the setter method, it indicates that the affected bean property must be explicitly assigned or automatically injected through the value in the bean definition during configuration. If not specified, NullPointerException similar exception will be thrown.

@Autowired

It can be used on construction methods. If there is only one construction method, it is unnecessary. If there are multiple construction methods, at least one uses Autowired to let the container know which construction method to use. Also, the container must have all the bean s needed to identify the constructor for Autowired.

@Autowired
public Person(Address address) {
    this.address = address;
}

Can be used on setter methods

@Autowired
public void setAddress(Address address) {
    this.address = address;
}

Can be used on any signature method

@Autowired
public void testAutowired(Address address){
    this.address = address;
}

Can be used on property fields

@Autowired
private Address address;

Even the above methods can be mixed
It can also be used for arrays and collections. Only maps with the key of String type can be injected. If you want to arrange them in a specific Order, the bean needs to implement the Ordered interface, or use @ Order or @ Priority to label them.
By default, automatic injection fails when no candidate bean s are available, which can be replaced by the following methods.

@Autowired(required = false)
public void setAddress(Address address) {
    this.address = address;
}

// This will return to Optional.Empty
@Autowired
public void setAddress(Optional<Address> address) {
    this.address = address;
}

@Autowired
public void setAddress(@Nullable Address address) {
    this.address = address;
}

@Primary

If there are multiple candidates and only one specific type of bean dependency is needed by another bean, the dependency marked with @ Primary annotation will be explicitly used. Otherwise, a NoUniqueBeanDefinitionException will be reported.

<bean class="com.nanborone.bean.AddressConfiguration"/>
public class AddressConfiguration {

    @Bean
    @Primary
    public Address firstAddress(){
        return new Address("first","first","first");
    }
    
    @Bean
    public Address secondAddress(){
        return new Address("second","second","second");
    }
}

@Resource

@Resource only supports field and bean property setter methods with a single parameter. In other cases, @ Qualifier can only be used
When @ Resource does not have the name attribute, first search in the container according to the field name as the bean's identity, if not through the field type.

@Qualifiers

When you need to make more constraints on the selection process, you can use the @ Qualifier annotation to bind the qualified values for the specified parameters.

<bean id="book1" class="com.nanborone.bean.Book">
    <property name="price" value="1771"/>
    <qualifier value="book1"/>
</bean>
<bean id="book2" class="com.nanborone.bean.Book">
    <property name="price" value="1772"/>
    <qualifier value="book2"/>
</bean>

The unique ID of the bean can also be used as the backup Qualifier value, so the effect of node removal in the above file is the same.

// Can be used on property fields
@Autowired
@Qualifier("book2")
private Book book;

// It can also be used to construct method parameters
@Autowired
public Person(@Qualifier("book2") Book book) {
    this.book = book;
}

// It can also be used in any signature method
@Autowired
public void testQulifier(@Qualifier("book2") Book book) {
    this.book = book;
}

You can use generics as autoassemble qualifiers

public class AddressConfiguration {

    @Bean
    public Address<String> firstAddress() {
        return new Address<String>("first", "first", "first");
    }

    @Bean
    public Address<Integer> secondAddress() {
        return new Address<Integer>(2, 3, 4);
    }
}

public class AddressTest {

    @Autowired
    private Address<String> address1;

    @Autowired
    private Address<Integer> address2;
}
Published 16 original articles, won praise 0, visited 387
Private letter follow

Tags: xml Spring encoding Attribute

Posted on Fri, 14 Feb 2020 07:19:07 -0500 by pootergeist