更新时间:2023-02-08 18:01:39
Use ContextLoaderPlugin and set the struts controller to the processorClass "AutowiringRequestProcessor" like this (in struts-config.xml):
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.AutowiringRequestProcessor" />
</controller>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config/action-servlet.xml"/>
</plug-in>
action-servlet.xml必须是一个空的bean上下文文件:
action-servlet.xml has to be an empty beans context file:
<beans></beans>
在web.xml中的ActionServlet中添加以下初始化参数:
Add the following init parameter to the ActionServlet in web.xml:
<init-param>
<param-name>autowire</param-name>
<param-value>byName</param-value>
</init-param>
只需编写常规的struts动作,并在每个动作中添加注释"@Component",以便spring可以发现这些动作并从中创建一个bean. "AutowiringRequestProcessor"将找到与在struts-config.xml中定义的动作类匹配的正确的bean.
Just write regular struts actions, and add the annotation "@Component" to every action so that spring will discover the actions and creates a bean out of it. The "AutowiringRequestProcessor" will find the right bean that matches the action class defined in your struts-config.xml.
现在还可以在设置器上使用@Autowired将其他bean注入到Action类中.
It's now also possible to have other beans injected into you Action class with @Autowired on setter(s).