13 Spring Boot:
01.spring boot introduce && 02.spring boot entry
04.spring boot configuration files
SpringBoot basis
Four, SpringBoot profile
SpringBoot profile type and role
SpringBoot is based on the contract, so a lot of configuration have default values, but if you want to use your own configuration, then replace the default configuration, you can use
application.properties or application.yml ( application.yaml ) configuration.
application.properties
Port number #tomcat server server.port = 8888 # Name of the current web application server.servlet.context -path =
Which, application.properties file is a key file type, and have been using before, so here is not to properties file format to elaborate.
In addition to properties outside the file, SpringBoot may also be used yml file configuration, Hereinafter yml file to explain.
4.1.2 application.yml profile
yml Profile Profile
YML file format is YAML (YAML Aint Markup Language) to write the file format, YAML is an intuitive number can be recognized by the computer's according to data serialization format,
and easy to read human beings, and easy interactive scripting language, can be support YAML libraries in different programming languages program guide into,
such as: C / C ++, Ruby, Python, the Java, Perl, C #, PHP and so on. YML files are data-centric than the traditional xml more simple way clean .
YML file extensions can be used .yml or .yaml
Configuring normal data
Syntax: Key: value
name: haohao
Note: value has a space before
Configuration object data
person: name: haohao age: 31 addr: beijing #或者 person: {name: haohao,age: 31,addr: beijing}
Note: key1 number of spaces is not limited to the foregoing, in yml syntax, the same indentation level represent the same
Configuration array ( List , the Set ) data
Syntax:
Key:
- value1
- value2
or:
Key: [value1, value2]
City:
- Beijing
- Tianjin
- Shanghai
- Chongqing
# or
City: [Beijing, Tianjin, Shanghai, Chongqing]
# collection element is an object in the form of
Student:
- name: zhangsan
Age: 18 is
Score: 100
- name: Lisi
Age: 28
Score: 88
- name: wangwu
Age: 38 is
Score: 90
Note: value1 between the - there is a space between
4.1.3 SpringBoot configuration information inquiries
Mentioned above, SpringBoot configuration file, the main purpose is to modify the configuration information, but in the configuration key query go where
it? We can access SpringBoot official documents
# ----------------------------------------
# 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. Autodetected 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.
# JEST (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.
4.2 Profile configuration attribute mapping class manner
4.2.1 Use annotations @Value map
we can @Value configuration values mapping file annotation to a Spring managed Bean on the field
Controller Layer Entity Bean code is as follows:
@Controller public class QuickStartController { @Value("${person.name}") private String name; @Value("${person.age}") private Integer age; @RequestMapping("/quick") @ResponseBody public String quick(){ return "springboot 访问成功! name="+name+",age="+age; } }
4.2.2 Use annotations @ConfigurationProperties map
By annotation @ConfigurationProperties (prefix = " profile key prefix ") can be arranged in the configuration file is automatically mapped and the entity.
<!-- @ConfigurationProperties的执行器的配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
Entity Bean code is as follows:
@Controller @ConfigurationProperties(prefix = "person") public class QuickStartController { private String name; private Integer age; @RequestMapping("/quick") @ResponseBody public String quick(){ return "springboot 访问成功! name="+name+",age="+age; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } }
Note: Use @ConfigurationProperties mode can be automatically mapped configuration file and entity fields, but the fields need to be provided set methods can.
====================
end