contextlisteneroverride
public void contextInitialized(ServletContextEvent event) {
// TODO Auto-generated method stub
System.out.println("context has started");
super.contextInitialized(event);
System.out.println(context.class.getName());
// Logger logme=LogManager.getLogger();
// Logger logme1=LogManager.getLogger("context-fileappend");
// logme.info("This is context logger"+this.toString());
// logme1.info("This is fileappend"+this.toString());
// final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
// final Configuration config = ctx.getConfiguration();
// Appender x=
// config.
//dostuff();
Context initCtx;
try {
initCtx = new InitialContext();
Context springCtx = initCtx.createSubcontext("spring");
Object bean=WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()).getBean(createdatasrc.class);
System.out.println(event.getServletContext().getMajorVersion()+" "+event.getServletContext().getMinorVersion());
springCtx.bind("bean", bean);
//START LOG4J INITIALIZATION FROM HERE
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//BasicDataSource
}
WEB.XML
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/data.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<listener>
<listener-class>log4j.sessionmgr</listener-class>
</listener>
<listener>
<listener-class>log4j.context</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>
DATA.XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<bean id="data" class="log4j.createdatasrc"></bean>
</beans>
CREATEDATASRC.JAVA
public class createdatasrc extends BasicDataSource {
public static int count=0;
public createdatasrc()
{
count++;
this.setMaxIdle(10);
this.setMaxTotal(-1);
this.setUrl("jdbc:sqlserver://DESKTOP-5N44AE\\SQLEXPRESS:1433;databaseName=woah");
this.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.setUsername("sa");
this.setPassword("pass");
System.out.println("datasource created");
}
}
LOG4J.XML部分
<JDBC name="databaseAppender" tableName="logs">
<DataSource jndiName="spring/bean" />
<Column name="eventDate" isEventTimestamp="true" />
<Column name="level" pattern="%level" />
<Column name="logger" pattern="%logger" />
<Column name="message" pattern="%message" />
<Column name="exception" pattern="%ex{full}" />
</JDBC>
在上面的log4j.xml中,jdbc附加程序需要一个jndi名称作为数据源,问题是,在web.xml中可以看到,在applicationcontext初始化期间,数据源的创建将动态地发生,随后我将进行绑定servletcontext初始化期间,jndi名称与数据源对象一起使用。因此,从本质上讲,当log4j启动时,jndi名称是不受限制的,这导致找不到jndi名称的错误。问题:
是否有一种更简单的方法来执行此操作,而又不会更改数据源将由Spring动态初始化的事实?
在将上下文参数isLog4jAutoInitializationDisabled设置为true并在web.xml中提供Log4jServletContextListener时,log4j2仍然可以使用默认配置来启动,就像在tomcat控制台中一样,这是否是定义的行为?
我也曾尝试在applicationcontext初始化期间创建数据源,但是log4j2甚至在此之前就启动了,导致jndi查找失败,所以在使用参数isLog4jAutoInitializationDisabled时我会出错,以及如何手动启动初始化用代码处理吗?