背景
很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境下配置用不同的配置文件或者不同的配置。
spring boot 提供
spring boot允许你通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件,然后通过在application.properyies通过spring.profiles.active来具体激活一个或者多个配置文件,如果没有没有指定任何profile的配置文件的话,spring boot默认会启动application-default.properties。
profile的配置文件可以按照application.properyies的放置位置一样,放于以下四个位置:
- 当前目录的 “/config”的子目录下
- 当前目录下
- classpath根目录的“/config”包下
- classpath的根目录下
demo 演示
#模拟测试变量 curvar=sit.curvar
和
#模拟生产变量 curvar=prd.curvar

我们在application.properyies也写上,并把profile切换到application-sit.properties的配置文件
#修改tomcat的默认的端口号,将8080改为8889 server.port=8889 #启用shutdown endpoint的HTTP访问 endpoints.shutdown.enabled=true #不需要用户名密码验证 endpoints.shutdown.sensitive=false #默认curvar值 curvar=default.curvar #切换配置文件 spring.profiles.active=sit
可以通过这样子来测试
package HelloWord; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") public class TaskController {
@RequestMapping(value = {
"/",""}) public String hellTask(@Value("${curvar}")String cusvar ){
return "hello task !! my current variable is " + cusvar; } }
在这里可以看到spring.profiles.active激活的profile不同,打印出来的结果也不一样。
除了可以用profile的配置文件来分区配置我们的环境变量,在代码里,我们还可以直接用@Profile注解来进行配置,例如数据库配置,这里我们先定义一个接口
public interface DBConnector {
public void configure(); }
分别定义俩个实现类来实现它
package config; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; / * 测试数据库 */ @Component @Profile("sitdb") public class SitDBConnector implements DBConnector {
@Override public void configure() {
System.out.println("sit-db"); } }
package config; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; / * 生产数据库 */ @Component @Profile("prddb") public class PrdDBConnector implements DBConnector {
@Override public void configure() {
System.out.println("prd-db"); } }
通过在配置文件激活具体使用哪个实现类
#修改tomcat的默认的端口号,将8080改为8889 server.port=8889 #启用shutdown endpoint的HTTP访问 endpoints.shutdown.enabled=true #不需要用户名密码验证 endpoints.shutdown.sensitive=false #默认curvar值 curvar=default.curvar #切换配置文件 #修改tomcat的默认的端口号,将8080改为8889 server.port=8889 #启用shutdown endpoint的HTTP访问 endpoints.shutdown.enabled=true #不需要用户名密码验证 endpoints.shutdown.sensitive=false #默认curvar值 curvar=default.curvar #切换配置文件 spring.profiles.active=sitdb
然后就可以这么使用
package HelloWord; import config.DBConnector; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/task") @ComponentScan(basePackages={
"config"}) public class TaskController {
@Autowired DBConnector connector; @RequestMapping(value = {
"/",""}) public String hellTask(@Value("${curvar}")String cusvar ){
connector.configure();//最终打印配置的db return "hello task !! my current variable is " + cusvar; } }

#修改tomcat的默认的端口号,将8080改为8889 server.port=8889 #启用shutdown endpoint的HTTP访问 endpoints.shutdown.enabled=true #不需要用户名密码验证 endpoints.shutdown.sensitive=false #默认curvar值 curvar=default.curvar #切换配置文件 spring.profiles.include: prd,prddb
以上就是spring boot用profile的作用。
以下为可选(主要是命令行使用):
通过命令行设置属性值
相信使用过一段时间Spring Boot的用户,一定知道这条命令:java -jar xxx.jar –server.port=8888,通过使用–server.port属性来设置xxx.jar应用的端口为8888。
在命令行运行时,连续的两个减号–就是对application.properties中的属性值进行赋值的标识。所以,java -jar xxx.jar –server.port=8888命令,等价于我们在application.properties中添加属性server.port=8888,该设置在样例工程中可见,读者可通过删除该值或使用命令行来设置该值来验证。
通过命令行来修改属性值固然提供了不错的便利性,但是通过命令行就能更改应用运行的参数,那岂不是很不安全?是的,所以Spring Boot也贴心的提供了屏蔽命令行访问属性的设置,只需要这句设置就能屏蔽:SpringApplication.setAddCommandLineProperties(false)。
多环境配置
针对各环境新建不同的配置文件application-dev.properties、application-test.properties、application-prod.properties 在这三个文件均都设置不同的server.port属性,如:dev环境设置为8080,test环境设置为9090,prod环境设置为80 application.properties中设置spring.profiles.active=dev,就是说默认以dev环境设置
执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为9090,也就是测试环境的配置(test) 执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为80,也就是生产环境的配置(prod)
多环境高级应用
/ * 发送邮件接口. */ public interface EmailService {
/发送邮件*/ publicvoid send(); } 发送邮件的具体实现(dev-开发环境的代码): @Service @Profile("dev") //开发环境的时候. public class DevEmailServiceImpl implements EmailService{
@Override publicvoid send() {
System.out.println("DevEmailServiceImpl.send().开发环境不执行邮件的发送."); } }
发送邮件的具体实现(prod-生产环境的代码):
@Service @Profile("prod") //生产环境. public class ProdEmailServiceImpl2 implements EmailService{
@Override publicvoid send() {
System.out.println("DevEmailServiceImpl.send().生产环境执行邮件的发送."); //具体的邮件发送代码. //mail.send(); } }
Profile(“dev”)表明只有Spring定义的Profile为dev时才会实例化DevEmailService这个类。那么如何设置Profile呢?
在application.properties中加入:
spring.profiles.active=dev
通过命令行参数
java -jar app.jar --spring.profiles.active=dev
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/198665.html原文链接:https://javaforall.net
