Use the configuration file application. properties in Spring Boot,
1. Call configuration items in the configuration document
EnterHttp: // localhost: 18080/user/testTo directly print the configuration content in the configuration file.
Ii. Bind object bean call
Sometimes there are too many attributes, and it is too tired to bind them to attribute fields one by one. The official Government advocates binding an object bean. Here we create a ConfigBean. java class. The annotation @ ConfigurationProperties (prefix = "com") must be used at the top to specify which java class to use.
@ ConfigurationProperties (prefix = "com") public class ConfigBean {private String name; private String id; // omit getter and setter}
After configuration, add @ EnableConfigurationProperties to the spring Boot entry class and specify the bean to be loaded. If ConfigBean. class is not written, add the bean class
@SpringBootApplication@EnableConfigurationProperties({ConfigBean.class})public class Chapter2Application { public static void main(String[] args) { SpringApplication.run(Chapter2Application.class, args); }}
Finally, introduce ConfigBean to the Controller as follows:
@RestControllerpublic class UserController { @Autowired ConfigBean configBean; @RequestMapping("/") public String hexo(){ return configBean.getName()+configBean.getId(); }}
Iii. Parameter reference
Parameters in application. properties can also be directly referenced to use them, just like the following settings:
Com. name = "Zhang San" com. id = "8" com. psrInfo =$ {com. name} No. $ {com. id}
In this way, we can just use the property psrInfo.
4. Use the custom configuration file
Create a 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}
Add an annotation.:@PropertySource("classpath:test.properties")
V. configuration file priority
The application. properties and application. yml files can be placed in the following four locations:
- External, In the/congfig subdirectory relative to the application program running directory.
- External, in the directory where the application is running
- Built-in, within the config package
- Built-in, in the root directory of Classpath
Similarly, this list is sorted by priority. That is, application. properties under src/main/resources/config overwrites the same properties in application. properties under src/main/resources,
In addition, if you have both application. properties and application. yml at the same priority, the properties in application. yml will overwrite the properties in application. properties.
Ps: Let's see how SpringBoot reads the application. properties file.
SpringBoot can read the application. properties file in three ways.
1. @ Value for example:
@ Value ("$ {spring. profiles. active}") private String profileActive; ------ injects spring. profiles. active from the properties file into the variable profileActive.
2. @ ConfigurationProperties for example:
@Component@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")public class TestProperties {String url;String key;}
When using other classes, you can directly inject the TestProperties to access related values.
3. Use Enviroment for example:
private Enviroment env;env.getProperty("test.url");
However, the env method is less efficient.
Note: @ ConfigurationProperties can also be used in other. properties files, As long as locations is specified.
Summary
The above section describes the configuration file application in Spring Boot. if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!