Spring/LDAP - 在 bean 配置中调用 setter 方法

发布于 2024-10-12 23:12:03 字数 946 浏览 8 评论 0原文

我正在编写一个 Spring LDAP 应用程序,我必须为我的 ContextSource 设置身份验证策略。我想在我的 beans XML 文件中执行此操作。 JavaDoc for ContextSource 说它有一个名为的 setter 方法

setAuthenticationStrategy(
    DirContextAuthenticationStrategy authenticationStrategy
)

要从我的 beans 文件调用此 setter,以下 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 是否会自动将属性名称转换为适当的 setter 方法?

I am writing a Spring LDAP application and I have to set the authentication strategy for my ContextSource. I would like to do this in my beans XML file. The JavaDoc for ContextSource says that it has a setter method called

setAuthenticationStrategy(
    DirContextAuthenticationStrategy authenticationStrategy
)

To invoke this setter from my beans file, is the following XML sufficient?

<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>

That is to say, what exactly determines the invocation of the method setAuthenticationStrategy? Is it that my property name is authenticationStrategy? Does Spring automatically translate property names to the appropriate setter method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-10-19 23:12:03

实际上,您误解了 JavaBean 上下文中“属性”一词的含义。

Bean 属性与字段

JavaBeans 标准(Spring 密切遵循)将 Bean 属性定义为具有遵循特定命名约定的 Getter 方法和/或 Setter 方法的东西:

对于属性“Bar foo”,可以是 getter Bar getFoo()(或布尔属性的 isFoo())或 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,类型:class java.lang.Integer
字段:iAmNotFoo,类型:class java.lang.String

Spring 中的属性

正如我上面所说,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相关,而不是实际的字段。

因此,用 JavaBean 来说:Field != Property,尽管在大多数情况下存在与属性具有相同类型和名称的字段。

Actually, you misunderstood the meaning of the word 'property' in the JavaBean context.

Bean Properties vs Fields

The JavaBeans standard (which Spring follows closely) defines a Bean property as something that has a Getter method and / or a Setter method that follows a certain naming convention:

for a property 'Bar foo', either the getter Bar getFoo() (or isFoo() for boolean properties) or the setter setFoo(Bar) must be present (or both), but there does not have to be a field named "foo". Per convention, there usually is a field with the same name as the property, but it's by no means required.

E.g. the following class (which conforms to the JavaBeans standard) has a bean property "foo" of type Integer, although the underlying field is called iAmNotFoo and is of type 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();
    }
}

We can test this assumption with the following code:

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());
    }
}

Output:

Property: foo, type: class java.lang.Integer
Field: iAmNotFoo, type: class java.lang.String

Properties in Spring

As I said above, Spring uses this exact mechanism to set properties. So when you configure a bean like this

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

"foo" refers to the bean property "foo" and hence to the setter setFoo()

Which makes constructs like the following possible:

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);
    }
}

You can wire this as follows

<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>

As you can see, the setter methods are relevant to Spring, not the actual fields.

So, in JavaBeans speak: Field != Property, although in most cases a field of the same type and name as the property exists.

小猫一只 2024-10-19 23:12:03

您的怀疑是正确的:Spring 将属性名称转换为 setter 方法。

您用作参数的 bean 类型为 DefaultTlsDirContextAuthenticationStrategy,并且该方法接受类型为 DirContextAuthenticationStrategy 的对象,因此 DefaultTlsDirContextAuthenticationStrategy 必须是子类DirContextAuthenticationStrategy 的实现者。

Your suspicion is correct: Spring translates property names to setter methods.

The bean you are using as the argument is of type DefaultTlsDirContextAuthenticationStrategy, and the method accepts an object of type DirContextAuthenticationStrategy, so DefaultTlsDirContextAuthenticationStrategy must be a subclass of implementor of DirContextAuthenticationStrategy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文