I have the following problem. I have a application with struts2, spring and struts2-spring-plugin up and running. The dependency Injection via Spring works in generally. (For exasmple. Inject a bean into an Action) But: My Action-classes are not Iinjected via spring per session as defined. The Actions constructor is called per reqiuest. It seems like spring doesn't use the Spring's object factory. When defining the Action in struts.xml instead of using the @Action Annotations the dependency injection works!
Here some snippets: Here ihave defined a bean and an Action. Injection of the bean works, but the Action is never created here, when using @Action annotation.
@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();
}
Here the implementation of the 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;
}
}
}
Calling the URL "http://localhost/myApp/patient" makes an Instance of the Action-Class on each request, without entering the public PatientAction PatientAction()
method.
When I use this in struts,xml:
<package name="default" extends="struts-default">
<action name="foo" class="patient">
<result>list.jsp</result>
</action>
</package>
And call "http://localhost/myApp/foo" the action is injected via spring.
This is my struts.properties file:
struts.i18n.encoding=UTF-8
struts.objectFactory = spring
## Tried settings with autoWire
#struts.objectFactory.spring.autoWire = auto
struts.objectFactory.spring.autoWire = type
Versions I use (via Maven:)
struts2-core 2.2.3.1
spring3 3.1.1.RELEASE
struts2-spring-plugin 2.3.1.2
Can anybody tell me what I'm doing wrong with annotations?