Hot deployment, configuration file Usage
first, hot load
Spring provides developers with a module called Spring-boot-devtools to enable the spring boot application to support hot deployment and increase developer productivity without having to manually restart the spring boot application.
the principle of Devtools
The underlying principle is to use two ClassLoader, one ClassLoader to load the classes that will not change (third-party jar packages), and another ClassLoader to load the class that will change, Called Restart ClassLoader, so that when there is code change, the original restart ClassLoader is discarded, re-create a restart ClassLoader, due to the need to load fewer classes, so a faster restart time.
Official address: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools
To implement a hot deployment, first introduce:Spring-boot-devtools.jar Package
Core Dependency Packages:
<Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-devtools</Artifactid> <Optional>True</Optional> </Dependency>
After adding a dependency, restart the app inside the IDE, and the subsequent changes will take effect immediately
Files that are not hot-deployed by default
1,/meta-inf/maven,/meta-inf/resources,/resources,/static,/public, or/ Templates
2. The specified file is not hot-deployed spring.devtools.restart.exclude=static/**,public/**
In development, we think of a problem?
If you write a logic code that requires several files, you can't do a hot deployment every time you save it, there's a workaround here.
Adding a manual trigger restart in application.properties
#指定某些文件不进行监听, the hot load is not #spring.devtools.restart.exclude=application.properties# use triggers to control when hot loads are deployed to deploy new files spring.devtools.restart.trigger-file=trigger.txt
Then, under the Src\main\resources directory, add the trigger.txt file
version=1
So you change the code every time, not every time you save the hot deployment, but change the code, change version=2 will be a hot deployment.
Note : Production environment do not turn on this feature, if you start with Java-jar, Springboot is not hot deployment
ii. springboot annotations automatically map configuration files to attributes and entity classes actual combatmode one, controller above configuration
Introduction: Explain using @value annotation profiles to automatically map to attributes and entity classes
1. configuration file Loading
Way One
1, controller above configuration
@PropertySource ({"Classpath:resource.properties"})
2. Add attributes
@Value ("${test.name}")
private String name;
Example
The file upload address of the previous article I was written dead.
This is obviously unscientific, which is changed here to write in 1. In the application.properties configuration file.
#文件上传路径配置web.file.path=c:/users/chenww/desktop/springbootstudy/springbootstudy/src/main/resources/ static/images/
2 in the Filecontroller class
@Controller@PropertySource({"classpath:application.properties"}) Public Class Filecontroller { // file placed under project images // Private String FilePath = "c:\\users\\chenww\\desktop\\springbootstudy\\springbootstudy\\src\\main\\resources \\static\\images\\ "; @Value ("${web.file.path}") Private String FilePath;
Summarize:
1: Which file is read by@PropertySource representative
2:@Value get value by key
mode two: Entity class configuration file
Steps:
1, add @Component annotations;
2. Specify the location of the configuration file using @PropertySource annotations;
3, the use of @ConfigurationProperties annotations, set the relevant properties;
4. The obtained profile value must be resource in the class by injecting an IOC object into it.
@Autowired
Private Serversettings serversettings;
Case:
1. In Application.properties
#测试配置文件路径Test.domain=www.jincou.comtest.name=springboot
2. Create a serversettings entity
ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.PropertySource;Importorg.springframework.stereotype.Component;//Server Configuration@Component @propertysource ({"Classpath:application.properties"}) @ConfigurationProperties Public classserversettings {//name Test.domain is a key value@Value ("${test.domain}") PrivateString name; //Domain Address@Value ("${test.name}") PrivateString domain; //provide set and Get methods }
Three Create Getcontroller
Importcom.jincou.model.ServerSettings;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classGetcontroller {//need to inject@AutowiredPrivateserversettings serversettings; @GetMapping ("/v1/test_properties") PublicObject testperoperties () {returnserversettings; }}
Page effects
In fact, the above can also be an optimization:
Create a serversettings entity
Importorg.springframework.boot.context.properties.ConfigurationProperties;ImportOrg.springframework.context.annotation.PropertySource;Importorg.springframework.stereotype.Component;//Server Configuration@Component @propertysource ({"Classpath:application.properties"}) @ConfigurationProperties (prefix= "Test")//This adds a test prefix. Public classserversettings {//This is not required to write Vlaue tags, as long as text.name remove the prefix test after the name and here name is the same, will automatically assign the value PrivateString name; //Domain Address PrivateString domain; //provide set and Get methods }
Think too much, do too little, the middle of the gap is trouble. Want to have no trouble, either don't think, or do more. Captain "6"
Springboot (4)---Hot deployment, configuration file Usage