spring2.0里怎么指定org.springframework.web.context.request.RequestContextListener

fxbird 2010-06-30 03:11:48
按照书上的说法,我tomcat是6.0,支持web-app2.4,按照如下方式配置web.xml应该是可以的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
</web-app>



applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="forumDao"
class="com.baobaotao.dao.jdbc.ForumJdbcDao" >
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>

<bean id="postDao" class="com.baobaotao.dao.jdbc.PostJdbcDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<bean id="topicDao" class="com.baobaotao.dao.jdbc.TopicJdbcDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<bean id="template" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>

<bean id="forumService1" class="com.baobaotao.service.ForumService1">
<property name="forumDao" ref="forumDao"/>
<property name="template" ref="template"/>
</bean>

<tx:annotation-driven transaction-manager="txManager"/>
<!-- a PlatformTransactionManager is still required -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="userService" class="com.baobaotao.service.UserService"/>
<bean id="forumService" class="com.baobaotao.service.ForumService">
<property name="userService" ref="userService"/>
</bean>

<bean id="bbtForum" class="com.baobaotao.service.impl.BbtForumImpl" autowire="byName">
</bean>
</beans>


test.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:directive.page import="com.baobaotao.service.BbtForum"/>
<jsp:directive.page import="org.springframework.context.ApplicationContext"/>
<jsp:directive.page import="org.springframework.web.context.support.WebApplicationContextUtils"/>
<jsp:directive.page import="com.baobaotao.domain.Topic"/>
<jsp:directive.page import="com.baobaotao.domain.Post"/>
<%
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
BbtForum bbtForum = (BbtForum)ctx.getBean("bbtForum");//这行获得了null
System.out.println("begin........");
Topic topic = new Topic();
topic.setTopicTitle("Title:jta-tomcat");
Post post = new Post();
post.setPostText("post content:jta-tomcat");
topic.setPost(post);
bbtForum.addTopic(topic);
System.out.println("end........");
System.out.println(ctx.getBean("bbtForum")==ctx.getBean("bbtForum"));
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

访问test.jsp的时候就抛出空指针异常了,如果用传统的org.springframework.web.context.ContextLoaderListener则运行正常。
...全文
283 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzc711 2010-06-30
  • 打赏
  • 举报
回复
RequestContextListener与ContextLoaderListener好像有点区别吧,至少对于作用域上是不同的,在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为request、session和global session,你去查一下Spring官方手册,好像里面是这样写的:当使用了Spring's DispatcherServlet以外的Servlet 2.4及以上的Web容器时(如使用JSF或Struts),你需要在Web应用的'web.xml'文件中增加 javax.servlet.ServletRequestListener 定义

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧