日期:2014-05-17  浏览次数:20691 次

ssh框架连接数据库的时候,非spring注入的方式如何访问数据库。
我在使用 filter 的时候连接了数据库的时候,会被指出空指针错误,
因为没有使用spring注入的方式。

因为除了action之外,还有很多地方都需要直接使用hibernate连接数据库的。
请问,ssh框架非spring注入的方式如何访问数据库?

代码如下:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOExceptionServletException {
        // TODO Auto-generated method stub
        // place your code here
        
        // pass the request along the filter chain
        
        SystemDao systemDao = new SystemDaoImpl();
        System system = systemDao.queryEntity();
        if (request.getServletContext().getAttribute("System") == null) {
            request.getServletContext().setAttribute("System", system);
            logger.error(system);
        }
        
        chain.doFilter(request, response);
    }

------解决方案--------------------
还是用注入的方式
SystemDao systemDao = new SystemDaoImpl();
改成
SystemDao systemDao = (SystemDao)WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()).getBean("systemDao");
------解决方案--------------------

又没规定Spring注入的数据源就只能在action访问,实际上是你想在哪里访问就在哪里注入!
------解决方案--------------------
代理加载 bean
------解决方案--------------------
使用 Spring 容器管理 Filter