The spring project will have more than one configuration file before the springboot appears. Like Web. XML, multiple spring-xxx.xml,xxx configured with spring represent a feature of spring, such as Aplication-datasource.xml,
Appliction-mvc.xml. The application itself also needs multiple profiles, and it needs to write code to read the parameters of these configuration files. Now Springboot simplifies the management and reading of spring configurations, requires only one application.properties and provides a variety of ways to read configuration files. Note (Springboot provides two configurations of properties and YML)
1, Springboot support two configuration methods: (Two of the difference between the configuration, please own Baidu)
- Application.properties
- Application.yml
2, springboot Common configuration:
A, yml format
#数据源配置spring: # Environment Dev|test|pro profiles: active:dev# Server configuration server: Tomcat: uri-encoding:utf-8 max-threads:1000 min-spare-threads:30 port:8080#mybatis configuration mybatis: mapper-locations:classpath* : Mybatis/*mapper.xml type-aliases-package:com.demo.entity
# jsp# mvc:# view:# prefix:/web-inf/# page default prefix directory # suffix:. JSP # Response page default suffix # Hot deployment Devtools: Restart: enabled:true #热部署生效 #设置重启的目录 additional-paths:src/main/java exclude:web-inf/ * * #classpath目录下的WEB-inf folder content modification does not restart
B, the Properties method
Mvcspring.mvc.async.request-timeout sets the timeout for the async request, in milliseconds, if not set, whichever is the specific implementation timeout, such as Tomcat's Servlet3 10 seconds. Spring.mvc.date-fo Rmat the format of the date, such as whether Dd/mm/yyyy.spring.mvc.favicon.enabled supports Favicon.ico, the default is: Truespring.mvc.ignore-default-model-on-redirect whether the default model content is ignored when redirecting, the default is Truespring.mvc.locale specifies the Locale.spring.mvc.message used -CODES-RESOLVER-FORMAT Specifies the format policy (prefix_error_code,postfix_error_code) for message codes. SPRING.MVC.VIEW.PREFIX Specifies the prefix of the MVC view. Spring.mvc.view.suffix Specifies the suffix of the MVC view. Messagesspring.messages.basename Specifies the basename of the message, multiple to tease If you do not add a package name, the default is to start with the Classpath path, by default: Messagesspring.messages.cache-seconds Set load resource file cache expiration time, 1 words never expires, default is -1spring.messages.encoding set message Bundles encoding, default: Utf-8mobilespring.mobile.devicedelegatingviewresolver.enable-fallback whether the fallback solution is supported, the default Falsespring.mobile.devicedelegat ingviewresolver.enabled If Device View resolver is started, the default is: Falsespring.mobile.devicedelegatingviewresolver.mobile-prefix sets the prefix for the mobile view by default: mobile/ Spring.mobile.devicedelegatingviewresolver.mobile-suffix setting the suffix SP for the mobile viewRing.mobile.devicedelegatingviewresolver.normal-prefix set the view prefix for a normal device spring.mobile.devicedelegatingviewresolver.normal-suffix Set the suffix of the normal device view Spring.mobile.devicedelegatingviewresolver.tablet-prefix set the tablet view prefix, default: tablet/ Spring.mobile.devicedelegatingviewresolver.tablet-suffix setting the tablet view suffix. spring.mobile.sitepreference.enabled whether sitepreference is enabled Handler, the default is: Trueviewspring.view.prefix sets the prefix for the MVC view. Spring.view.suffix sets the suffix of the MVC view. Resourcespring.resources.add-mappings whether to turn on default resource handling, default to Tru Espring.resources.cache-period sets the cache aging of the resource, in seconds. Spring.resources.chain.cache whether the cache is turned on, the default is: Truespring.resources.chain.enabled whether to open resources handling chain, The default is Falsespring.resources.chain.html-application-cache whether to open the H5 app's cache manifest override, by default: Falsespring.resources.chain.strategy.content.enabled whether the content versioning policy is turned on, the default is falsespring.resources.chain.strategy.content.paths specifies that you want to apply The version of the path, with multiple comma-separated, by default: [/**] spring.resources.chain.strategy.fixed.enabled If a fixed version policy is turned on, by default falsespring.resources.chain.strategy.fixed.paths specifies the path to which the version policy is to be applied. Multiple spring.resources.chain.strategy.fixed separated by commas. version specifies the version number used by the version policy spring.resources.static-locations specifies the static resource path, which defaults to classpath:[/meta-inf/resources/,/resources/,/ static/,/public/] and context:/ Multipartmultipart.enabled whether to turn on file upload support, the default is truemultipart.file-size-threshold to write to the disk threshold, in MB or KB, the default is 0multipart.location on the specified file Path. multipart.max-file-size Specifies the maximum file size, the default 1mbmultipart.max-request-size specifies the maximum value per request, which defaults to 10mbfreemarkerspring.freemarker.allow-r EQUEST-OVERRIDE Specifies whether the HttpServletRequest property can overwrite an item of the controller's model with the same name spring.freemarker.allow-session-override the specified HttpSession property is No can overwrite the controller's model with the same name Spring.freemarker.cache whether to open the template Caching.spring.freemarker.charset sets the encoding for the template. Spring.freemarker.check-template-location whether to check if templates path exists. Spring.freema Rker.content-type Set whether content-type.spring.freemarker.enabled allows MVC to use freemarker.spring.freemarker.expose-request-attributes settings All request properties are set in the merge to template, Whether you want to add them to the model. Spring.freemarker.expose-session-attributes sets all the properties of the httpsession to be added to the model when the merge to the template. SPRING.FREEMARKER.E Xpose-spring-macro-helpers set whether to SpringmacrorequeStcontext form exposes RequestContext to spring's macro The library uses spring.freemarker.prefer-file-system-access to load the template from the file system to support hot loading, default to Truespring.freemarker.prefix settings Freemarke The prefix of the R template. Spring.freemarker.request-context-attribute Specifies the name of the RequestContext property. Spring.freemarker.settings Settings Freemarker Keys.spring.freemarker.suffix sets the suffix of the template. Spring.freemarker.template-loader-path sets the load path for the template, with multiple comma-delimited, default: ["classpath:/ Templates/"] SPRING.FREEMARKER.VIEW-NAMES Specifies the list of views that use the template. Velocityspring.velocity.allow-request-override Specifies whether the properties of the HttpServletRequest can be overridden contro Ller model with the same name spring.velocity.allow-session-override specifies whether the HttpSession property can override the controller's model with the same name Spring.velocity.cache whether to open the template Cache Spring.velocity.charset Sets the template encoding spring.velocity.check-template-location whether to check if the template path exists. Spring.velocity.content-type Settings Contentt The value of ype Spring.velocity.date-tool-attribute sets the name spring.velocity.enabled of the Datetool that is exposed to the velocity context to allow MVC to be used Velocityspring.vel Ocity.expose-request-attributes whether the request attribute is added to the model when the merge template is in the Spring.velocity.expose-session-attributes, add the HttpSession property to the model Spring.velocity.expose-spring-macro-helpers set whether to expose RequestContext to SPR in the name of Springmacrorequestcontext ING ' s The Macro class library uses Spring.velocity.number-tool-attribute to set the name of the Numbertool that is exposed to the velocity context to spring.velocity.prefer-file-system-access precedence from The file system loads the template to support hot loading, The default is truespring.velocity.prefix to set the velocity template prefix. spring.velocity.properties sets the additional properties of the velocity. Spring.velocity.request-context-at Tribute set the name of the RequestContext attribute. Spring.velocity.resource-loader-path set the template path, the default is: classpath:/templates/ Spring.velocity.suffix sets the suffix of the velocity template. Spring.velocity.toolbox-config-location sets the path to the Velocity Toolbox configuration file, such as/ web-inf/ Toolbox.xml.spring.velocity.view-names sets the name of the view that needs to be resolved. Thymeleafspring.thymeleaf.cache whether to open the template cache, default Truespring.thymeleaf.check-te Mplate-location whether the template path is checked for existence, the default truespring.thymeleaf.content-type specifies Content-type, and the default is: text/ htmlspring.thymeleaf.enabled whether MVC is allowed to use Thymeleaf, the default is: truespring.thymeleaf.encoding Specifies the encoding of the template, by default: utf-8spring.thymeleaf.excluded-view-names Specifies a view name that does not use a template, and multiple comma-delimited. Spring.thymeleaf.mode Specifies the mode of the template, specificallyTo view standardtemplatemodehandlers, the default is: HTML5spring.thymeleaf.prefix Specifies the prefix for the template, which defaults to: classpath:/templates/ SPRING.THYMELEAF.SUFFIX Specifies the suffix of the template, which defaults to:. Htmlspring.thymeleaf.template-resolver-order Specifies the parse order of the template, which defaults to the first. Spring.thymeleaf.view-names Specifies the view name that uses the template, and multiple comma-delimited. Mustcachesprin G.mustache.cache whether the Enable template CACHING.SPRING.MUSTACHE.CHARSET Specifies the encoding of the template. Spring.mustache.check-template-location whether to check if the default path exists. spring.mustache.content -TYPE Specifies whether content-type.spring.mustache.enabled opens Mustcache template support. spring.mustache.prefix Specifies the prefix of the template, default: classpath:/ TEMPLATES/SPRING.MUSTACHE.SUFFIX Specifies the suffix of the template, default:. Htmlspring.mustache.view-names Specifies the view name to use for the template. Groovy template spring.groovy.template.allow-request-override Specifies whether HttpServletRequest properties can be With the same name of the model that overrides the controller spring.groovy.template.allow-session-override specifies whether the HttpSession property can overwrite the name of the controller's model Spring.groo Vy.template.cache whether to open the template cache. spring.groovy.template.charset Specifies the template encoding. Spring.groovy.template.check-template-location whether to check for templates Whether the path exists. Spring.groovy.template.configuration.auto-escape whether to automatically troubleshoot model variables when rendering templates, the default is: Falsespring.groovy.template.configuration.auto-indent whether the template is automatically indented when it is rendered, the default is Falsespring.groovy.template.configuration.auto-inden t-string if Auto indent is enabled, spaces or tab is used, the default is: Spacesspring.groovy.template.configuration.auto-new-line If you want to output line breaks when rendering a template, default to Falsespring.groovy.template.configuration.base-te Mplate-class Specifying template base Class.spring.groovy.template.configuration.cache-templates If you want to cache the template, default to Truespring.groovy.template.configuration.declaration -encoding the encoding spring.groovy.template.configuration.expand-empty-elements used when writing to the declaration header is used<BR/>This form, or<BR></BR>This expansion mode defaults to: false) Spring.groovy.template.configuration.locale Specify template locale.spring.groovy.template.configuration.new-line-string when auto wrap is enabled, the output of the newline is defaulted to the value of the system's Line.separator property Spring.groovy.template . configuration.resource-loader-path specifies the template path for groovy, which defaults to classpath:/templates/ SPRING.GROOVY.TEMPLATE.CONFIGURATION.USE-DOUBLE-QUOTES Specifies whether the property is to use double or single quotation marks, Falsespring.groovy.template.content-type is specified by default Conten T-type.spring.groovy.template.enabled whether to turn on groovy template support. Spring.groovy.template.expose-request-attributes setting all request properties in the merge To the template, Whether you want to add them to the model. Spring.groovy.template.expose-session-attributes sets all request properties to be added to the model when the merge is in the template. Spring.groovy.temp Late.expose-spring-macro-helpers set whether to expose RequestContext to spring's macro in the form of Springmacrorequestcontext The library uses Spring.groovy.template.prefix to specify the prefix for the template. Spring.groovy.template.request-context-attribute specifies the name of the RequestContext property. Spring . groovy.template.resource-loader-path specifies the path to the template, which defaults to: classpath:/templates/ Spring.groovy.template.suffix the suffix of the specified template spring.groovy.template.view-names specifies the template to useThe name of the view. Httpspring.hateoas.apply-to-primary-object-mapper sets whether the Hateoas is also supported for object Mapper, by default: Truespring.http.converters.preferred-json-mapper whether to use JSON first Mapper to convert. Spring.http.encoding.charset specify the HTTP request and the corresponding CharSet, default: Utf-8spring.http.encoding.enabled whether to turn on encoding support for HTTP, by default Truespring.http.encoding.force whether to force HTTP requests and responses to be encoded, default to Truejsonspring.jack Son.date-format Specify a date format, such as Yyyy-mm-dd HH:mm:ss, or the fully qualified name of the specific formatted class spring.jackson.deserialization whether to turn on the spring.jackson.generator of Jackson's deserialization whether to turn on JSON generators.spring.jackson.jo DA-DATE-TIME-FORMAT Specifies the format of the Joda date/time, such as Yyyy-mm-dd HH:mm:ss). If not Configured, DateFormat as Backupspring.jackson.locale Specifies whether the Locale.spring.jackson.mapper used by JSON will open the Jackson Generic feature. Spring.jackson.parser whether to turn on JA Parser characteristics of Ckson. Spring.jackson.property-naming-strategy Specify Propertynamingstrategy (camel_case_to_lower_case_with_ Underscores) Or specify the fully qualified class name of the Propertynamingstrategy subclass. Spring.jackson.serialization whether to turn on Jackson serialization. Spring.jackson.serialization-inclusion Specified sequence The inclusion method of the property, see the Jsoninclude.include enumeration. Spring.jackson.time-zone meansFixed date format time zone, such as America/los_angeles or gmt+10.jerseyspring.jersey.filter.order specify the order of the Jersey filter, by default: 0SPRING.JERSEY.INIT Specifies the initialization parameters that are passed to the jersey. SPRING.JERSEY.TYPE specifies the integration type of the jersey, either servlet or filter.
3. Read application Configuration
Springboot provides three ways to read the configuration file
- Enviroment class, can be read Key-value way to Application.properties
@Configuration public class MyProperty { //inject environment to get @Autowired through environment private environment environment; public void init () { Relaxedpropertyresolver propertyresolver = new Relaxedpropertyresolver (Environment, "jdbc."); String url = environment.getproperty ("url"); } }
public class MyProperty { //padding value by annotation value @Value ("${jdbc.url}") private String URL; }
- @ConfigurationProperties read the properties in the Application property configuration file.
@Getter @setter@configuration //Declaration of registering beans in the form of Java code
@PropertySource (value = {"classpath:application.properties"},encoding= "utf-8")//@ The Propertysource annotation is used to specify the directory, specifying the encoding to read the properties file @ConfigurationProperties (prefix = " Spring.datasource.sakila ")//the same configuration information is automatically encapsulated into the entity class public class Dbconfiginfo { private String URL; Private String username; private String password; Private String driverclassname; Private String initialsize; Private String Maxidle; Private String Minidle;}
4. Configuration file Priority
Config/application-default.properties > Config/application-default.yml >
Application-default.properties > Application-default.yml >
Config/application.properties > Config/application.yml >
Application.properties > Application.yml
Links: http://www.imooc.com/article/23670
Springboot Series (iv) Springboot configuration