Custom SessionListener, name is not bound in this context, javax.naming.NameNotFoundException
- by mehmet6parmak
Hi,
I am trying to implement HttpSessionListener so that users of this listener can register implementation of ISessionEvent interface to session Events.code is below:
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());
}
}
When user implement ISessionEvent and add as a bean, SessionCreated and SessionDestroyed functions of implementation will be called when these events occured. You can ask why dont you just write inside listeners methods, i dont i'm just trying.
When i try the code above i got the following error message:
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 annotation causes the error but i could not resolve it.
Thanks All...
Interface and Implementation
@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 Definition
<context:annotation-config/>
<context:component-scan base-package="com.mehmet6parmak">
</context:component-scan>
<bean id="sessionEvent" autowire="byName" class="com.mehmet6parmak.sessionlistener.SessionEvent"></bean>
Solution:Using the method used in link works.