本文介绍了定制SessionListener,名称没有在此背景下,javax.naming.NameNotFoundException绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现HttpSessionListener使这一监听器的用户可以注册实施ISessionEvent接口的会话活动code是如下:

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());
 }
}

当用户执行ISessionEvent并添加一个bean,实现SessionCreated和SessionDestroyed功能将被当这些事件发生调用。你可以问你为什么不只是写里面的侦听器的方法,我不我只是想。

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.

当我尝试code上面我得到了以下错误消息:

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注解导致错误,但我不能解决这个问题。

Resource annotation causes the error but i could not resolve it.

感谢所有...

接口和实现


@Resource
public interface ISessionEvent {

公共无效SessionCreated(HttpSession的会议);
 公共无效SessionDestroyed(HttpSession的会议);

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>

解决方案:使用的工作。

推荐答案

这是仅仅因为 ISessionEvent 不管理,我相信。是否有 @Resource 标注?喜欢的东西,

It's merely because ISessionEvent is not managed, I believe. Does it have @Resource annotation? Something like,

@Resource
public class ISessionEvent {...}

这篇关于定制SessionListener,名称没有在此背景下,javax.naming.NameNotFoundException绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 12:33