1: It is easy to read the configuration of the custom properties in the Application.properties, so there are not too many descriptions here:
Custom properties and loading
For example, define the following properties:
com.blog.title=spring Boot Tutorial
The corresponding configuration properties are then loaded by @Value ("${Property Name}") annotations, as follows:
@Component public
class Blogproperties {
@Value ("${com.blog.title}")
private String title;
Omit getter and Setter
}
references between Parameters
You can also use a direct reference between the various parameters in Application.properties, as in the following settings:
com.blog.title=spring boot tutorial
com.blog.desc=${com.didispace.blog.name} in continuous update
The Com.blog.desc parameter references the name and title properties defined above.
using random numbers
In some cases, some parameters we need to hope that it is not a fixed value, such as keys, service ports, etc. The Spring Boot property configuration file can be ${random} to produce an int, a long, or string string that supports the random value of the property.
# random string
Com.blog.value=${random.value}
Custom configuration Files
Create a new redis.properties configuration file under the Resources folder, as follows:
redis.ip=127.0.0.1
redis.port=6379
redis.maxactive=1024
redis.maxidle=200
redis.maxwait=10000
redis.testonborrow=true
redis.testonreturn=true
redis.maxtotal=600
redis.timebetweenevictionrunsmillis=30000
Also to prepare for the back integration of Redis;
Then create a new entity class to assign the value of the configuration file:
@Configuration
@PropertySource (value = "classpath:redis.properties")
@ConfigurationProperties (prefix = " Redis ") Public
class Redis {
private String IP;
Private Integer Port;
Private String password;
Private Integer maxactive;
Private Integer Maxidle;
Private Long maxwait;
Private Boolean Testonborrow;
Private Boolean Testonreturn;
Private Integer expire;
Private Integer maxtotal;
Omit Getter,setter
}
Create a new Read class:
/**
* @Package: Com.springboot.utils
* @ClassName: ReadProperties
* @Description: Read the custom configuration file
* @Author Shuyu.wang
* @Date 2017-12-07 12:18
**/
@EnableConfigurationProperties ({redis.class})
@Component Public
class ReadProperties {
@Autowired
private Redis Redis;
public void Read () {
System.out.println (Redis.getip ());
}
}
Then add the appropriate test interface to the Web class:
@Autowired
private readproperties readproperties;
@RequestMapping (value = "/read", method = requestmethod.get) public
String Read () {
readproperties.read ();
Return "over";
}
Then restart the project to access the Http://127.0.0.1:8082/boot/read
See the console Print IP:
Multiple Environment Profiles
In a real-world development environment, we need a different configuration environment, in the format application-{profile}.properties, where {profile} corresponds to your environment identity, such as: Application-test.properties: Test Environment Application-dev.properties: Development environment application-prod.properties: production environment
Modify the Application.properties as follows:
#根据环境读取配置文件
#application-test.properties: Test environment
#application-dev.properties: Development Environment
# Application-prod.properties: Production environment
Spring.profiles.active=dev
New Application-dev.properties and Application-prod.properties,
The configuration is as follows:
# Server settings (serverproperties)
#端口
server.port=8082
server.address=127.0.0.1
# server.sessiontimeout=30
#访问路径名称
server.contextpath=/boot
Configure a different port in two files, and then select which profile by changing the configuration of the application.properties. My local development environment is 8081 configured, and the deployment environment is 8082,application.properties you need to change the port number to access the service.