3

Spring LDAP アプリケーションを作成しており、ContextSource の認証戦略を設定する必要があります。ビーンズ XML ファイルでこれを行いたいと思います。ContextSourceのJavaDoc には、呼ばれるセッターメソッドがあると書かれています

setAuthenticationStrategy(
    DirContextAuthenticationStrategy authenticationStrategy
)

このセッターをビーンズ ファイルから呼び出すには、次の XML で十分ですか?

<bean id="authStrategy"
    class="org.springframework...DefaultTlsDirContextAuthenticationStrategy">
 ...
</bean>

<bean id="contextSource"
    class="org.springframework.ldap.core.support.LdapContextSource">

    <property name="url" ... />
    <property name="base" ... />
     ...
    <property name="authenticationStrategy" ref="authStrategy" /> 
</bean>

つまり、メソッドの呼び出しを正確に決定するものは何setAuthenticationStrategyですか? 私の物件名はauthenticationStrategyですか?Spring はプロパティ名を適切なセッター メソッドに自動的に変換しますか?

4

2 に答える 2

5

実は、JavaBean コンテキストでの「プロパティ」という言葉の意味を誤解しています。

Bean プロパティとフィールド

JavaBeans 標準 (Spring はこれに厳密に従っています) は、特定の命名規則に従う Getter メソッドおよび / または Setter メソッドを持つものとして Bean プロパティを定義します。

プロパティ 'Bar foo' の場合、getter Bar getFoo()(またはisFoo()boolean プロパティの場合) または setter setFoo(Bar)(または両方) が存在する必要がありますが、"foo" という名前のフィールドが存在する必要はありません。慣例により、通常はプロパティと同じ名前のフィールドがありますが、必須ではありません。

たとえば、次のクラス (JavaBeans 標準に準拠) には、Integer 型の Bean プロパティ「foo」がありますが、基礎となるフィールドは呼び出されiAmNotFoo、String 型です。

public class Dummy {
    private String  iAmNotFoo;
    public Integer getFoo() {
        return  Integer.valueOf(this.iAmNotFoo);
    }

    public void setFoo(final Integer foo) {
        this.iAmNotFoo = foo.toString();
    }
}

この仮定は、次のコードでテストできます。

public static void main(final String[] args) throws Exception {
    for (final PropertyDescriptor descriptor :
        Introspector
            .getBeanInfo(Dummy.class, Object.class)
            .getPropertyDescriptors()) {
        System.out.println(
            "Property: "
            + descriptor.getName()
            + ", type: "
            + descriptor.getPropertyType()
        );
    }
    for (final Field field : Dummy.class.getDeclaredFields()) {
        System.out.println(
            "Field: " 
            + field.getName() 
            + ", type: " 
            + field.getType());
    }
}

出力:

プロパティ: foo、タイプ: クラス java.lang.Integer
フィールド: iAmNotFoo、タイプ: クラス java.lang.String

春の物件

上で述べたように、Spring はまさにこのメカニズムを使用してプロパティを設定します。したがって、このように Bean を構成すると

<bean class="Dummy">
    <property name="foo" value="123" />
</bean>

「foo」は Bean プロパティ「foo」を参照するため、setter を参照します。setFoo()

これにより、次のような構成が可能になります。

public class Dummy2 {
    private List<String> foos;
    public void setFoos(List<String> foos) {
        this.foos = foos;
    }
    public void setFoo(String foo){
        this.foos = Collections.singletonList(foo);
    }
}

これを次のように配線できます

<bean class="Dummy2">
    <!-- either set a single value -->
    <property name="foo" value="123" /> 
    <!-- or a list of values -->
    <property name="foos"> 
        <util:list>
            <value>Abc</value>
            <value>Xyz</value>
            <value>123</value>
            <value>789</value>
        </util:list>
    </property>
</bean>

ご覧のとおり、setter メソッドは実際のフィールドではなく、Spring に関連しています。

そのため、JavaBeans では次のように話します: フィールド != プロパティ。ただし、ほとんどの場合、プロパティと同じ型と名前のフィールドが存在します。

于 2011-01-21T20:43:57.077 に答える
2

あなたの疑いは正しいです。Spring はプロパティ名をセッター メソッドに変換します。

引数として使用している Bean はタイプDefaultTlsDirContextAuthenticationStrategyであり、メソッドはタイプ のオブジェクトを受け入れるDirContextAuthenticationStrategyためDefaultTlsDirContextAuthenticationStrategy、実装者のサブクラスである必要がありますDirContextAuthenticationStrategy

于 2011-01-21T18:13:23.167 に答える