2

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?

3
  • i am not sure what you mean by this statement "The Actions constructor is called per reqiuest" Feb 27, 2012 at 4:16
  • Each time, when a request is sent from the client (e.g. a browser) a new instance of the action is created. I don't want a new instance each time. I want the action to be held in Web-Session Scope by Spring IoC.
    – Tarator
    Mar 2, 2012 at 16:05
  • 3
    i will NOT AT ALL suggest you to do this, since S2 action not only work as central processing for your particular request but also work as Model used to transfer data, so that's why they are being created at each request and its as per the architecture of S2, changing in the core behavior can lead to undesirable results including inconsistency results Mar 2, 2012 at 17:11

1 Answer 1

1

The value for struts.objectFactory is not correct, instead of "spring" it should be "org.apache.struts2.spring.StrutsSpringObjectFactory"

<struts>
  <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> 
  ... 
</struts>

For further information see: http://struts.apache.org/2.3.1.2/docs/spring-plugin.html

Note that actions are instantiated per request, and so holding them in session would very likely cause weird things to happen with no discernible benefit (profile it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.