服务器-看异常,用配置没问题。但是注解会报错,求解答!!!

问题描述

看异常,用配置没问题。但是注解会报错,求解答!!!
 严重: 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

服务器-看异常,用配置没问题。但是注解会报错,求解答!!!的相关文章

安卓-android 新人仿照视频写的一个计算的Demo 对比几次没问题,但是执行 报错求救

问题描述 android 新人仿照视频写的一个计算的Demo 对比几次没问题,但是执行 报错求救 Activity03.java private EditText Ac03textone; private EditText Ac03texttwo; private TextView Ac03viewone; private Button Ac03buttone; @Override protected void onCreate(Bundle savedInstanceState) { supe

jmx-【防火墙已关闭】帮看下JMX本地Main方法连接取Broker报错

问题描述 [防火墙已关闭]帮看下JMX本地Main方法连接取Broker报错 **帮我看下JMX本地Main方法连接取Broker报错 (No broker is found at any of the 1 configured urls)** JConsole通过jmxrmi连接 service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi本地正常 activemq.xml配置 <!-- Licensed to the Apache Software

ie8下 全选功能报错-ie8下 JS全选功能报错 求达人帮忙看下代码

问题描述 ie8下 JS全选功能报错 求达人帮忙看下代码 // ???? function autoCalDaysNm() { for(var j=1; j<=29; ++j) { var el = $("#writeForm input[name=atdDay" + j + "]"); var d = Date.parse("2016/02/" + el.val()); if (d) { var t = d.toString("

图片-androidstudio 报错求方法。大神看一下吧

问题描述 androidstudio 报错求方法.大神看一下吧 androidstudio Plugin with id 'com.android.application求方法 解决方案 要下载相应插件,可以找下国内镜像.搜:Android国内镜像. 解决方案二: http://stackoverflow.com/questions/24795079/error1-0-plugin-with-id-com-android-application-not-found 解决方案三: 建议使用vpn去

模板-图中断点处,85行显示没问题,但94行会报错,为什么?

问题描述 图中断点处,85行显示没问题,但94行会报错,为什么? #include #include #include using namespace std; const int DefaultSize = 50; template class bitSet { public: bitSet(int sz = DefaultSize); bitSet(const bitSet& R); ~bitSet(){delete []bitVector;} void makeEmpty() { for

nginx fastdfs 配置后 上传成功 访问报404 求大神协助

问题描述 nginx fastdfs 配置后 上传成功 访问报404 求大神协助 nginx 配置 location /group1/M00 { root /home/fastdfs/data; ngx_fastdfs_module; } # the group name of the local storage server group_name=group1 if the url / uri including the group name set to false when uri lik

spring配置多数据源t junit测试报错

问题描述 spring配置多数据源t junit测试报错 Error creating bean with name 'seentao.workflow.engine.EngineTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: publi

java基础高手看这里了,这几道基础性的面试题求解答。

问题描述 java基础高手看这里了,这几道基础性的面试题求解答. 同学出去应聘,笔试的时候遇到这几道基础题不会做,拿给我看,发现自己也不怎么会,java基础好多都有些忘了,来帮忙解答一下吧. 1.实现一个函数,函数有一个形参,类型为整数,功能是将形参的十进制数的二进制序列打印到控制台上. 2.实现一个函数,函数有一个形参,类型为集合,功能是将集合中的内容按照每行3个输出(写出两种以上方法). 3.有数据表,字段定义为如下: 客户 商品 报价 报价日期 表中存放着不同客户,不同商品,不同日期的报价

大神,虚拟机运行没问题,手机运行报错,我懵了

问题描述 大神,虚拟机运行没问题,手机运行报错,我懵了 这是为啥么呢,大神 解决方案 把错误截图发一下.... 解决方案二: 编译没问题,运行不了?