Note that there are some explanatory texts on larger screens.

plurals
  1. POStruts2 Spring Plugin: Dependency Injection with Annotated Actions does not work
    primarykey
    data
    text
    <p>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!</p> <p>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.</p> <pre><code>@Bean @Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS) public PatientForm PatientForm(){ System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; PatientForm() "); return new PatientForm(); } @Bean(name="patient") @Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS) public PatientAction PatientAction(){ System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; PatientAction() "); return new PatientAction(); } </code></pre> <p>Here the implementation of the Action:</p> <pre><code> 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; } } } </code></pre> <p>Calling the URL "http://localhost/myApp/patient" makes an Instance of the Action-Class on each request, without entering the <code>public PatientAction PatientAction()</code> method.</p> <p>When I use this in struts,xml:</p> <pre><code>&lt;package name="default" extends="struts-default"&gt; &lt;action name="foo" class="patient"&gt; &lt;result&gt;list.jsp&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; </code></pre> <p>And call "http://localhost/myApp/foo" the action is injected via spring.</p> <p>This is my struts.properties file:</p> <pre><code>struts.i18n.encoding=UTF-8 struts.objectFactory = spring ## Tried settings with autoWire #struts.objectFactory.spring.autoWire = auto struts.objectFactory.spring.autoWire = type </code></pre> <p>Versions I use (via Maven:)</p> <pre><code>struts2-core 2.2.3.1 spring3 3.1.1.RELEASE struts2-spring-plugin 2.3.1.2 </code></pre> <p>Can anybody tell me what I'm doing wrong with annotations?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload