JDK1.5.0_22 + Tomcat 6.0.39 + Srping 3.0.0
NameBean:
public class NameBean {
public String introduce(){
return "Just for test!!!";
}
}
nameBean.xml:
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="nameBean" class="com.sean.NameBean"/>
</beans>
OutputBean:
public class OutputBean {
private NameBean nameBean;
public String output(){
return nameBean.introduce();
}
public void setNameBean(NameBean nameBean) {
this.nameBean = nameBean;
}
}
outputBean.xml:
<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="outputBean" class="com.sean.OutputBean">
<property name="nameBean">
<ref bean="nameBean"/>
</property>
</bea
web.xml:
1,如果log4j的配置文件存放在src目录下,则不需要使用log4jConfigLocation参数指定其位置,也不需要配置Log4jConfigListener监听从特定位置加载log4j配置文件
2,特别需要注意的是Log4jConfigListener一定在配在ContextLoaderListener前面
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:outputBean.xml;
classpath:nameBean.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
log4j.properties:
log4j.rootLogger=info,default
log4j.appender.default=org.apache.log4j.ConsoleAppender
log4j.appender.default.layout=org.apache.log4j.PatternLayout
log4j.appender.default.layout.ConversionPattern=%d{[MM-dd HH:mm:ss]} -> %m%n
test.jsp:
WebApplicationContext对象将作为属性放置在ServletContext中,以便Web应用环境可以访问Spring应用上下文
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="org.springframework.web.context.*,com.sean.OutputBean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
<%
//ServletContext可以获取到WebApplicationContext
WebApplicationContext wac = (WebApplicationContext)
application.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
OutputBean output = (OutputBean)wac.getBean("outputBean");
out.write(output.output());
//WebApplicationContext也可以获取到ServletContext
application.setAttribute("Attribute","Attribute");
out.write(wac.getServletContext().getAttribute("Attribute").toString());
%>
</body>
</html>
运行结果如下: