SpringBoot profile (c)

1. SpringBoot Profile Type

1.1 SpringBoot profile type and role

  SpringBoot is based on the contract, if you want to customize the configuration to replace the default configuration, then you can use application.properties or application.yml (application.yaml) to be configured.

  SpringBoot default load application.properties or application.yml (application.yaml) file from the Resource Directory. application.properties file is a key file types, we often use here is not to do too much explanation. We analyze the main yml configuration file.

1.2 application.yml profile

1.2.1 yml profile introduction

  Format yml file is YAML, YAML is an intuitive can be recognized by the computer of the data sequence format, easy to read, easy and scripting languages ​​interact, may be introduced to support YAML libraries in different programming languages ​​(such as C / C ++ , Ruby, Python, Java, Perl, C #, PHP, etc.). yml file is data-centric, more compact than the traditional way of xml.

  Yml file extensions can be .yml or .yaml.

Syntax 1.2.2 yml profile

(1) Common Data Configuration

  Syntax: key: value

# General data
name: by2

Note: There is a space before value

(2) Configuration of object data

  grammar:

  key:

  key1: value1

  key2: value2

  or:

  key:{key1: value1,key2: value2}

# Configuration object data
idol:
  name: by2
  age: 27
  addr: beijing
# Alternatively (used above kind)
#person: {name: by2,age: 27, addr: beijing}

Note: the number of spaces is not limited to the foregoing key1, yml syntax in the same indentation level represent the same

(3) the configuration data map

  With the object syntax above.

(4) configuration array (List, Set) data

  grammar:

  key:

  - value1

  - value2

  or

  key:{value1,value2}

# Configuration array
image:
  - yumi
  - miko
  - yumiko
# Alternatively (used above kind)
#image: [yumi, miko, yumiko]
# Element is in the form of an object
by2:
  - name: yumi
    age: 27
    cname: 'Sweet Corn'
  - name: miko
    age: 27
    cname: 'vested cake'

Note: value1 and before - there is a space between

1.3 SpringBoot query configuration information

  SpringBoot official document: https: //docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#boot-features-kotlin-configuration-properties

  If we want to modify the default configuration SpringBoot, you can refer to the official documents relevant to the query key value.

  Common configuration excerpt follows:

# QUARTZ SCHEDULER (QuartzProperties) 
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode. spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@. sql # Path to the SQL file to use to initialize the database schema. 
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties. 

# ---------------------------------------- # 
WEB PROPERTIES 
# ---------------------------------------- 

# EMBEDDED SERVER CONFIGURATION (ServerProperties) 
server.port=8080 # Server HTTP port. 
server.servlet.context-path= # Context path of the application. 
server.servlet.path=/ # Path of the main dispatcher servlet. 

# HTTP encoding (HttpEncodingProperties) 
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly. 

# JACKSON (JacksonProperties) 
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`. 

# SPRING MVC (WebMvcProperties) 
spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher servlet. 
spring.mvc.static-path-pattern=/** # Path pattern used for static resources. 
spring.mvc.view.prefix= # Spring MVC view prefix. 
spring.mvc.view.suffix= # Spring MVC view suffix. 

# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) 
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto- detected based on the URL by default. spring.datasource.password= # Login password of the database. 
spring.datasource.url= # JDBC URL of the database. 
spring.datasource.username= # Login username of the database. 

# IS (Elasticsearch HTTP client) (JestProperties) 
spring.elasticsearch.jest.password # = Login password. 
spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use. 
spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use. 
spring.elasticsearch.jest.read-timeout=3s # Read timeout. 
spring.elasticsearch.jest.username= # Login username.

2. Configuration file attribute mapping mode configuration class

2.1 mapping using annotations @Value

  Bean can be on the field by @Value notes the configuration values ​​in the map file to a Spring-managed

  E.g:

  application.yml configuration is as follows:

idol:
  name: by2
  age: 27
  addr: beijing

  Entity Bean code is as follows:

@Controller
public class Quick2Controller {
    @Value("${idol.name}")
    private String name;
    @Value("${idol.age}")
    private String age;

    @RequestMapping("/quick2")
    @ResponseBody
    public String quick2(){
        return "name:" + name + ",age:" + age;
    }
}

  Browser to access the results:

 

 

 2.2 mapping using annotations @ConfigurationProperties

  The configuration file may be automatically configured with the mapping entity annotation @ConfigurationProperties (prefix = "profile of the key prefix")

  E.g:

  application.yml the same configuration as above.

  Entity Bean code is as follows:

@Controller
@ConfigurationProperties(prefix = "idol")
public class Quick2Controller2 {
    private String name;
    private String age;
    private String addr;

    @RequestMapping("/quick3")
    @ResponseBody
    public String quick3(){
        return "name:" + name + ",age:" + age + ",addr" + addr;
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }
}

  Browser to access the following results:

 

 

 Note: When using @ConfigurationProperties way to automatically map the profile of the entity fields, fields need to provide a set method can. The need to provide set methods when using @Value comment.

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/willncy/p/11877200.html