springboot允许我们通过命名约定按照一定的格式((application-{profile}.properties)来定义多个配置文件,然后在application.properties中通过属性spring.profiles.active来指定一个或多个配置文件,如果没有没有指定任何profile的配置文件的话,spring boot默认会启动application-default.properties。
application-{profile}.properties文件可以存放于一下四个位置:
- 当前目录的/config子目录下
- 当前目录下
- classpath根目录的/config子目录下
- classpath根目录下
下面贴出测试代码:
application.properties:
server.port=8081
server.servlet.context-path=/cn
name= 52pqq
##thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
##org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties已经有了相关的配置,可以修改默认配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
##开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
##thymeleaf end
##指定具体的配置文件 application-dev.properties
spring.profiles.active=dev
application-dev.properties:
server.port=8082
结果:通过8081端口访问已经不通了,端口已经重新绑定到8082上去了;
在这里可以看到spring.profiles.active属性指向了dev文件,application-dev.properties中的server.port属性覆盖了application.properties文件中的server.port属性;
一般用途:
(一)多环境配置:
对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot处理起来更加的简单;
在springboot中,多环境配置文件名需要满足application-{profile}.properties,其中profile对应的是我们的环境变量名称,如:
application-dev.properties
application-test.properties
application-prd.properties
我们通过修改application.properties中的spring.profiles.active来选择不同的配置文件;
(二)多环境高级配置:
在某些情况下,应用的某些业务逻辑可能有不同的实现,比如短信服务:假设MessageService中包含的send(String phone)方法向不同的手机号发送指定的短信,但是我们仅仅希望只有在生产环境才想用户发送消息,而开发环境和测试环境不发消息。我们可以借用Spring注解的@Profile实现这样的功能,这样需要定义两个实现MessageService接口的实现类。
MessageService.java:
package com.twopqq.springboot;
public interface MessageService {
/***
* @desc 发送短信
* @author fanzhen
* @date 2020-03-25 20:06
* @param phone
* @return void
*/
void send(String phone) throws Exception;
}
DevMessageServiceImpl.java:
package com.twopqq.springboot;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
/**
* @author fanzhen
* @desx
* @date 2020-03-25
*/
@Service
@Profile("dev")
public class DevMessaegServiceImpl implements MessageService {
@Override
public void send(String phone) throws Exception {
System.out.println("测试环境不发送信息");
}
}
PrdMessageServiceImpl.java:
package com.twopqq.springboot;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
/**
* @author fanzhen
* @desx
* @date 2020-03-25
*/
@Service
@Profile("prd")
public class PrdMessageServiceImpl implements MessageService {
@Override
public void send(String phone) throws Exception {
System.out.println("生产环境发送信息");
//具体的发送代码
//send();
}
}
demo1:
application.properties:
spring.profiles.active=dev
日志:
2020-03-25 20:12:49.043 INFO 3441 --- [nio-8082-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/cn] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-25 20:12:49.044 INFO 3441 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-03-25 20:12:49.056 INFO 3441 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 12 ms
测试环境不发送信息
demo2:
application.properties:
spring.profiles.active=prd
日志:
2020-03-25 20:17:00.125 INFO 3463 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/cn] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-25 20:17:00.125 INFO 3463 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-03-25 20:17:00.133 INFO 3463 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
生产环境发送信息