Spring Boot configuration file application.properties

Source: Internet
Author: User

The spring boot default configuration file is Application.properties, placed under the Src/main/resources directory or in the/config directory of the Classpath, as the Spring boot global configuration file. Modify some of the default configurations.

Next use the example to show the usage of the Spring boot configuration file:

First create a new application.properties file under Src/main/resources, with the following content

Author.name= Micro-son blog author.sex= male


ackage com.springboot.properties;import org.springframework.beans.factory.annotation.value;import  org.springframework.web.bind.annotation.RequestMapping;import  org.springframework.web.bind.annotation.restcontroller;//does not need to specify the location of the configuration file because the default configuration file used is @restcontrollerpublic  class democontroller {@Value ("${author.name}")//inject the value in the configuration file private string name; @Value ("${ Author.sex} ") private string sex; @RequestMapping ("/") Public string index () {Return name  +  ":"  + sex;}} 

Create a Portal class

Package Com.springboot.properties;import Org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class App {public static void Main (string[] args) {Springapplication.run (app.class, args);}}

Perform:

650) this.width=650; "src=" Http://www.weare.net.cn/EasyWeb/upload/image/20161219/1482132936140081094.png "title=" Micro-son blog "alt=" micro-blog "width=" 331 "height=" "style=" Border:0px;vertical-align:middle;height:auto;/>

So how do you use the other configuration files?

Create a new author.properties with the following content:

Person.name=weareperson.sex=man

Modify the Democontroller to:

@RestController @propertysource ("classpath:author.properties")//Specify Config file public class Democontroller {@Value ("${ Person.name} ") private string name; @Value (" ${person.sex} ") private string sex; @RequestMapping ("/") public string Index ( {return name + ":" + Sex;}}

Run

650) this.width=650; "src=" Http://www.weare.net.cn/EasyWeb/upload/image/20161219/1482134451232044044.png "title=" Micro-son personal blog "alt=" micro-son personal blog "width=" 325 "height=" 142 "style=" Border:0px;vertical-align:middle;height:auto; "/>

The properties in the example above are injected one by @value, but if many of the injected properties are cumbersome, then use @configurationproperties to associate the profile properties with the Bean's properties

To create the author class:

package com.springboot.properties;import  org.springframework.boot.context.properties.configurationproperties;import  org.springframework.stereotype.Component; @Component//declaration This is a bean@configurationproperties (prefix= "author")// Specifies the prefix in the configuration file public class author {private string name;private string sex; Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public string getsex ()  {return sex;} Public void setsex (String sex)  {this.sex = sex;}} 

To modify the Democontroller class:

@RestController @configurationpropertiespublic class Democontroller {@Autowiredprivate Author Author; @RequestMapping ("/") public String Index () {return author.getname () + ":" + Author.getsex ();}}

Run the app class with the results such as:

650) this.width=650; "src=" Http://www.weare.net.cn/EasyWeb/upload/image/20161219/1482132936140081094.png "title=" Micro-son blog "alt=" micro-blog "width=" 331 "height=" "style=" Border:0px;vertical-align:middle;height:auto;/>

For more information, please visit Weibo blog

This article is from the "c67ed4" blog, make sure to keep this source http://c67ed4.blog.51cto.com/3429042/1884005

Spring Boot configuration file application.properties

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.