Springboot-read the core configuration file and custom configuration file

Source: Internet
Author: User

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)
1 @RestController2  Public classWebcontroller {3 4@Value ("${test.msg}")5     PrivateString msg;6 7@RequestMapping (value = "Index", method =requestmethod.get)8      PublicString Index () {9         return"The 1:" +msg;Ten     } One}

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
@RestController  Public class Webcontroller {    @Autowired    Private  environment env;     = "Index2", method = requestmethod.get)    public  String index2 () {        return "The 2:" + env.getproperty ("test.msg");}    }

 Note:   This approach is dependent on injecting  evnironment   to complete, create the member variable  private Environment env   plus   @Autowired   Note to complete the dependency injection and then use the  env.getproperty ("Key Name")  

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

To read a custom configuration file
    • Using @ConfigurationProperties annotations to inject and get configuration through getter, setter methods

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:



Web.jdbc.password=root

 

To create an entity class that manages the configuration:

@ConfigurationProperties (locations = "Classpath:config/my-web.properties", prefix = "web") @Component Public classmywebconfig{PrivateString name; Privatejdbc JDBC; classJDBC {PrivateString username; PrivateString password; //getter ...    }     PublicInteger Geport () {return  This. Port; }     PublicJdbc Getjdbc () {return  This. jdbc; }}
Attention:
    1. @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)
    2. Use @Component is to allow the class to be used elsewhere, using @Autowired annotations to create instances.

    • Through @PropertySource annotations, and then using a per- @Value injection configuration
1 @Configuration2@PropertySource ("Classpath:test.properties")3  Public classElconfig {4 5@Value ("${book.name}")6     PrivateString bookname;7 8     //Propertysourcesplaceholderconfigurer This bean, this bean is mainly used to solve the ${used in @value ...} Placeholder. If you don't use ${...} placeholder, you can not use this bean. 9 @BeanTen      Public Staticpropertysourcesplaceholderconfigurer propertyconfigure () { One         return Newpropertysourcesplaceholderconfigurer (); A     } -  -      Public voidOutputsource () { the System.out.println (bookname); -     } -}

 

Springboot-read the core configuration file and custom configuration file

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.