
简介
在创建易于部署的WEB应用程序项目的文章中,我们介绍了如何重建WEB项目以使项目WAR程序包变为无状态,这为项目部署和升级带来了极大的便利:
1)首先,由JVM系统参数指定项目配置文件,并且将项目部署文件移出WAR包,因此项目WAR包仅包含程序,不包含配置信息;
2)此外,它还介绍了如何通过Maven项目中的jetty插件提供JNDI数据源,以及如何将数据源配置移出Spring配置文件.
我们提供的解决方案不仅考虑了生产环境项目的可部署性,而且还考虑了项目开发环境的可移植性. 两者自然是统一的,可以真正转移到“最佳项目实践”中
![]()
但是我们留下了一个我们尚未考虑的重要问题: 如何将项目的日志配置文件(即log4j配置文件)移出项目?因为在生产环境中,当项目运行时,我们经常需要控制项目的日志行为: 例如日志级别,日志文件大小等.
如何配置传统的Log4J?

使用Spring框架的Web项目通常在org.springframework.web.util.Log4jConfigListener在web.xml中通过Log4j配置,并通过
log4jConfigLocation上下文参数指定log4J配置文件的地址,如下所示:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>600000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
尽管您可以指定一个独立于WAR的外部log4j配置文件(例如f: /conf/log4j.properties),但这限制了生产环境中log4j配置文件路径的自由度. 另外,log4j配置文件独立于Web项目,并且开发环境很麻烦.
我们期望的解决方案是统一开发环境和生产环境的便利性,因此我们希望通过项目配置文件的属性来定义log4j配置文件. 如果是这样,在开发环境中,项目配置文件的属性指向项目中的log4j配置文件,而在生产环境中,它指向外部的log4j配置文件.
最佳做法
为了通过项目配置文件的属性指定log4j配置文件,我们需要覆盖org.springframework.beans.factory.config.PropertiesFactoryBean类,以便在加载属性文件时进行log4j配置该属性值将加载文件以初始化Log4J日志引擎.
报价

您为什么要寻找PropertiesFactoryBean?因为我们希望尽早初始化log4j引擎,并且在启动Spring时,首先要加载项目配置文件,所以自然而然地选择这趟快速火车
![]()
![]()
完成任务的人
看看我们如何覆盖PropertiesFactoryBean类:
package com.xxx.sys;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.util.Assert;
import org.springframework.util.Log4jConfigurer;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.Properties;
/**
* 加载平台属性文件,同时初始化Log4J引擎
* @author : chenxh
* @date: 2015/7/28
*/
public class FrameworkPropertiesFactoryBean extends PropertiesFactoryBean {
private static final String LOG4J_CONF_LOCATION = "log4j.conf.location";
private static final int REFRESH_INTERVAL_INSECONDS = 5;
public static final String REFRESH_INTERVAL = "log4j.conf.refreshInterval";
@Override
protected Properties createProperties() throws IOException {
Properties properties = super.createProperties();
//①通过项目配置文件初始化Log4J引擎配置
initLog4j(properties);
return properties;
}
private void initLog4j(Properties properties) throws IOException{
String confLocation = properties.getProperty(LOG4J_CONF_LOCATION);
Assert.hasText(confLocation,"配置文件未定义:"+LOG4J_CONF_LOCATION);
String refreshInterval = properties.getProperty(REFRESH_INTERVAL);
long refreshIntervalLong = REFRESH_INTERVAL_INSECONDS;
if (StringUtils.hasText(refreshInterval)) {
int value = Integer.parseInt(refreshInterval);
if (value < 2) {
value = 2;
}
refreshIntervalLong = Integer.parseInt(refreshInterval)*1000L;
}
//②初始化Log4J引擎的配置
Log4jConfigurer.initLogging(confLocation,refreshIntervalLong);
}
}
使用Spring的org.springframework.util.Log4jConfigurer可以轻松地初始化Log4J引擎,因为这里我们重写了PropertiesFactoryBean的CreateProperties()方法,并且在加载了项目配置文件之后,立即初始化了Log4J引擎.
安装

在编写FrameworkPropertiesFactoryBean之后,我们必须在Spring配置文件中替换项目属性文件的初始内容:
以下代码段:
<bean id="frameworkConf"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>#{systemProperties.FRAMEWORK_CONFFILE_PATH}</value>
<value>classpath*:conf/frameworkVersion.properties</value>
</list>
</property>
</bean>
替换为:
<bean id="frameworkConf"
class="com.hsit.tso.framework.web.sys.FrameworkPropertiesFactoryBean">
<property name="locations">
<list>
<value>#{systemProperties.FRAMEWORK_CONFFILE_PATH}</value>
<value>classpath*:conf/frameworkVersion.properties</value>
</list>
</property>
</bean>
通过这种方式,在初始化FrameworkPropertiesFactoryBean时,将初始化Log4J引擎.
项目配置文件的相关属性
我们定义log4j配置文件路径log4j配置文件,并通过项目配置文件的log4j.conf.location和log4j.conf.refreshInterval更新刷新周期. 在开发环境中,我们可以这样定义它:

#log4j的配置文件路径 #1)文件如果在类路径下,以classpath:为前缀,形如:classpath:conf/log4j.properties #2)文件如果在文件系统下,则采用文件绝对路径,形如 f:/log4j.properties,/etc/conf/log4j.properties #①这儿=>开发环境嘛,当然基于类路径了 log4j.conf.location=classpath:conf/log4j.properties #log4j.conf.location=f:/log4j.properties #日志配置文件变更扫描刷新周期,单位为秒 log4j.conf.refreshInterval=5
在生产环境中,我们可以这样定义它:
#log4j的配置文件路径 #1)文件如果在类路径下,以classpath:为前缀,形如:classpath:conf/log4j.properties #2)文件如果在文件系统下,则采用文件绝对路径,形如 f:/log4j.properties,/etc/conf/log4j.properties #①这儿=>生产环境哦,基于文件系统的路径了 log4j.conf.location=f:/log4j.properties #log4j.conf.location=f:/log4j.properties #日志配置文件变更扫描刷新周期,单位为秒 log4j.conf.refreshInterval=5
摘要
为方便起见,在生产环境中,建议将Log4J配置文件log4j.properties和项目配置文件放在同一目录中,这样,当您要更改项目的配置时,只需要在此目录中找到配置文件更改. 当然,在开发环境中,最好同时管理两者. 让我们看一下我的项目目录的结构:

这时,我们使WEB项目完全无状态: 项目属性文件,数据源和log4J配置文件都是外部化管理. 项目实施人员的升级仅大胆地更新了WAR软件包log4j配置文件,很难笑.
![]()
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-267439-1.html
既然美国不承认十二海里只承认三海里拍几艘核潜艇到美国三海里以外溜达溜达看看美国什么反应