У меня следующая проблема. У меня есть приложение с Struts2, Spring и Struts2-Spring-плагин и работает. Зависимость Injection через Spring работает в общем. (Например. Внедрите bean-компонент в Action). Но: Мои Action-классы не вводятся через пружину за сеанс, как определено. Конструктор Actions называется per reqiuest. Кажется, что Spring не использует фабрику объектов Spring. При определении действия в Struts.xml вместо использования аннотаций @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://localhost/myApp/patient" создает экземпляр класса действия при каждом запросе, без ввода метода public PatientAction PatientAction()
.
Когда я использую это в стойках, xml:
<package name="default" extends="struts-default">
<action name="foo" class="patient">
<result>list.jsp</result>
</action>
</package>
И звоните "http://localhost/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
Кто-нибудь может сказать мне, что я делаю неправильно с аннотациями?