Spring boot uses the custom properties "Learn Spring boot from scratch"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.