Spring/LDAP - 在 bean 配置中调用 setter 方法
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您误解了 JavaBean 上下文中“属性”一词的含义。
Bean 属性与字段
JavaBeans 标准(Spring 密切遵循)将 Bean 属性定义为具有遵循特定命名约定的 Getter 方法和/或 Setter 方法的东西:
对于属性“Bar foo”,可以是 getter
Bar getFoo()
(或布尔属性的isFoo()
)或 settersetFoo(Bar)
必须存在(或两者都存在),但确实存在不必是名为“foo”的字段。按照约定,通常有一个与属性同名的字段,但这绝不是必需的。例如,下面的类(符合 JavaBeans 标准)具有 Integer 类型的 bean 属性“foo”,尽管底层字段名为
iAmNotFoo
并且是 String 类型。我们可以使用以下代码来测试这个假设:
输出:
Spring 中的属性
正如我上面所说,Spring 使用这种确切的机制来设置属性。因此,当您配置像这样的 bean 时,
“foo”引用 bean 属性“foo”,从而引用 setter
setFoo()
这使得如下的构造成为可能:
方式连接它
您可以按如下 可以看到,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()
(orisFoo()
for boolean properties) or the settersetFoo(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.We can test this assumption with the following code:
Output:
Properties in Spring
As I said above, Spring uses this exact mechanism to set properties. So when you configure a bean like this
"foo" refers to the bean property "foo" and hence to the setter
setFoo()
Which makes constructs like the following possible:
You can wire this as follows
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.
您的怀疑是正确的: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 typeDirContextAuthenticationStrategy
, soDefaultTlsDirContextAuthenticationStrategy
must be a subclass of implementor ofDirContextAuthenticationStrategy
.