1024programmer Blog [SpringBoot] Project property configuration – Programmer Sought

[SpringBoot] Project property configuration – Programmer Sought

class=”markdown_views prism-atom-one-light”>

Briefly introduce the property configuration of SpringBoot.

Continue to the blog in the previous section,

Configure application.properties file:

#The first configuration method
 #In this configuration method, each attribute name must be written in full
 server.port=8081
 # configure port
 server.context-path=/hello
 #Configure project path

You can run it and have a look. At this time, you need to add /hello project name to the path to access the project.
And the port is 8081, the default is 8080

Before performing the second configuration method, delete the application.properties file first
I will not delete it, I renamed it application.txt file, I suggest you delete it, choose the second configuration method

The second configuration method-recommended

Create a new file in the resource directory: application.yml

server:
   port: 8081
 # : There must be a space after the number
   context-path: /hello

This configuration is much more convenient, without the full name.
There is a note, please see the comments in the code

The running result is the same as the first configuration method

Configuration variables for customization

For example, add:

server:
   port: 8082
 # : There must be a space after the number
   context-path: /hello
 name: Chen Haoxiang
 age: 20

We don’t need to configure the variable type here, just write the attribute type when injecting
We are using @Value injection

Read configuration in code:

package cn.chenhaoxiang;

 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;

 /**
  * Created with IntelliJ IDEA.
  * User: Chen Haoxiang.
  * Date: 2017/12/25.
  * Time: 9:44 PM.
  * Explain:
  */
 @RestController
 public class   HelloController {
     

     @Value("${name}")//  This variable reading method is a bit like jsp reading session's 
     private String name;

     @Value("${age}")
     private Integer age;

     @RequestMapping(value = "/hello", method = RequestMethod.GET)
     public String say() {
         return "Hello Spring Boot!";
     }

     @RequestMapping(value = "/info", method = RequestMethod.GET)
     public String info() {
         return name+","+age;
     }
 }
 

After running, enter the address in the browser to see the running results

You can also use the configuration in the configuration, we can write this in the configuration file:

info: "name:${name}  ,age:${age}"

In this way, the value of name and age can be referenced in the configuration

Did you find that the above configuration method is a bit troublesome? If I have many attributes, don’t I have to write a lot of reading and writing?
Don’t worry, there must be a simple way. At this time, we can choose to use classes to encapsulate

We define a People class.
There are age, name, address attributes
Let’s look at the code next, the code explains everything

package cn.chenhaoxiang;

 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;

 /**
  * Created with IntelliJ IDEA.
  * User: Chen Haoxiang.
  * Date: 2017/12/25.
  * Time: 9:58 pm.
  * Explain:
  */
 @Component //Required for injecting Bean
 @ConfigurationProperties(prefix = "people")//Get  The prefix is ​​the configuration of people
 public class   People {
     

     private String name;

     private Integer age;

     private String address;

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Integer getAge() {
         return age;
     }

     public void setAge(Integer age) {
         this.age = age;
     }

     public String getAddress() {
         return address;
     }

     public void setAddress(String address) {
         this.address = address;
     }

     @Override
     public String toString() {
         return "People{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 ", address='" + address + '\'' +
                 '}';
     }
 }
 

Go to the configuration to configure the value of People:

people:
   name: chx
   age: 20
   address: Changsha

HelloController.java
We can inject People’s Bean like this:

@Autowired
     private People people;

 @RequestMapping(value = "/people", method = RequestMethod.GET)
 public People people() {
     return people;
 }//The return is the JSON string of the object

Let’s look at the output:

Dynamic configuration

For example, the database address we use is different when we develop and release, we can configure it like this

Create two configuration files, respectively:
application-dev.yml for development and use
application-prod.yml for release and use

The contents of the default configuration file can be deleted. Write:
application.yml

spring:
   profiles:
     active: dev

Modify the value in application-dev.yml, which is different from application-prod.yml. At this time, you can run the project, open the link, and you can see that the value of people is the content of the dev file
You can change the dev Change to prod, the configuration content is the content in the prod file

But this is not dynamic yet, because we need to change the value in application.yml every time.
So we can do this, using the startup method of the previous blog
That is, the startup method of java -jar

Compile first:

mvn install

Then run:

java -jar target/hello-0.0.  1-SNAPSHOT.jar --spring.profiles.active=prod

Just add dynamic parameters after it

Source code download address:

GITHUB source code download address:
click me to download]

This article was written by [问忆], all rights reserved.
Welcome to reprint, sharing is the source of progress.

Reprint please indicate the source: http://chenhaoxiang.cn/2017/ 12/25/0001/
This article is from Memory’s Blog]

Just bring dynamic parameters

Source code download address:

GITHUB source code download address:
click me to download]

This article was written by [问忆], all rights reserved.
Welcome to reprint, sharing is the source of progress.

Reprint please indicate the source: http://chenhaoxiang.cn/2017/ 12/25/0001/
This article is from Memory’s Blog]

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/springboot-project-property-configuration-programmer-sought/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索