Ext.: http://blog.csdn.net/linxingliang/article/details/52069515
Spring boot uses application.properties to default a number of configurations. But what should we do when we need to add some configuration for ourselves?
If you continue to add in application.properties
Such as:
1. Wisely2.name=wyf2
2. Wisely2.gender=male2
To define a configuration class:
1. @ConfigurationProperties (prefix = "wisely2")
2. Public class Wisely2settings {
3. private String name;
4. private String gender;
5. public String getName () {
6. return name;
7.}
8. Public void setName (String name) {
9. this. Name = name;
10.}
One. public String Getgender () {
return gender;
13.}
Public void Setgender (String gender) {
. This . gender = gender;
16.}
17.
18.}
If new configuration file is used
As I create a new wisely.properties
1. Wisely.name=wangyunfei
2. Wisely.gender=male
You need to define the following configuration class
1. @ConfigurationProperties (prefix = "wisely", locations = "Classpath:config/wisely.properties")
2. Public class Wiselysettings {
3. private String name;
4. private String gender;
5. public String getName () {
6. return name;
7.}
8. Public void setName (String name) {
9. this. Name = name;
10.}
One. public String Getgender () {
return gender;
13.}
Public void Setgender (String gender) {
. This . gender = gender;
16.}
17.
18.}
Finally note the Spring boot entry class plus @enableconfigurationproperties
1. @SpringBootApplication
2. @EnableConfigurationProperties ({wiselysettings. Class, Wisely2settings. class})
3. Public class DemoApplication {
4.
5. Public static void main (string[] args) {
6. Springapplication.run (demoapplication. Class, args);
7.}
8.}
Using the defined properties
1. @Controller
2. Public class TestController {
3. @Autowired
4. Wiselysettings wiselysettings;
5. @Autowired
6. Wisely2settings wisely2settings;
7.
8. @RequestMapping ("/test")
9. public @ResponseBody String test () {
System.out.println (Wiselysettings.getgender () + "---" +wiselysettings.getname ());
One. System.out.println (Wisely2settings.getgender () + "= = =" +wisely2settings.getgender ());
return "OK";
13.}
14.}
Spring boot uses the custom properties "Learn Spring boot from scratch"