struts2注释spring托管bean

时间:2012-10-04 14:50:08

标签: spring spring-mvc struts2 annotations spring-annotations

我正在使用Spring来管理struts 2动作bean。我正在尝试从基于XML的配置迁移到基于注释。我正在使用struts2-spring-plugin,以便struts获取spring管理的对象的句柄。 以下是myAction的applicationContext文件。

<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>

在struts2-tiles配置文件中,我定义了请求映射。此映射使用对spring创建的bean的引用。 class属性引用spring bean定义中定义的id属性。

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

Action类注释如下:

@Controller
public class MyAction {

    @Autowired
    public MyService myService;

    public MyService getMyService() {
        return myService;
    }

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

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

请在下面找到堆栈跟踪,当我删除spring action bean reference时,由struts抛出。

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

据我所知,我没有提供正确的注释,这是导致此问题的原因。虽然我已经尝试过阅读相关内容,但我仍然对迁移这个特定部分感到困惑。我无法弄清楚我是否会使用struts 2注释或弹簧注释。任何帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

您还需要

<context:component-scan base-package="my.package.path" />
在您的application-context.xml文件中

告诉Spring去哪里寻找注释。使用@Controller注释没有任何问题。