【发布时间】:2014-11-13 09:31:17
【问题描述】:
我有一个使用定义的拦截器,从拦截器我想通过 DAO 层进行 db 调用,那么如何将 spring bean 注入到 struts 拦截器。是否可以将 spring bean 注入到 struts 拦截器 任何人都可以对此提出任何想法。
【问题讨论】:
-
你试过注入吗?究竟是什么不工作?
我有一个使用定义的拦截器,从拦截器我想通过 DAO 层进行 db 调用,那么如何将 spring bean 注入到 struts 拦截器。是否可以将 spring bean 注入到 struts 拦截器 任何人都可以对此提出任何想法。
【问题讨论】:
编辑
由于不需要将 Interceptor 声明为 Spring bean,因此我删除了不必要的部分。感谢@AleksandrM 对其进行测试。
与 Actions 完全一样,除了(如果我没记错的话)在 beans.xml 中声明它的例外,因为拦截器不扩展 ActionSupport(默认情况下是自动装配的)。
web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
ApplicationContext.xml
<bean id="daoServiceBean"
class="org.foo.bar.business.dao.DaoService"/>
罢工>
<bean id="myInterceptorBean"
class="org.foo.bar.presentation.interceptors.MyInterceptor"/>
Struts.xml
<constant name="struts.objectFactory" value="spring" />
<package ...>
<interceptors>
罢工>
<interceptor name="myInterceptor" class="myInterceptorBean" />
<interceptor name="myInterceptor"
class="org.foo.bar.presentation.interceptors.MyInterceptor"/>
MyInterceptor.java
private DaoService daoServiceBean; // Autowired by Spring
另请阅读:
【讨论】: