0

列挙型ContentTypeがあり、regText ORセッションに評価できるContentType.getName()のようなメソッドがあります。したがって、このメソッドの戻り値に基づいてBeanをインスタンス化できる以下の方法を実行するにはどうすればよいですか。また、これはXML構成でのみ実行し、注釈では実行しません。

<property name="contentCaptureRegEx" ref="${ContentType.getName()}">
</property>

<bean id="regText" class="java.util.regex.Pattern" factory-method="compile" lazy-init="true">
<constructor-arg value="xyz" /></bean>

<bean id="session" class="java.util.regex.Pattern" factory-method="compile" lazy-init="true">
<constructor-arg value="abc" /></bean>
4

1 に答える 1

1

パターン正規表現はすでにそのパターンを使用しているので、静的ファクトリメソッドをお勧めします。それらを削除して追加するだけです:

package com.mine;

public class MyFactory {

    public static Pattern newContentCaptureRegEx() {
        String patternString;
        if ("regText".equals(ContentType.getName())) {
            patternString = "xyz";
        } else if ("session".equals(ContentType.getName())) {
            patternString = "abc";
        } else {
            throw new IllegalStateException("ContentType must be regText or session");
        }
        Pattern.compile(patternString);
    }

}

次のように配線できます:

<bean id="ContentCaptureRegEx" class="com.mine.MyFactory"
    factory-method="newContentCaptureRegEx" />

そして、そのBeanをどこでも次のように参照できます。

<property name="contentCaptureRegEx" ref="ContentCaptureRegEx" />
于 2012-08-20T15:46:02.077 に答える