maven项目:在pom.xml文件中的dependencies标签前添加如下代码
<profiles>
<profile>
<id>devid>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
<properties>
<profileActive>devprofileActive>
properties>
profile>
<profile>
<id>proid>
<properties>
<profileActive>prodprofileActive>
properties>
profile>
profiles>
pom文件配置后可以在application配置文件中通过@profileActive@方式拿到profileActive的值
#区分springboot是线上还是线下环境
spring.profiles.active=@profileActive@
可以根据不同的环境设置不同的日志输出路径
<springProfile name="prod">
<property name="LOG_HOME" value="/opt/myapp/logs/mengmeng-admin"/>
springProfile>
<springProfile name="dev">
<property name="LOG_HOME" value="/opt/myapp/logs/mengmeng-admin-dev"/>
springProfile>
如果参照这个配置后启动报
Logback configuration error detected:
错误
请参考这篇博客解决 https://blog.csdn.net/qq_33430083/article/details/91360771
同springboot一样
<profiles>
<profile>
<id>devid>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
<properties>
<profileActive>devlopmentprofileActive>
properties>
profile>
<profile>
<id>proid>
<properties>
<profileActive>productionprofileActive>
properties>
profile>
profiles>
maven-war-plugin
插件,这样作是为了在编译时,web.xml文件能识别${profileActive}<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>2.1.1version>
<configuration>
<webResources>
<resource>
<directory>src/main/webappdirectory>
<filtering>truefiltering>
resource>
webResources>
configuration>
plugin>
<context-param>
<param-name>spring.profiles.defaultparam-name>
<param-value>${profileActive}param-value>
context-param>
通过监听器获取到web.xml中spring.profiles.default
的值,存入系统属性中
/**
* 获取项目开发环境监听器
*
* @author 段誉
* @create 2018-09-19 16:56
*/
public class XyyContextLoaderListener extends ContextLoaderListener {
@Override
public void contextInitialized(ServletContextEvent event) {
String attribute = event.getServletContext().getInitParameter("spring.profiles.default");
System.setProperty("DEV_MODEL", attribute);
super.contextInitialized(event);
}
}
在web.xml中添加监听器配置,在配置spring.profiles.default标签的后边添加
<listener>
<listener-class>com.weijian.xiaoyingying.listener.XyyContextLoaderListenerlistener-class>
listener>
<dependency>
<groupId>org.codehaus.janinogroupId>
<artifactId>janinoartifactId>
<version>3.0.6version>
dependency>
<dependency>
<groupId>org.codehaus.janinogroupId>
<artifactId>commons-compilerartifactId>
<version>3.0.8version>
dependency>
<if condition='property("DEV_MODEL").contains("production")'>
<then>
<property name="LOG_HOME" value="/opt/myapp/logs/mjsadmin-pro"/>
then>
<else>
<property name="LOG_HOME" value="/opt/myapp/logs/mjsadmin-dev"/>
else>
if>
ContextLoaderListener
监听器之前导致配置没有生效<dependency>
<groupId>org.logback-extensionsgroupId>
<artifactId>logback-ext-springartifactId>
<version>0.1.1version>
dependency>
<context-param>
<param-name>logbackConfigLocationparam-name>
<param-value>classpath:logback-aaa.xmlparam-value>
context-param>
/**
* 项目开发/生产环境监听器
*
* @author 段誉
* @create 2019-02-21 10:28
*/
public class ErpContextLoaderListener extends LogbackConfigListener {
@Override
public void contextInitialized(ServletContextEvent event) {
String attribute = event.getServletContext().getInitParameter("spring.profiles.default");
System.setProperty("DEV_MODEL", attribute);
super.contextInitialized(event);
}
}