Struts2 Spring插件:带有注释操作的依赖项注入不起作用
我有以下问题。我有一个struts2、spring和struts2-spring插件的应用程序正在运行。通过Spring的依赖项注入通常可以工作。(例如,将一个bean注入到一个动作中)但是:我的动作类并不是按照定义通过spring每会话注入的。Actions构造函数是根据Requiest调用的。spring似乎没有使用spring的对象工厂。当在struts.xml中定义操作而不是使用@Action注释时,依赖项注入就起作用了 这里有一些片段:这里我定义了一个bean和一个操作。bean的注入是可行的,但是当使用@Action注释时,这里永远不会创建操作Struts2 Spring插件:带有注释操作的依赖项注入不起作用,spring,dependency-injection,struts2,annotations,spring-annotations,Spring,Dependency Injection,Struts2,Annotations,Spring Annotations,我有以下问题。我有一个struts2、spring和struts2-spring插件的应用程序正在运行。通过Spring的依赖项注入通常可以工作。(例如,将一个bean注入到一个动作中)但是:我的动作类并不是按照定义通过spring每会话注入的。Actions构造函数是根据Requiest调用的。spring似乎没有使用spring的对象工厂。当在struts.xml中定义操作而不是使用@Action注释时,依赖项注入就起作用了 这里有一些片段:这里我定义了一个bean和一个操作。bean的注入
@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://localhost/myApp/patient在每个请求上生成Action类的实例,而不输入public PatientAction PatientAction()
方法
当我在struts中使用它时,xml:
<package name="default" extends="struts-default">
<action name="foo" class="patient">
<result>list.jsp</result>
</action>
</package>
我使用的版本(通过Maven:)
谁能告诉我注释有什么不对吗?struts.objectFactory的值不正确,应该是“org.apache.struts2.spring.StrutsSpringObjectFactory”而不是“spring”
...
有关更多信息,请参阅:
请注意,每个请求都会实例化操作,因此在会话中保留它们很可能会导致奇怪的事情发生,而没有明显的好处(对其进行分析) 我不确定您所说的“Actions constructor is called per Requiest”是什么意思,每次从客户端(例如浏览器)发送请求时,都会创建一个新的action实例。我不希望每次都有新的实例。我想让Spring IoC在Web会话范围内执行此操作。我完全不建议您这样做,因为S2操作不仅作为您特定请求的中央处理,而且还作为用于传输数据的模型,这就是为什么在每次请求时都会根据S2的体系结构创建它们,更改核心行为可能会导致不希望的结果,包括不一致的结果
struts.i18n.encoding=UTF-8
struts.objectFactory = spring
## Tried settings with autoWire
#struts.objectFactory.spring.autoWire = auto
struts.objectFactory.spring.autoWire = type
struts2-core 2.2.3.1
spring3 3.1.1.RELEASE
struts2-spring-plugin 2.3.1.2
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
...
</struts>