我有以下问题。我有一个带有struts2、spring和struts2-spring-plugin的应用程序正在运行。通过Spring的依赖注入通常可以正常工作。(例如。向Action注入一个bean ),但是:我的Action类并不是像定义的那样通过每个会话的spring注入的。根据请求调用操作构造函数。看起来spring并没有使用Spring的对象工厂。当在struts.xml中定义操作而不是使用@操作注释时,依赖注入可以工作!
这里有一些代码片段:我在这里定义了一个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();
}这里是Action的实现:
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;
}
}
}在没有进入public PatientAction PatientAction()方法的情况下,调用URL "http://localhost/myApp/patient“将在每个请求上生成一个操作类的实例。
当我在struts中使用它时,xml:
<package name="default" extends="struts-default">
<action name="foo" class="patient">
<result>list.jsp</result>
</action>
</package>并调用"http://localhost/myApp/foo“,动作是通过spring注入的。
这是我的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有人能告诉我我在注释方面做错了什么吗?
发布于 2012-04-13 06:03:55
spring的值不正确,它应该是"org.apache.struts2.spring.StrutsSpringObjectFactory“,而不是”struts.objectFactory“
<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
...
</struts>有关更多信息,请参阅:http://struts.apache.org/2.3.1.2/docs/spring-plugin.html
请注意,每个请求都会实例化操作,因此将它们保留在会话中很可能会导致奇怪的事情发生,而没有明显的好处(分析它)。
https://stackoverflow.com/questions/9446176
复制相似问题