Three methods of configuration binding in SpringBoot (@ConfigurationProperties annotation)

The properties configuration file is as follows:

human.name=Mr.Yu
human.age=21
human.gender=male

How to bind the configuration in properties to JavaBean, our previous approach is as follows:

public class PropertiesUtil {
    
    
    public static void getProperties(Person person) throws IOException {
    
    
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/main/resources/demo.properties"));
        //得到配置文件key的名字
        Enumeration enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()){
    
    
            String key =(String)enumeration.nextElement();
            String value = properties.getProperty(key);
            System.out.println("key="+key+"——————value="+value);
            //封装到JavaBean
            //以下内容省略
        }
    }
}

Output result:

key=human.name——————value=Mr.Yu
key=human.age——————value=21
key=human.gender——————value=male

Process finished with exit code 0

You can see that this process is very complicated, but this process will become very simple in SpringBoot.
There are three methods in SpringBoot:

1.@ConfigurationProperties annotation + @Component (or @Controller or @Service or @Repository) annotation

  • Only the components in the container will have the powerful functions provided by SpringBoot, that is, if we need to use the @ConfigurationProperties annotation, then we must first ensure that the pair of JavaBean objects are in the IoC container, so we need to use the Component annotation to add components To the container.
  • In the @ConfigurationProperties annotation, prefix and value are aliases for each other
    Insert picture description here
  • That is @ConfigurationProperties(value = "human")the @ConfigurationProperties(prefix = "human")same
  • prefix and value refer to the meaning of prefix

Code test:
properties configuration file:

human.name=Mr.Yu
human.age=21
human.gender=male

Person class:

@Component 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
@ConfigurationProperties(value = "human")
public class Person {
    
    
    public String name;
    public int age;
    public String gender;

    public Person() {
    
    
    }

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

    public String getName() {
    
    
        return name;
    }

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

    public int getAge() {
    
    
        return age;
    }

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

    public String getGender() {
    
    
        return gender;
    }

    public void setGender(String gender) {
    
    
        this.gender = gender;
    }

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

The test classes are as follows:

//@Controller
@ResponseBody以字符串的方式写给浏览器,而不是跳转到某个页面
//@ResponseBody

//@RestController =  @Controller +  @ResponseBody
@RestController
public class HelloController {
    
    

    //自动注入属性
    @Autowired
    Person person;
    
    @RequestMapping("/person")
    public Person getPerson(){
    
    
        return person;
    }
    
}

Test Results:
Insert picture description here

2.@ConfigurationProperties annotation+@EnableConfigurationProperties annotation

In this way, the @EnableConfigurationProperties annotation must be written on the configuration class. @EnableConfigurationProperties means to turn on the property configuration function, whose property configuration function is turned on, because our Person class above wants to configure binding, so we add it later Parameters Person.class:@EnableConfigurationProperties(Person.class)

The function of @EnableConfigurationProperties(Person.class) is to register the Person component in the container. The name of the object is: human -com.ysw.boot.properties.Person where human is the prefix value in @ConfigurationProperties(value = "human") Value of

There are still @ConfigurationProperties annotations on the Person class. This method replaces the @Component annotation on the Person class with the @EnableConfigurationProperties(Person.class) annotation on the configuration class.
Insert picture description here
Insert picture description here
This method is mainly used when referencing third-party packages, such as the first There is a Person class in the third party package. @Component is not used in this class. We cannot add @Component to the class in the third party package. At this time, we can use the method of adding @EnableConfigurationProperties annotation to the configuration class.

3.@ConfigurationProperties annotation + @Import annotation

  • Use @Import to automatically create this type of component in the container, and the name of the default component is the full class name
  • The @Import annotation has the same effect as the @ConfigurationProperties annotation in 2
  • It's just that they have different names registered in the container:
    • The name of the object registered with the @ConfigurationProperties annotation in the container is: human-com.ysw.boot.properties.Person where human is the value of the prefix value in @ConfigurationProperties(value = "human")
    • The name of the object registered in the container with the @Import annotation is: com.ysw.boot.properties.Person

Configuration class:
Insert picture description here
Person class:
Insert picture description here
Test class:
Insert picture description here
application.properties file:

server.port=8888
human.name=Mr.Yu
human.age=21
human.gender=male222

Test Results:
Insert picture description here

Guess you like

Origin blog.csdn.net/MrYushiwen/article/details/111985310