2

我遇到以下问题。我有一个应用程序与struts2,弹簧和struts2弹簧插件启动和运行。通过Spring的依赖注入通常工作。 (例如,将一个bean注入到一个Action中)但是:我的Action-classes不会按照定义的每个会话的春季注入。 Actions构造函数被称为per requestuest。春天似乎不使用Spring的对象工厂。当在struts.xml中定义Action而不是使用@Action Annotations时,依赖注入将起作用!Struts2 Spring插件:带注释操作的依赖注入不起作用

这里有一些片段:这里我已经定义了一个bean和一个Action。注入bean作品,但在使用@Action注释时,Action从未在这里创建。

@Bean 
@Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public PatientForm PatientForm(){ 
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> PatientForm() "); 
    return new PatientForm(); 
} 

@Bean(name="patient") 
@Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public PatientAction PatientAction(){ 
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> PatientAction() "); 
    return new PatientAction(); 
} 

在这里,操作的执行:

public class PatientAction extends TherapyActionSupport { 
     private static final Logger logger = LoggerFactory.getLogger(PatientAction.class); 

     @Autowired 
     private PatientForm form; 

     public PatientAction(){ 
      logger.debug("Constructor called."); 
     } 

     @Override 
     @Action(name="/patient", 
     results={ 
      @Result(name=SUCCESS, location="/therapy/patient/edit.jsp"), 
      @Result(name=ERROR, location="/therapy/patient/edit.jsp"), 
      @Result(name=INPUT, location="/therapy/patient/edit.jsp") 
      } 
     ) 
     @SkipValidation 
     public String execute() throws Exception { 
      logger.info("Execute called."); 
      return SUCCESS; 
     } 

     @Action(value="/save", 
     results={ 
      @Result(name=SUCCESS, location="/therapy/patient/list.jsp"), 
      @Result(name=ERROR, location="/therapy/patient/edit.jsp"), 
      @Result(name=INPUT, location="/therapy/patient/edit.jsp") 
      } 
     ) 
     public String savePatient() throws Exception{ 
      try { 
       logger.info("Saving patient."); 
       getForm().savePatient(); 
       return list(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       return ERROR; 
      } 
     } 
    } 

调用URL “HTTP://本地主机/对myApp /患者” 使得操作-类的实例对每个请求,而不进入public PatientAction PatientAction()方法。

当我在支柱用这个,XML:

<package name="default" extends="struts-default"> 
    <action name="foo" class="patient"> 
     <result>list.jsp</result> 
    </action> 
</package> 

而且叫的 “http://本地主机/对myApp/foo” 的作用是通过弹簧注入。

这是我的struts.properties文件:我用

struts.i18n.encoding=UTF-8 
struts.objectFactory = spring 
## Tried settings with autoWire 
#struts.objectFactory.spring.autoWire = auto 
struts.objectFactory.spring.autoWire = type 

版本(通过Maven的:)

struts2-core 2.2.3.1 
spring3 3.1.1.RELEASE 
struts2-spring-plugin 2.3.1.2 

谁能告诉我我在做什么毛病注解?

+0

我不确定这句话的含义“动作构造函数是每个reqiuest调用的” – 2012-02-27 04:16:25

+0

每当客户端(例如浏览器)发送一个请求时,就会创建一个动作的新实例。我不希望每次都有新的实例。我希望Spring IoC能够在Web会话范围中进行操作。 – Tarator 2012-03-02 16:05:55

+3

我不会建议你这样做,因为S2操作不仅适用于您的特定请求的中央处理,而且还可以作为用于传输数据的模型,所以这就是为什么它们是在每个请求和它根据S2的体系结构,核心行为的变化可能导致包括不一致结果在内的不良结果 – 2012-03-02 17:11:06

回答

1

为struts.objectFactory的价值是不正确的,而不是 “春天”,它应该是 “org.apache.struts2.spring.StrutsSpringObjectFactory”

<struts> 
    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> 
    ... 
</struts> 

欲了解更多信息,请参见:http://struts.apache.org/2.3.1.2/docs/spring-plugin.html

请注意,每个请求都会实例化操作,因此将它们保留在会话中很可能会导致奇怪的事情发生,但没有明显的好处(配置文件)。