javax.naming.NamingException: Cannot create resource instance error modification

//The following content is obtained from the Internet

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: The package name of this class is not added to the Spring Scan scanner, but the annotation is used to obtain the service instance from the Spring container (the @Resource annotation wants to inject the instance from the Spring container, sorry, Spring did not give it to you. resource allocation)

2: Add the package to the scanner in Spring. Without adding the (@Component) annotation to the class body, I want to use @Resource to take out the service instance. I'm sorry that Spring didn't find the class mentioned at all.

3: If you are too lazy to configure these configuration files, I dare to say that you are an excellent programmer - lazy, there are solutions, like in the class

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

What to scan and what to assemble is a cloud. We can go to the ApplicationContext context in the context object to get it from the Spring container that owns the resource. If we take out the service, this problem can also be solved.

 

 

//The following content is the reason for my modification of the problem:

In the Tomcat configuration file service.xml

<Resource name="jdbc/mqService" 

The above names are repeated, which leads to the above problems. Comment out the unused ones, or delete them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325705895&siteId=291194637