How Spring is injecting properties in Struts actions, that are not defined in applicationContext.xml

落花浮王杯 提交于 2019-11-28 05:55:50

问题


I am in a project having frameworks like Spring 3 + Struts 2 + Hibernate 3. Also it uses spring security.

Spring and Struts is integrated using spring ContextLoaderListener defined in web.xml

<listener>
    <display-name>Spring Initialization</display-name>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

There are several service classes which usually make db calls. like agentserviceprocess is one such class having method like getAgents().

Struts actions have a property and setter getter for each process and those process classes are defined as bean in application-context.xml.

Surprisingly I can not find any bean having action class as class parameter. I mean spring is not controlling the action object instantiation. Then how spring can inject service property objects inside these actions?


Also, I found following entry

<filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*.action</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

回答1:


For integration with spring you should use struts2-spring plugin. When this plugin is loaded a plugin's configuration is applied to use its own object factory to instantiate action classes. When action instanses are built it passes them to spring for autowiring. You should provide getters and setters of properties for being autowired. You have also provide the spring configuration for dependencies.



来源:https://stackoverflow.com/questions/28476069/how-spring-is-injecting-properties-in-struts-actions-that-are-not-defined-in-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!