2

次の問題があります。struts2、spring、および struts2-spring-plugin が稼働しているアプリケーションがあります。Spring を介した依存性注入は一般的に機能します。(たとえば、Bean をアクションに注入します) しかし、私のアクション クラスは、定義されているように、セッションごとにスプリングを介して注入されません。Actions コンストラクターは、要求ごとに呼び出されます。Spring は Spring のオブジェクト ファクトリを使用していないようです。@Action アノテーションを使用する代わりに struts.xml で Action を定義すると、依存性注入が機能します。

ここにいくつかのスニペットがあります: ここでは、Bean とアクションを定義しています。Bean のインジェクションは機能しますが、@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;
            }
        }
    }

public PatientAction PatientAction()URL「http://localhost/myApp/patient」を呼び出すと、メソッドを入力せずに、リクエストごとに Action-Class のインスタンスが作成されます。

これを 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

注釈で何が間違っているのか誰か教えてもらえますか?

4

1 に答える 1

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 を参照してください。

アクションはリクエストごとにインスタンス化されるため、それらをセッションに保持すると、認識できる利点がなく奇妙なことが発生する可能性が非常に高いことに注意してください (プロファイルします)。

于 2012-04-12T22:03:55.273 に答える