SpringBoot Annotation Profiles are automatically mapped to properties and entity classes (solving hard-coding problems)
Configuration File Automatic Mapping Solves Problems
You can customize attribute values from configuration files and automatically map annotations to attributes or entity classes to solve hard-coding problems in code.
Configuration files are automatically mapped to attributes
For example, an upload file control class needs to inject the path attribute of the upload picture.
1. Add a profile path annotation to the Controller class:
The configuration attribute information that needs to be injected is directly customized in application.properties, so application.properties are introduced.
@PropertySource({"classpath:application.properties"})
2. Customize the properties that need to be injected in the application.properties configuration file:
3. Write injected attributes in Controller:
@Value("${web.file.path}") private String filePath;
Configuration files are automatically mapped to entity classes
Configuration files are automatically mapped to properties that can only be injected into the Controller class
To address this limitation
Configuration files can be automatically mapped to entity classes
Then dependency injection entity class
1. Define an entity class ServerSetting.java for mapping:
package com.springboot20.xdclass.springboot.demo2.setting; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource({"classpath:resources.properties"}) @ConfigurationProperties public class ServerSetting { @Value("${test.domain}") private String domain; @Value("${test.name}") private String name; public String getDomain() { return domain; } public void setDomain(String domain) { this.domain = domain; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Note: There are three class annotations
2. Define a configuration information file resources.properties:
3.ServerSetting.java uses @Value to assign values to properties files:
4. Write the unit test class TestSetting.java:
package com.springboot20.xdclass.springboot.demo2; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.springboot20.xdclass.springboot.demo2.setting.ServerSetting; @RunWith(SpringRunner.class) @SpringBootTest public class TestSetting { @Autowired private ServerSetting serverSetting;//Injection ServerSetting @Test public void testSetting(){ System.out.println(serverSetting.getDomain()); System.out.println(serverSetting.getName()); } }
5. Unit test results:
Console output results:
Automatic injection of proof configuration information into entity classes
Note that in ServerSetting.java @Value can be omitted for values in the property configuration file:
resources.properties configuration file:
Execution of unit tests yielded consistent results