程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 服務器-看異常,用配置沒問題。但是注解會報錯,求解答!!!

服務器-看異常,用配置沒問題。但是注解會報錯,求解答!!!

編輯:編程綜合問答
看異常,用配置沒問題。但是注解會報錯,求解答!!!
 嚴重: Error configuring application listener of class ibatis.apache.org.util.ApplicationListener
javax.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;
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved