开发者社区> 问答> 正文

struts2的注解模式配置action - struct报错

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管理啊,有大牛解惑下嘛?

展开
收起
montos 2020-06-02 19:13:04 486 0
1 条回答
写回答
取消 提交回答
  • 引用来自“基哥”的答案

    引用来自“JFinal”的答案

    OMG
    看过您的jfinal,确实很好用,无奈公司非要用struts2。。。 求大神指点啊。。。
    你的测试方法是错误的,用struts2-sping插件后,插件会拦截struts2默认的action实例,而改为spring 创建,至于是不是spring创建不用怀疑,你action类上那些注解都没用,只要你用spring的方法在action中注入一个 bean就知道了,非spring创建的action 这个bean肯定不会有值,而spring创建的这个bean肯定有值,试试!    
    ######OMG######

    需要说明的是,struts2默认不是单例的,就是说每次请求都会new 所以当然会调用 constructor

    struts1是默认单例的.

    ######

    引用来自“潮汐、”的答案

    需要说明的是,struts2默认不是单例的,就是说每次请求都会new 所以当然会调用 constructor

    struts1是默认单例的.

    我设@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON),就是想看下是不是由spring管理的,如果是的话,应该是单例,结果显示多例说明该action没有被spring管理呀
    ######

    引用来自“JFinal”的答案

    OMG
    看过您的jfinal,确实很好用,无奈公司非要用struts2。。。 求大神指点啊。。。
    ######

    引用来自“基哥”的答案

    引用来自“JFinal”的答案

    OMG
    看过您的jfinal,确实很好用,无奈公司非要用struts2。。。 求大神指点啊。。。
        我以前用 SSH 的时候,自己做了封装,基于Hibernate做了一个 Dao 工具类专门应付数据库操作,另外自己做了一个 Controller 继承自 ActionSupport 简化控制层,这个Controller与现在 JFinal Controller 很像。始终对 SSH 的开发效率很失望,一直期盼 java 世界的 rails 能出来,无奈一直没人做这事,只有自己做了 :)
    ######唔,按照自己的思路以及您的框架体系,对ssh进行了类似的封装,把事务的声明提到了action层,写了2个小项目,还没感觉有什么不便.您觉得这样封装有什么值得注意的么 :)######

    引用来自“抓瓦Or”的答案

    引用来自“基哥”的答案

    引用来自“JFinal”的答案

    OMG
    看过您的jfinal,确实很好用,无奈公司非要用struts2。。。 求大神指点啊。。。
    你的测试方法是错误的,用struts2-sping插件后,插件会拦截struts2默认的action实例,而改为spring 创建,至于是不是spring创建不用怀疑,你action类上那些注解都没用,只要你用spring的方法在action中注入一个 bean就知道了,非spring创建的action 这个bean肯定不会有值,而spring创建的这个bean肯定有值,试试!    
    是这样的,非常感谢
    ######

    引用来自“JFinal”的答案

    引用来自“基哥”的答案

    引用来自“JFinal”的答案

    OMG
    看过您的jfinal,确实很好用,无奈公司非要用struts2。。。 求大神指点啊。。。
        我以前用 SSH 的时候,自己做了封装,基于Hibernate做了一个 Dao 工具类专门应付数据库操作,另外自己做了一个 Controller 继承自 ActionSupport 简化控制层,这个Controller与现在 JFinal Controller 很像。始终对 SSH 的开发效率很失望,一直期盼 java 世界的 rails 能出来,无奈一直没人做这事,只有自己做了 :)
    Jfinal真的挺好的,希望有机会能把Jfinal用到项目中,感谢大神热心回复
    ######多谢支持,随时欢迎回归 JFinal
    2020-06-02 19:13:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载