blaks
blaks

Reputation: 123

struts2 annotation spring managed beans

I'm using Spring to manage the struts 2 action beans. I am trying to migrate from XML based configuration to annotation based. I am using the struts2-spring-plugin so that struts gets the handle to the object managed by spring. The following is in the applicationContext file for myAction.

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema
      /beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org
      /schema/context/spring-context-3.0.xsd">
    <context:annotation-config />
    <bean id="myAction123" class="com.action.MyAction" scope="prototype"></bean>
</beans>

In the struts2-tiles config file I define the request mapping. This mapping uses a reference to the bean created by spring. The class attribute refers to the id attribute defined in the spring bean definition.

    <action name="myAction_*" method="{1}" class="myAction123">
        <result name="success" type="tiles">action.request.view</result>
    </action>

The Action class is annotated as follows :

@Controller
public class MyAction {

    @Autowired
    public MyService myService;

    public MyService getMyService() {
        return myService;
    }

    public void setMyService(MyService myService) {
        this.myService = myService;
    }

    public String doSearch() {
        /* ... */
    }
}

Please find the stack trace below, thrown by struts when I remove the spring action bean reference.

09:10:37,852 ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/gas]  - Exception starting filter struts
Action class [volumeRequestAction] not found - action - file:/C:/dev/Workspace/GAS2/_GAS2WebApp/target/_GAS2WebApp-2.0/WEB-INF/classes/struts2-tiles.xml:701:81
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:374)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:329)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:429)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:221)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:302)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:78)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3635)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4222)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Oct 5, 2012 9:10:37 AM org.apache.catalina.core.StandardContext start

I understand that I have not put the correct annotations in place which is the cause for this issue. Although I have tried reading about this quite a bit, I am still confused about migrating this particular part. I am unable to figure out if I will be using struts 2 annotations or spring annotations for this. Any help is appreciated. Thank you.

Upvotes: 0

Views: 2327

Answers (1)

user497087
user497087

Reputation: 1591

You will also need the line

<context:component-scan base-package="my.package.path" />

in your application-context.xml file to tell Spring where to go and look for annotations. There is nothing wrong with using the @Controller annotation.

Upvotes: 1

Related Questions