SpringBoot项目
1.区分线上线下环境配置
maven项目:在pom.xml文件中的dependencies标签前添加如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> </profile> <profile> <id>pro</id> <properties> <profileActive>prod</profileActive> </properties> </profile> </profiles>
|
pom文件配置后可以在application配置文件中通过@profileActive@方式拿到profileActive的值
1 2
| spring.profiles.active=@profileActive@
|
2.在logback中通过如下方式进行区分环境
可以根据不同的环境设置不同的日志输出路径
1 2 3 4 5 6
| <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
SpringMVC项目
1.区分线上线下环境配置
同springboot一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>devlopment</profileActive> </properties> </profile> <profile> <id>pro</id> <properties> <profileActive>production</profileActive> </properties> </profile> </profiles>
|
2.在pom文件中引入maven-war-plugin插件,这样作是为了在编译时,web.xml文件能识别${profileActive}
1 2 3 4 5 6 7 8 9 10 11 12 13
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <webResources> <resource> <directory>src/main/webapp</directory> <filtering>true</filtering> </resource> </webResources> </configuration> </plugin>
|
3.在web.xml文件中修改spring.profiles.default配置,没有则添加
1 2 3 4 5
| <context-param> <param-name>spring.profiles.default</param-name> <param-value>${profileActive}</param-value> </context-param>
|
4.创建一个监听器,继承ContextLoaderListener
通过监听器获取到web.xml中spring.profiles.default的值,存入系统属性中
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
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标签的后边添加
1 2 3
| <listener> <listener-class>com.weijian.xiaoyingying.listener.XyyContextLoaderListener</listener-class> </listener>
|
5.引入logback使用if判断需要的依赖
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.0.6</version> </dependency>
<dependency> <groupId>org.codehaus.janino</groupId> <artifactId>commons-compiler</artifactId> <version>3.0.8</version> </dependency>
|
6.在logback中使用if判断区分环境
1 2 3 4 5 6 7 8
| <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>
|
配置后没有生效的原因
- 有些项目可能因为配置的原因导致logback初始化执行在了
ContextLoaderListener 监听器之前导致配置没有生效如果SpringMVC按照上述配置无法成功解决办法
1.引入logback与spring整合的依赖设置后可以自定义logback.xml的文件名/路径
1 2 3 4 5
| <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.1</version> </dependency>
|
2.将logback.xml文件的名字修改,比如叫logback-aaa.xml
3.在web.xml中配置logback配置文件初始化(注:要写在spring配置的上边)
1 2 3 4
| <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback-aaa.xml</param-value> </context-param>
|
4.将之前配置的监听器改成继承LogbackConfigListener,配置logback文件初始化的监听器
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
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); } }
|
作者: 只是学习学习
邮箱: fengzxia1000@163.com
原文地址: https://fengzxia.gitee.io/posts/5f5c7dc7.html
版权声明: 商业转载请联系作者获得授权,非商业转载请注明出处。