struts2的注解模式配置action
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("testt" + 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管理啊,有大牛解惑下嘛?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
引用来自“基哥”的答案
引用来自“JFinal”的答案
OMG
引用来自“基哥”的答案
引用来自“JFinal”的答案
OMG
唔,按照自己的思路以及您的框架体系,对ssh进行了类似的封装,把事务的声明提到了action层,写了2个小项目,还没感觉有什么不便.您觉得这样封装有什么值得注意的么 :)
引用来自“基哥”的答案
引用来自“JFinal”的答案
OMG
OMG
需要说明的是,struts2默认不是单例的,就是说每次请求都会new 所以当然会调用 constructor
struts1是默认单例的.
需要说明的是,struts2默认不是单例的,就是说每次请求都会new 所以当然会调用 constructor
struts1是默认单例的.
引用来自“基哥”的答案
引用来自“JFinal”的答案
OMG