自定义SessionListener,名称未绑定在此上下文中,javax.naming.NameNotFoundException

时间:2011-01-05 09:53:57

标签: java spring servlets dependency-injection annotations

我正在尝试实现HttpSessionListener,以便此侦听器的用户可以将ISessionEvent接口的实现注册到会话Events.code,如下所示:


public class MySessionListener implements HttpSessionListener{

 @Resource
 ISessionEvent sessionEvent;

 public ISessionEvent getSessionEvent() {
  return sessionEvent;
 }

 public void setSessionEvent(ISessionEvent sessionEvent) {
  this.sessionEvent = sessionEvent;
 }

 @Override
 public void sessionCreated(HttpSessionEvent arg0) {
  sessionEvent.SessionCreated(arg0.getSession());
 }

 @Override
 public void sessionDestroyed(HttpSessionEvent arg0) {
  sessionEvent.SessionDestroyed(arg0.getSession());
 }
}

当用户实现ISessionEvent并添加为bean时,将在发生这些事件时调用SessionCreated和SessionDestroyed实现函数。你可以问为什么你不只是写内部的听众方法,我不只是在尝试。

当我尝试上面的代码时,我收到以下错误消息:


javax.naming.NameNotFoundException: Name com.mehmet6parmak.sessionlistener.MySessionListener is not bound in this Context
 at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
 at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
 at org.apache.catalina.util.DefaultAnnotationProcessor.lookupFieldResource(DefaultAnnotationProcessor.java:278)
 at org.apache.catalina.util.DefaultAnnotationProcessor.processAnnotations(DefaultAnnotationProcessor.java:187)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4082)
 at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
 at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
 at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
 at org.apache.catalina.core.StandardService.start(StandardService.java:519)
 at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
 at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
 at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

资源注释导致错误但我无法解决它。

全部谢谢...

接口和实施


@Resource
public interface ISessionEvent {

public void SessionCreated(HttpSession session); public void SessionDestroyed(HttpSession session);

}



@Resource
public class SessionEvent implements ISessionEvent {

 @Override
 public void SessionDestroyed(HttpSession session) {
  System.out.println("From Session Event Callback(Destroy):" + session.getId());
 }

 @Override
 public void SessionCreated(HttpSession session) {
  System.out.println("From Session Event Callback(Create):" + session.getId());
 }

}


Bean定义



<context:annotation-config/>
<context:component-scan base-package="com.mehmet6parmak">
</context:component-scan>

<bean id="sessionEvent" autowire="byName" class="com.mehmet6parmak.sessionlistener.SessionEvent"></bean>

解决方案:使用link中使用的方法。

2 个答案:

答案 0 :(得分:1)

这仅仅是因为ISessionEvent未被管理,我相信。是否有@Resource注释?像,

@Resource
public class ISessionEvent {...}

答案 1 :(得分:0)

我没有关于注释听众和公司的经验,但是, 我检查了Specs的方法,我遇到了以下内容:

protected static void lookupFieldResource(javax.naming.Context context,
                                          java.lang.Object instance,
                                          java.lang.reflect.Field field,
                                          java.lang.String name,
                                          java.lang.Class<?> clazz)
                                   throws javax.naming.NamingException,
                                          java.lang.IllegalAccessException

在指定字段中注入资源。 抛出: javax.naming.NamingException中 java.lang.IllegalAccessException

这意味着,您尝试将资源注入您的字段,该字段必须是JNDI资源。

@Resource注释适用于JNDI条目: http://download.oracle.com/javaee/5/api/javax/annotation/Resource.html

您是否忘记在JNDI上下文中定义资源?

更新 资源注入文章: http://java.sun.com/developer/technicalArticles/J2EE/injection/