How to read the core profile application and customize the properties configuration file in Spring-boot __spring-boot

Source: Internet
Author: User

To understand the spring-boot of this technique, you should know Spring-boot's core profile application.properties, and of course you can customize the profile **.properties information through annotations.


I. Reading the contents of the core profile information Application.properties

A core profile is a application.properties or application.yml configuration file that is in the resources root directory, and there are two ways to read the two configuration files, which are simpler.


(1) The core configuration file Application.properties content as follows:

[Java] view plain copy Test.msg=hello World Springboot

(1) mode one: Use @value method (commonly used)

[Java]View plain copy package Solin.controller;   Import Org.springframework.beans.factory.annotation.Value;   Import org.springframework.web.bind.annotation.RequestMapping;      Import Org.springframework.web.bind.annotation.RestController;              @RestController public class Webcontroller {@Value ("${test.msg}") Private String msg;       @RequestMapping ("/index1") public String index1 () {return "way one:" +MSG; Note: The ${} in @value contains the key names in the core configuration file. Adding @restcontroller on the controller class indicates that all views in this class are displayed in JSON, similar to adding @responsebody to the view method.
Access: http://localhost:8088/index1: "Way One: Hello World Springboot"


(2) mode two: Use Environment way

[Java]  View plain  copy package solin.controller;      import  org.springframework.beans.factory.annotation.autowired;   import  org.springframework.beans.factory.annotation.value;   import  org.springframework.core.env.environment;   import  org.springframework.web.bind.annotation.requestmapping;   import  org.springframework.web.bind.annotation.restcontroller;      @RestController    public class webcontroller {        @Autowired         private Environment env;                @RequestMapping ("/index2")         public String  Index2 () {           return  "mode two:" +env.getproperty (" Test.msg ");       } &NBSp   NOTE: This approach relies on injection evnironment to complete, adding @autowired annotations on the created member variable private environment env to complete dependency injection, and then using Env.getproperty ("Key Name ") to read the corresponding value.

Access: Http://localhost:8088/index2: "mode two: Hello World Springboot"

(2) The core configuration file Application-dev.yml content as follows:

[Java] view plain Copy #--** Bank configuration file path start
Czb
Configpath:/usr/local/czbconfig/config.properties
#-Zhejiang merchant Bank related configuration file path end application*.yml Core configuration file, also read its contents in the above two ways. For example: The following figure is used to read:

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.