Springboot custom profiles and read configuration files

Source: Internet
Author: User

This article from the "Knowledge Forest"

Read the core configuration file

The core configuration file refers to the resources root directory application.properties or application.yml configuration file, read the two configuration files in two ways, are relatively simple.

The contents of the core configuration file application.properties are as follows:

server.port=9090test.msg=Hello World Springboot!
    • How to use @Value (common):
@RestControllerpublic class WebController { @Value("${test.msg}") private String msg; @RequestMapping(value = "index", method = RequestMethod.GET) public String index() { return "The Way 1 : " +msg; }}

Note: the @Value key name in the core configuration file is included in the ${}. Adding to the controller class @RestController means that all views in this class are displayed in JSON, similar to adding on a view method @ResponseBody .

Access: Http://localhost:9090/index will getThe Way 1 : Hello World Springboot!

    • How to use Environment
@RestControllerpublic class WebController { @Autowired private Environment env; @RequestMapping(value = "index2", method = RequestMethod.GET) public String index2() { return "The Way 2 : " + env.getProperty("test.msg"); }}

Note: This is done by relying on injection, Evnironment adding annotations on the created member variable to private Environment env complete the @Autowired dependency injection, and then using it to env.getProperty("键名") read the corresponding value.

Access: HTTP://LOCALHOST:9090/INDEX2 will getThe Way 2 : Hello World Springboot!

To read a custom configuration file

In order not to destroy the original ecology of the core file, but also need to have the custom configuration information exists, in general will choose the custom configuration file to put these custom information, here in the resources/config directory to create the configuration filemy-web.properties

resources/config/my-web.propertiesThe contents are as follows:

1.0web.author=[email protected]

To create an entity class that manages the configuration:

@ConfigurationProperties (locations ="Classpath:config/my-web.properties", prefix ="Web")@ComponentPublicClassMywebconfig {private String name;Private String version;Private String author;Public Stringgetauthor () {return author;} public String getname () {return Name } public String getversion () { return version; } public void setAuthor (String Author) {this.author = author;} public void setName (String name) { Span class= "Hljs-keyword" >this.name = name; } public void setVersion (String Version) {this.version = version;}}         

Attention:

    • @ConfigurationPropertiesThere are two properties in the note:

      • locations: Specify the location of the configuration file
      • prefix: Specifies the prefix of the key name in the configuration file (I have all the key names in the configuration file to web. begin with)
    • Use @Component is to allow the class to be used elsewhere, using @Autowired annotations to create instances.

Create a test controller
 @RestController  @RequestMapping (value = " config ") public Span class= "Hljs-class" >class configcontroller { @Autowired private mywebconfig mywebconfig;  @RequestMapping (value =  "index", method = Requestmethod.get ) public String index () {return Span class= "hljs-string" > "WebName:" +mywebconfig.getname () + "webversion:" + Mywebconfig.getversion () +   

Note: because annotations are added to the Mywebconfig class @Component , you can use it here directly @Autowired to create its instance object.

Access: Http://localhost:9090/config/index will getwebName: zslin, webVersion: V 1.0, webAuthor: [email protected]

Example code: HTTPS://GITHUB.COM/ZSL131/SPRING-BOOT-TEST/TREE/MASTER/STUDY02

This article from the "Knowledge Forest"

Springboot custom profiles and read configuration files

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.