Custom SessionListener, name is not bound in this context, javax.naming.NameNotFoundException
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(Htt开发者_运维百科pSession 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.
It's merely because ISessionEvent
is not managed, I believe. Does it have @Resource
annotation? Something like,
@Resource
public class ISessionEvent {...}
i have no experience about annotated listeners and Co. but, i checked the method on Specs, i met the following:
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
Inject resources in specified field. Throws: javax.naming.NamingException java.lang.IllegalAccessException
that means, you try to inject a resource into your field which must be a JNDI resource.
@Resource annotation works with a JNDI- entry: http://download.oracle.com/javaee/5/api/javax/annotation/Resource.html
did you forget to define your resources in JNDI Context?
Update Resource injection article: http://java.sun.com/developer/technicalArticles/J2EE/injection/
精彩评论