简体   繁体   中英

Can I inject multiple spring dependencies from struts actions

I have struts xml in which I had created one action say Baseaction. I need to inject all the beans defined in application context file.

I am using struts2 spring plug-in. Can I inject all the bean dependencies from springs application context xml file separately.

Thanks, Nilesh

Yes you can.

Set Spring as Struts objectFactory in Struts.xml :

<struts>
  <constant name="struts.objectFactory" value="spring" />
  ...
</struts>

Declare the Spring listener in web.xml :

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

Since the default autowire strategy is by name, just provide an Action property with a setter for each bean you want to be injected in that Action.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
                       "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
    <bean id="fooManager" class="com.acme.FooManager" 
       scope="prototype"/>
    ...
</beans>

Read more on the documentation .

If you need to inject all your beans in an action, then declare all the corrispondent properties and setters in that action.

This btw is weird, because if the BaseAction will be extended by all other actions, it is unlikely that each one of the other actions will need all the beans... you should define the beans in the actions that need them; this sounds like a bit of lazyness, that will cost your webapp a bit of performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM