javax.naming.NamingException: Cannot create resource instance at

javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:115)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:842)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.NamingContext.lookup(NamingContext.java:830)
at org.apache.naming.NamingContext.lookup(NamingContext.java:167)
at org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource(DefaultInstanceManager.java:582)
at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:472)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:159)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:140)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4888)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase StartChild.call(ContainerBase.java:1559)atorg.apache.catalina.core.ContainerBase StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
“`
##原因:

package com.vrv.cems.listeners;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.sys.common.util.DateUtils;
import com.sys.common.util.UUIDUtils;
import com.vrv.cems.log.domain.ManagerLoginLog;
import com.vrv.cems.log.service.ManagerLoginLogService;
import com.vrv.cems.mgr.auth.http.ManagerSessionUtils;
import com.vrv.cems.mgr.auth.session.ManagerSession;
import com.vrv.cems.mgr.domain.Manager;
import com.vrv.cems.mgr.service.ManagerService;
@Component
public class LoginSessionListener implements HttpSessionListener {
//登出
private final int EXIT = 2;
//非正常退出
private final int ABNORMALEXIT = 3;
private ManagerService managerService;
public ManagerService getManagerService() {
return managerService;
}
public void setManagerService(ManagerService managerService) {
this.managerService = managerService;
}
@Override
public void sessionCreated(HttpSessionEvent se) {
// TODO Auto-generated method stub

}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
    // TODO Auto-generated method stub
    HttpSession session = se.getSession();
     ManagerSession managerSession = ManagerSessionUtils.getManagerSession(session);
     if(managerSession == null){
         //正常退出
     }else{
         //非正常退出
         ApplicationContext context = (ApplicationContext)se.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
         ManagerLoginLogService managerLoginLogService = context.getBean("managerLoginLogService",ManagerLoginLogService.class);
         /*ManagerService managerService=context.getBean("managerService",ManagerService.class);*/
         String uuid = UUIDUtils.get32UUID();
         Manager manager= managerService.queryByAccount(managerSession.getAccount());
        /* Manager manager= managerService.queryByAccount(managerSession.getAccount());*/
         managerLoginLogService.save(new ManagerLoginLog(uuid,managerSession.getNiceName(),manager,EXIT,ABNORMALEXIT,DateUtils.getCurrentTimestamp(),managerSession.getLoginIp()));

     }
}

}

1:没有在Spring Scan扫描器中加入该类的包名,就是用注解,相从Spring容器中获取service实例(@Resource注解想从Spring容器中注入实例,不好意思,人家Spring更本没给你分配资源)

2:在Spring中扫描器加入该包,没有在该类体上加(@Component)注解,就想用@Resource取出service实例,不好意思人家Spring根本没找到说的这个类

3:如果你懒得配这些配置文件,我敢说你是一名出色的程序员-懒,解决方法也是有的,像类中这样

ApplicationContext context = (ApplicationContext)se.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
ManagerLoginLogService managerLoginLogService = context.getBean(“managerLoginLogService”,ManagerLoginLogService.class);

什么扫描什么装配,都是浮云,我们自己去上下文对象中 ApplicationContext context 去取拥有资源的Spring容器中取就行了 ,取出service了,这个问题也能解决

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值