Spring / LDAP - calling setter methods in beans configuration

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

setAuthenticationStrategy(
    DirContextAuthenticationStrategy authenticationStrategy
)

To call 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, what exactly defines a method call setAuthenticationStrategy? Is this my property name authenticationStrategy? Does Spring automatically translate property names into the appropriate configuration method?

+3
source share
2 answers

Your suspicion is true: Spring translates property names into setter methods.

bean, DefaultTlsDirContextAuthenticationStrategy, DirContextAuthenticationStrategy, DefaultTlsDirContextAuthenticationStrategy DirContextAuthenticationStrategy.

+2

, "" JavaBean.

Bean

JavaBeans ( Spring) Bean , Getter / Setter, :

'Bar foo', getter Bar getFoo() ( isFoo() ), setter setFoo(Bar) ( ), "Foo". , , .

. ( JavaBeans) Bean "foo" Integer, 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, type: class java.lang.Integer
: iAmNotFoo, type: class java.lang.String

Spring

, Spring . , Bean,

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

"foo" Bean "foo" , , 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>

, Spring, .

, JavaBeans : Field!= , , .

+5

Source: https://habr.com/ru/post/1786984/


All Articles