我正在尝试实现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中使用的方法。
最佳答案
我相信,这仅仅是因为ISessionEvent
没有得到管理。是否具有@Resource
批注?就像是,
@Resource
public class ISessionEvent {...}