Spring annotation development 03 ------- attribute assignment and automatic assembly

Posted by cherubrock74 on Sun, 30 Jan 2022 20:07:13 +0100

Attribute assignment

summary

In Spring, we can use @ Value to assign values to our properties.
Using @ Value assignment, you can:

  1. Basic parameters
  2. Spel expressions can be written: #{}
  3. You can use ${} to extract the value in the configuration file (. properties) (that is, the value in the running environment)

test

1. Write the configuration file person Properties, set the nickname of the person in the configuration file
person.nickName = three eggs
2. Write the person class:

package com.xdw.pojo;

import org.springframework.beans.factory.annotation.Value;

public class Person {

    // Assignment with @ Value:
    // 1. Basic parameters
    // 2. You can write spiel: #{}
    // 3. You can write ${}; Take out the value of [properties] in the configuration file (the value in the running environment variable)

    @Value("Zhang San")
    private String name;

    @Value("#{20-2*3}")
    private Integer age;

    @Value("${person.nickName}")
    private String nickName;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

3. Write configuration class

package com.xdw.config;

import com.xdw.pojo.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
// @PropertySource loads the configuration file under the specified path into the program running environment
@PropertySource(value="classpath:person.properties")
@Configuration
public class MainConfigOfValueProperty {

    @Bean
    public Person person() {
        return new Person();
    }

}

4. Write test class

import com.xdw.config.MainConfigOfValueProperty;
import com.xdw.pojo.Person;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestProperty {
    @Test
    public void test01() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfValueProperty.class);
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person);
    }
}

5. Operation test

summary

@The Value annotation can be used in the following three ways:

  1. Basic parameters
  2. Spel expressions can be written: #{}
  3. You can use ${} to extract the value in the configuration file (. properties) (that is, the value in the running environment)
  4. We can use the @ PropertySource annotation to load our configuration file into the runtime environment

automatic assembly

summary

Automatic assembly: Spring uses dependency injection (DI) to complete the assignment of dependencies of various components in the IOC container

Method 1: Spring comes with annotation @ autowritten

  1. By default, the annotation takes precedence to find the corresponding component in the container according to the type, and the value will be assigned directly when it is found
  2. If multiple components of the same type are found, the name of the attribute is used as the id of the component to find it in the container
  3. The @ Qualifier annotation can be used to specify the id of the component to be assembled
  4. The components must be assembled by default for automatic assembly, otherwise an error will be reported! This default configuration can be modified using @ Autowired(required = false)
  5. @The Primary annotation is placed on the bean or the method of creating the bean. It is preferred when Spring performs default assembly!

Method 2: @ Resource(JSR250) and @ inject (jsr330) [annotation of Java Specification]

@Resource:

  1. Like @ autowritten, it can realize automatic assembly function: assembly is carried out by component name by default
  2. Neither @ Primary annotation nor @ Autowired(required = false) configuration is supported

@Inject:

  1. Need to import javax The function of input dependency is the same as that of @ autowritten, but there is no function of required=false
<!-- support JSR330 standard -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

Automatic assembly is relatively simple to use. There is no test here. You can refer to the document to test yourself!

Topics: Spring