自己定义的一个listener 在web.xml配置 为神马显示NameNotFoundException呢?

duoduo3_69 发布于 2013/05/19 13:54
阅读 2K+
收藏 1

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


	<!-- 对Spring容器进行实例化 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>


	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<!-- struts2容器控制器 -->
	    
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	 
	<session-config>
		<session-timeout>10</session-timeout>
	</session-config>
	<listener>
		<listener-class>com.yjj.sk.listener.UserLoginListener</listener-class>
	</listener>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


UserLoginListener.java

package com.yjj.sk.listener;

import javax.annotation.Resource;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.yjj.sk.auth.model.Uuser;
import com.yjj.sk.auth.service.UserService;

public class UserLoginListener implements HttpSessionListener {
	@Resource
	private UserService userService;
	private Uuser user;

	public void sessionCreated(HttpSessionEvent arg0) {

	}

	public void sessionDestroyed(HttpSessionEvent se) {
		user = (Uuser)se.getSession().getAttribute("user");
		if(user!=null){
			System.out.println(userService);
		}
	}

}
异常

严重: Error configuring application listener of class com.yjj.sk.listener.UserLoginListener
javax.naming.NameNotFoundException: Name com.yjj.sk.listener.UserLoginListener is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)


加载中
0
duoduo3_69
duoduo3_69
发现一个帖子 http://stackoverflow.com/questions/4602710/custom-sessionlistener-name-is-not-bound-in-this-context-javax-naming-namenotf 这个哥们的问题和我一样,貌似是不能在这里使用注解 ,没办法只能把这里就不报错了,很奇怪。
//原来
@Resource
private UserService userService;


//改成
//@Resource
private UserService userService = new UserServiceBean();
0
excepiton
excepiton

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

userService = ctx.getBean(UserServiceBean.class);


0
墨鱼ID
墨鱼ID
要用Spring注入,起码要先把类注册到Spring容器先。filter是放在web.xml里面和spring容器没有关系,如楼楼上,先在Servlet的上下文中找到Spring的上下文,再通过Spring获取类的实例
0
jingcoco
jingcoco
我看了楼主说的stackflow网站,那里面题主解决了,贴了一个链接:http://www.mkyong.com/spring/spring-how-to-do-dependency-injection-in-your-session-listener/
返回顶部
顶部