Struts2 Spring插件:带注释操作的依赖注入不起作用

时间:2012-02-25 17:18:40

标签: spring dependency-injection struts2 annotations spring-annotations

我有以下问题。我有一个应用程序与struts2,spring和struts2-spring-plugin启动并运行。通过Spring的依赖注入通常起作用。 (例如。将一个bean注入一个Action)但是:我的Action-classes不是按照定义的每个会话弹出的。每个reqiuest调用Actions构造函数。看起来spring不使用Spring的对象工厂。在struts.xml中定义Action而不是使用@Action Annotations时,依赖注入有效!

这里有一些片段:这里我已经定义了一个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;
            }
        }
    }

调用URL“http:// localhost / myApp / patient”会在每个请求上创建一个Action-Class实例,而无需输入public PatientAction PatientAction()方法。

当我在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

有人可以告诉我注释错误吗?

1 个答案:

答案 0 :(得分:1)

struts.objectFactory的值不正确,而不是“spring”,它应该是“org.apache.struts2.spring.StrutsSpringObjectFactory”

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

有关详细信息,请参阅:http://struts.apache.org/2.3.1.2/docs/spring-plugin.html

请注意,每个请求都会实例化操作,因此将它们保存在会话中很可能会导致奇怪的事情发生而没有明显的好处(对其进行配置)。