javax.naming.NamingException: Cannot create resource instance报错修改

时间:2023-02-01 21:11:30

//下面内容为网上获取

avax.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.ContainerBaseStartChild.call(ContainerBase.java:1559)atorg.apache.catalina.core.ContainerBaseStartChild.call(ContainerBase.java:1559)atorg.apache.catalina.core.ContainerBaseStartChild.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了,这个问题也能解决

 

 

//下面内容为本人修改问题原因:

在Tomcat配置文件service.xml中

<Resource name="jdbc/mqService" 

上述name名称重复,导致以上问题的出现,注释掉不用的,或者删除即可