问题描述
- 看异常,用配置没问题。但是注解会报错,求解答!!!
-
严重: Error configuring application listener of class ibatis.apache.org.util.ApplicationListenerjavax.naming.NamingException: Cannot create resource instance at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:115) at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304) 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:559) at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:449) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:135) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:116) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4919) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517) 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:652) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1263) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1948) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) 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:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:662)
public class ApplicationListener implements ServletContextListener { @Resource private NoteTableDAO noteTableDAO; @Resource private NoteOperDAO noteOperDAO; @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub this.noteTableDAO = null; this.noteOperDAO = null; } @Override public void contextInitialized(ServletContextEvent servletcontextevent) { // TODO Auto-generated method stub //获取ServletContext对象 ServletContext servletContext = servletcontextevent.getServletContext(); //服务器启动的时候加载日志需要的信息 noteMap(servletContext); } private void noteMap(ServletContext servletContext) { // TODO Auto-generated method stub //获取日志表的对应数据 Map<String String> noteTableMap = noteTableDAO.queryAllForMap(); //获取日志操作的对应数据 Map<String String> noteOperMap = noteOperDAO.queryAllForMap(); //将查询到的结果存放至作用域 servletContext.setAttribute(""noteTableMap"" noteTableMap); servletContext.setAttribute(""noteOperMap"" noteOperMap); }}
解决方案
自己解决的:理由是,filter listener servlet 无法由spring 管理,因此用注解获取不到相应对象,必须通过getBean 的方式来获取。
值得一提的是,通过注解过的类也可以通过getBean("""")方式获取到,而不必再进行相关配置。
解决方案:
public class ApplicationListener implements ServletContextListener { //获取spring注入的bean对象 private WebApplicationContext springContext; private NoteTableDAO noteTableDAO; private NoteOperDAO noteOperDAO; @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub this.springContext = null; this.noteTableDAO = null; this.noteOperDAO = null; } @Override public void contextInitialized(ServletContextEvent servletcontextevent) { // TODO Auto-generated method stub //获取ServletContext对象 ServletContext servletContext = servletcontextevent.getServletContext(); //服务器启动的时候加载日志需要的信息 noteMap(servletContext); } private void noteMap(ServletContext servletContext) { // TODO Auto-generated method stub springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); if (springContext != null) { noteOperDAO = (NoteOperDAO) springContext.getBean(""noteOperDAO""); noteTableDAO = (NoteTableDAO) springContext.getBean(""noteTableDAO""); //获取日志表的对应数据 Map<String String> noteTableMap = noteTableDAO.queryAllForMap(); //获取日志操作的对应数据 Map<String String> noteOperMap = noteOperDAO.queryAllForMap(); //将查询到的结果存放至作用域 servletContext.setAttribute(""noteTableMap"" noteTableMap); servletContext.setAttribute(""noteOperMap"" noteOperMap); } } public NoteTableDAO getNoteTableDAO() { return noteTableDAO; } public void setNoteTableDAO(NoteTableDAO noteTableDAO) { this.noteTableDAO = noteTableDAO; } public NoteOperDAO getNoteOperDAO() { return noteOperDAO; } public void setNoteOperDAO(NoteOperDAO noteOperDAO) { this.noteOperDAO = noteOperDAO; }
解决方案二:
@Resource 是byName jdk自带的注入 如果有多个或者没有名字,会抛出异常。 @Autowired是byType 可以用@Autowired试试 Spring的注入
解决方案三:
Autowired能完成的 Resource都能完成。况且Listener是属于JavaEE范畴,怎么可能用Autowired?所以这个结论肯定是不成立的。
解决方案四:
at org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource(DefaultInstanceManager.java:559)at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:449)at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:135)at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:116) 你这里是找到多个name重名的,
时间: 2024-09-17 20:33:11