简体   繁体   English

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

[英]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: 我正在尝试实现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());
 }
}

When user implement ISessionEvent and add as a bean, SessionCreated and SessionDestroyed functions of implementation will be called when these events occured. 当用户实现ISessionEvent并作为bean添加时,这些事件发生时将调用实现的SessionCreated和SessionDestroyed函数。 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. 解决方案:使用链接工作中使用的方法。

It's merely because ISessionEvent is not managed, I believe. 我相信,这仅仅是因为ISessionEvent未被管理。 Does it have @Resource annotation? 是否具有@Resource批注? 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: 我没有关于带注释的侦听器和Co.的经验,但是,我检查了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

Inject resources in specified field. 在指定字段中注入资源。 Throws: javax.naming.NamingException java.lang.IllegalAccessException 抛出:javax.naming.NamingException java.lang.IllegalAccessException

that means, you try to inject a resource into your field which must be a JNDI resource. 这意味着,您尝试将一种资源注入到您的字段中,该资源必须是JNDI资源。

@Resource annotation works with a JNDI- entry: http://download.oracle.com/javaee/5/api/javax/annotation/Resource.html @Resource注释与JNDI条目一起使用: http : //download.oracle.com/javaee/5/api/javax/annotation/Resource.html

did you forget to define your resources in JNDI Context? 您忘记在JNDI Context中定义资源了吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.naming.NameNotFoundException:名称未绑定在此Context中。无法找到 - javax.naming.NameNotFoundException: Name is not bound in this Context. Unable to find javax.naming.NameNotFoundException:名称[org.wso2.carbon.custom.ejb-1.0-SNAPSHOT / HelloBean]在此上下文中未绑定。 找不到[ejb:] - javax.naming.NameNotFoundException:Name [org.wso2.carbon.custom.ejb-1.0-SNAPSHOT/HelloBean] is not bound in this Context. Unable to find [ejb:] javax.naming.NameNotFoundException:名称[jdbc / spitterDS]未绑定在此Context中。找不到[jdbc] - javax.naming.NameNotFoundException: Name [jdbc/spitterDS] is not bound in this Context. Unable to find [jdbc] javax.naming.NameNotFoundException:名称[comp / env]在此上下文中未绑定。 在Tomcat中找不到[comp] - javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp] in Tomcat javax.naming.NameNotFoundException:名称[jdbc / rhwebDB]未绑定在此Context中。找不到[jdbc] - javax.naming.NameNotFoundException: Name [jdbc/rhwebDB] is not bound in this Context. Unable to find [jdbc] 名称[ejb:..]未在此上下文中绑定。 无法在Wildfly中找到[ejb:]。]的根本原因javax.naming.NameNotFoundException - Name [ejb:..] is not bound in this Context. Unable to find [ejb: ].] with root cause javax.naming.NameNotFoundException in Wildfly @Named批注无法在Servlet中加载Bean-javax.naming.NameNotFoundException:此上下文中未绑定名称学生 - @Named annotation can't load a bean in a Servlet - javax.naming.NameNotFoundException: Name student is not bound in this Context javax.naming.NameNotFoundException:名称[jdbc / skynetdb]未绑定在此Context中。 找不到[jdbc] - javax.naming.NameNotFoundException: Name [jdbc/skynetdb] is not bound in this Context. Unable to find [jdbc] javax.naming.NameNotFoundException:名称[SessionFactory]未绑定在此Context中。 无法找到[SessionFactory] - javax.naming.NameNotFoundException: Name [SessionFactory] is not bound in this Context. Unable to find [SessionFactory] javax.naming.NameNotFoundException:名称 [comp/env] 未绑定在此上下文中。 无法找到 java 调度程序的 [comp] 错误 - javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp] error with java scheduler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM