1. Calling of configuration documents
After starting, enter http://localhost:18080/user/test directly in the browser , and print out the configuration content in the configuration file.
2. Bind object bean call
Sometimes there are too many attributes, and it is too tiring to bind to the attribute fields one by one. The official advocates binding an object bean. Here we build a ConfigBean.java class. At the top, you need to use the annotation @ConfigurationProperties(prefix = "com") to indicate which one to use.
@ConfigurationProperties(prefix = "com")public class ConfigBean { private String name; private String id; // omit getter and setter}After the configuration, you need to add @EnableConfigurationProperties to the spring Boot entry class and specify which bean to load. If you do not write ConfigBean.class, add it to the bean class.
@SpringBootApplication@EnableConfigurationProperties({ConfigBean.class})public class Chapter2Application { public static void main(String[] args) { SpringApplication.run(Chapter2Application.class, args); }}Finally, you can use ConfigBean in Controller, as follows:
@RestControllerpublic class UserController { @Autowired ConfigBean configBean; @RequestMapping("/") public String hexo(){ return configBean.getName()+configBean.getId(); }} 3. Reference between parameters
You can also directly refer to each parameter in application.properties, just like the following settings:
com.name="Zhang San"com.id="8"com.psrInfo=${com.name}numbered as ${com.id}In this way, we can just use the psrInfo property
4. Use custom newly created configuration files
We create a new bean class as follows:
@Configuration@ConfigurationProperties(prefix = "com.md") @PropertySource("classpath:test.properties")public class ConfigTestBean { private String name; private String want; // Omit getter and setter} The main thing is to add an annotation :@PropertySource("classpath:test.properties")
5. Configuration file priority
The application.properties and application.yml files can be placed in four locations:
Similarly, this list is sorted by priority, that is, application.properties under src/main/resources/config override the same properties in application.properties under src/main/resources, as shown in the figure:
In addition, if you have both application.properties and application.yml at the same priority position, then the properties in application.yml will override the properties in application.properties.
ps: Let's take a look at SpringBoot's application.properties file to read
SpringBoot reads application.properties files, usually there are 3 ways
1. @Value For example:
@Value("${spring.profiles.active}")private String profileActive;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2. @ConfigurationProperties For example:
@Component@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")public class TestProperties {String url;String key;}When used in other classes, you can directly inject the TestProperties to access the relevant values.
3. Use Environment For example:
private Environment env;env.getProperty("test.url");The env method is less efficient
Note: @ConfigurationProperties can also be used in other .properties files, as long as locations are specified
Summarize
The above is the use of the Spring Boot configuration file application.properties introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!