struts2的action可以交给spring去管理,用xml配置的action没有问题。但是我发现用struts2提供的注解配置的action,无法由spring管理。
先看下我的配置:
web.xml
<!--spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param>
<!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
sping.xml<!-- 开启注解模式 --> <context:annotation-config />
<!-- 自动扫描action、dao、service包(自动注入) --> <context:component-scan base-package="ylj.struts2.*" />
struts.xml<constant name="struts.objectFactory" value="spring" />TestAction.java
package ylj.struts2.action; import java.util.Date; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.opensymphony.xwork2.ActionSupport; @ParentPackage("default") @Namespace("/test") @Component("testAction") @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) public class TestAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; @Action(value = "test", results = { @Result(name = SUCCESS, location = "/test.jsp", type = "dispatcher") }) public String test() { return SUCCESS; } public TestAction() { System.out.println("--------------------------------------------"); System.out.println("test\t" + new Date().toString()); System.out.println("--------------------------------------------"); } }工程启动的时候控制台显示该action已经实例化了
信息: Initializing Spring root WebApplicationContext 2013-05-20 21:47:17 [org.springframework.web.context.ContextLoader]-[INFO] Root WebApplicationContext: initialization started 2013-05-20 21:47:17 [org.springframework.web.context.support.XmlWebApplicationContext]-[INFO] Refreshing Root WebApplicationContext: startup date [Mon May 20 21:47:17 CST 2013]; root of context hierarchy 2013-05-20 21:47:17 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [spring.xml] 2013-05-20 21:47:17 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2b7db1: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,testAction]; root of factory hierarchy -------------------------------------------- test Mon May 20 21:47:17 CST 2013 -------------------------------------------- 2013-05-20 21:47:18 [org.springframework.web.context.ContextLoader]-[INFO] Root WebApplicationContext: initialization completed in 260 ms 2013-05-20 21:47:18 [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider]-[INFO] Parsing configuration file [struts-default.xml] 2013-05-20 21:47:18 [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider]-[INFO] Parsing configuration file [struts-plugin.xml] 2013-05-20 21:47:18 [com.opensymphony.xwork2.config.providers.XmlConfigurationProvider]-[INFO] Parsing configuration file [struts.xml] 2013-05-20 21:47:18 [org.apache.struts2.config.BeanSelectionProvider]-[INFO] Choosing bean (spring) for (com.opensymphony.xwork2.ObjectFactory)但是我每次去访问该action的时候,依然会去调用TestAction的构造方法:
-------------------------------------------- test Mon May 20 21:49:21 CST 2013 -------------------------------------------- -------------------------------------------- test Mon May 20 21:49:23 CST 2013 -------------------------------------------- -------------------------------------------- test Mon May 20 21:49:30 CST 2013 -------------------------------------------- -------------------------------------------- test Mon May 20 21:49:35 CST 2013 --------------------------------------------是不是用注解配置的action没办法交给spring管理啊,有大牛解惑下嘛?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。