How to integrate an old Struts application with Spring 3.x
Asked Answered
L

4

6

i was wondering how to and what the prefered way of integrating an Struts 1.x application with Spring 3.x so we can benefit from the IOC stuff.

Leukoderma answered 29/4, 2011 at 18:32 Comment(0)
T
7

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 has to be an empty beans context file:

<beans></beans>

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>

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.

It's now also possible to have other beans injected into you Action class with @Autowired on setter(s).

Trainbearer answered 4/5, 2011 at 12:16 Comment(2)
Do not annotate action classes by @Component because AutowiringRequestProcessor does not take their instances from Spring's BeanFactory it takes them from Struts and then calls autowireBeanProperties() method on them. So using @Component results on having unused beans in Spring context.Subgroup
It works. Component annotations are not needed. Autowired work for properties only, not for fields: private ITestService service; public void setService(@Autowired ITestService ts) { this.service = ts; }Rambouillet
C
2

Use ContextLoaderPlugIn. Deprecated in Spring 3.0, but still there.

I used it with Struts 1.x and Spring 2.5.x - it worked beautifully. This integration approach allows to inject Spring beans directly into Struts actions, which is pretty clean and straightforward.

Craniometry answered 29/4, 2011 at 21:4 Comment(0)
E
2

You can use the ApplicationContextAware interface to have a utility class have access to the ApplicationContext.

public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    public static Object getSpringBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public void setApplicationContext(ApplicationContext appContext)
        throws BeansException {
        applicationContext = appContext;
   }
}

You can then access the static method from your Action class.

public class MyAction extends LookupDispatchAction {
    private MyService getMyService() {
        return (MyService) SpringUtil.getSpringBean("myService");
    }
}

Not the most elegant solution, but it works.

Embellish answered 3/5, 2011 at 19:49 Comment(0)
I
-1

This does not work. The ContextLoaderPlugin is not available in ANY version of Spring after Spring 2.x. Struts 1.x is not compatible with any version of Spring after 2.x. It is IMPOSSIBLE to configure any version of Spring after 2.x to use Struts 1.x. You would either need to downgrade your Spring version of upgrade Struts. It would probably be easier to downgrade Spring unless you're using a hibernate version past 3.x.

Internationalize answered 4/2, 2016 at 3:5 Comment(3)
According to other answers it is possible.Bev
We have successfully used Spring 3.0 with Struts 1.2.Glossitis
All things are possible ~ you need to have a spring-2.x.x jar on the classpath...Oblivious

© 2022 - 2024 — McMap. All rights reserved.