如何在filter中使用String的IOC
如题,需求描述如下:
在filter中把用户点击的URL、IP、cookie、时间等信息获取出来,保存到数据库,用于分析哪个url使用的最多,同时也作为一个系统日志。
问题:
获取到信息后,要在filter中使用String的注解bean,然后把数据保存。但是,根据系统启动时加载顺序:listener >> filter >> servlet >> spring,在filter加载的时候,spring中的bean还没有加载,这就导致我在filter中使用bean时,bean为null。
在网上找了几种办法,试了都不行:
方法一:
其中,ITestDao为接口类,在实现类中有一个注解:@Component("testDao")
这种方式报错信息如下:
org.hibernate.HibernateException: No Session found for current thread
方法二:
web.xml中:
在application-beans.xml中:
在TestFilter中:
这个也报错,原因为this.sessionFactory.getCurrentSession()为null了。
我使用的是:struts2+hibernate4+spring4
请各位给点化一下,谢谢!
在filter中把用户点击的URL、IP、cookie、时间等信息获取出来,保存到数据库,用于分析哪个url使用的最多,同时也作为一个系统日志。
问题:
获取到信息后,要在filter中使用String的注解bean,然后把数据保存。但是,根据系统启动时加载顺序:listener >> filter >> servlet >> spring,在filter加载的时候,spring中的bean还没有加载,这就导致我在filter中使用bean时,bean为null。
在网上找了几种办法,试了都不行:
方法一:
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
this.baseDao = (ITestDao<Test>)webApplicationContext.getBean("testDao");
其中,ITestDao为接口类,在实现类中有一个注解:@Component("testDao")
这种方式报错信息如下:
org.hibernate.HibernateException: No Session found for current thread
方法二:
web.xml中:
<filter>
<filter-name>DelegatingFilterProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>myFilter</param-value>
</init-param>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DelegatingFilterProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在application-beans.xml中:
<bean id="myFilter"
class="com.Test.filter.TestFilter">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
在TestFilter中:
this.sessionFactory.getCurrentSession().save(test);
这个也报错,原因为this.sessionFactory.getCurrentSession()为null了。
我使用的是:struts2+hibernate4+spring4
请各位给点化一下,谢谢!
...全文
相关推荐
详解spring security filter的工作原理
主要介绍了详解spring security filter的工作原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
struts2基础简要介绍
简单的介绍strut的基本知识,应该对初学者有较大的帮助
java软件工程师面试基本题_共5页.pdf
java软件工程师面试基本题_共5页.pdf
通过实例解析java过滤器和拦截器的区别
主要介绍了通过实例解析java过滤器和拦截器的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
java8源码-esmusssein777.github.io:我的学习记录
java8 源码 Spring IoC源码 effectiveJava学习笔记 Java8的实战学习笔记



