概述
在配置软件启动执行某段代码时(采用servlet方式),报了javax.naming.NamingException: Cannot create resource instance异常
javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:117)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.catalina.core.DefaultInstanceManager.lookupMethodResource(DefaultInstanceManager.java:625)
at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:471)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:162)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:143)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1120)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1678)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
因为少用了个代理器,加上代理器就好了。代理器如下:
Xml文件
<servlet>
<description></description>
<display-name>ServiceInfoServlet</display-name>
<servlet-name>serviceInfoServlet</servlet-name> 这里的名跟servlet类中的注解名一样
<servlet-class>com.xcfh.invoice.controller.ServiceInfoServletProxy</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
代理类:
public class ServiceInfoServletProxy extends GenericServlet{
private String targetBean;
private Servlet proxy;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
this.targetBean = getServletName();
getServletBean();
proxy.init(getServletConfig());
}
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
proxy.service(req, res);
}
private void getServletBean(){
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.proxy = (Servlet) wac.getBean(targetBean);
}
}
Servlet类
@Component("serviceInfoServlet")
public class ServiceInfoServlet extends HttpServlet{
private ServiceInfoSerivce serviceInfoService;
public ServiceInfoSerivce getServiceInfoService() {
return serviceInfoService;
}
@ Resource(name = "ServiceInfoSerivce")
public void setServiceInfoService(ServiceInfoSerivce serviceInfoService) {
this.serviceInfoService = serviceInfoService;
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
serviceInfoService.loadServiceInfo();
}
}
按照以上配置,正常启动
最后
以上就是专一小海豚为你收集整理的javax.naming.NamingException: Cannot create resource instance的全部内容,希望文章能够帮你解决javax.naming.NamingException: Cannot create resource instance所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复