XML p-namespace 属性的等效 Java 配置是什么?
What is the equivalent Java configuration for an XML p-namespace attribute?
例如,如何将以下内容转换为 JavaConfig @Bean
?
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl" p:ldapUrl-ref="ldapHost"/>
这是我使用等效的 JavaConfig Bean 的地方
@Bean(name="ldapDao")
public LdapDAOImpl getLdapDAOImpl(){
LdapDAOImpl ldapDAOImpl = new LdapDAOImpl();
//How can I set the reference here to ldapHost?
return new LdapDAOImpl();
}
看起来 p:ldapUrl-ref 只是在我的 LdapDAOImpl class 中设置 ldapUrl 的值。如此简单 setter 工作得很好。
@Bean(name="ldapDao")
public LdapDAOImpl getLdapDAOImpl(){
LdapDAOImpl ldapDAOImpl = new LdapDAOImpl();
ldapDAOImpl.setLdapUrl(url);
return new LdapDAOImpl();
}
首先,p-namespace
定义为
The p-namespace
enables you to use the bean element’s attributes,
instead of nested <property/>
elements, to describe your property
values and/or collaborating beans.
换句话说,它只是定义 bean 属性的替代方法。
例如,
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl" p:ldapUrl-ref="ldapHost"/>
等同于
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl">
<property name="ldapUrl" ref="ldapHost" />
</bean>
p:
属性中的 -ref
后缀在文档中用示例进行了解释
As you can see, this example includes not only a property value using
the p-namespace, but also uses a special format to declare property
references. Whereas the first bean definition uses <property name="spouse" ref="jane"/>
to create a reference from bean john to
bean jane, the second bean definition uses p:spouse-ref="jane"
as an
attribute to do the exact same thing. In this case spouse is the
property name, whereas the -ref
part indicates that this is not a
straight value but rather a reference to another bean.
您的 <bean>
定义中出现的每个 property
元素都需要 bean class 中的相应 setter。
鉴于以上所有内容,相应的 @Bean
定义将初始化类型 org.mycompany.LdapDAOImpl
的对象并使用对应的对象调用其 setLdapUrl
setter名为 ldapHost
的 bean 作为参数。
例如,假设你有这样一个bean
@Bean
public LdapHost ldapHost() {
return new LdapHost();
}
然后您将使用它来初始化您的 ldapDao
@Bean
public LdapDaoImpl ldapDao() {
LdapDaoImpl ldapDao = new LdapDaoImpl();
ldapDao.setLdapUrl(ldapHost());
return ldapDao;
}
或者,您可以让 Spring 为您注入它。
@Bean
public LdapDaoImpl ldapDao(LdapHost ldapHost) {
LdapDaoImpl ldapDao = new LdapDaoImpl();
ldapDao.setLdapUrl(ldapHost);
return ldapDao;
}
例如,如何将以下内容转换为 JavaConfig @Bean
?
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl" p:ldapUrl-ref="ldapHost"/>
这是我使用等效的 JavaConfig Bean 的地方
@Bean(name="ldapDao")
public LdapDAOImpl getLdapDAOImpl(){
LdapDAOImpl ldapDAOImpl = new LdapDAOImpl();
//How can I set the reference here to ldapHost?
return new LdapDAOImpl();
}
看起来 p:ldapUrl-ref 只是在我的 LdapDAOImpl class 中设置 ldapUrl 的值。如此简单 setter 工作得很好。
@Bean(name="ldapDao")
public LdapDAOImpl getLdapDAOImpl(){
LdapDAOImpl ldapDAOImpl = new LdapDAOImpl();
ldapDAOImpl.setLdapUrl(url);
return new LdapDAOImpl();
}
首先,p-namespace
定义为
The
p-namespace
enables you to use the bean element’s attributes, instead of nested<property/>
elements, to describe your property values and/or collaborating beans.
换句话说,它只是定义 bean 属性的替代方法。
例如,
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl" p:ldapUrl-ref="ldapHost"/>
等同于
<bean id="ldapDao" class="org.mycompany.LdapDAOImpl">
<property name="ldapUrl" ref="ldapHost" />
</bean>
p:
属性中的 -ref
后缀在文档中用示例进行了解释
As you can see, this example includes not only a property value using the p-namespace, but also uses a special format to declare property references. Whereas the first bean definition uses
<property name="spouse" ref="jane"/>
to create a reference from bean john to bean jane, the second bean definition usesp:spouse-ref="jane"
as an attribute to do the exact same thing. In this case spouse is the property name, whereas the-ref
part indicates that this is not a straight value but rather a reference to another bean.
您的 <bean>
定义中出现的每个 property
元素都需要 bean class 中的相应 setter。
鉴于以上所有内容,相应的 @Bean
定义将初始化类型 org.mycompany.LdapDAOImpl
的对象并使用对应的对象调用其 setLdapUrl
setter名为 ldapHost
的 bean 作为参数。
例如,假设你有这样一个bean
@Bean
public LdapHost ldapHost() {
return new LdapHost();
}
然后您将使用它来初始化您的 ldapDao
@Bean
public LdapDaoImpl ldapDao() {
LdapDaoImpl ldapDao = new LdapDaoImpl();
ldapDao.setLdapUrl(ldapHost());
return ldapDao;
}
或者,您可以让 Spring 为您注入它。
@Bean
public LdapDaoImpl ldapDao(LdapHost ldapHost) {
LdapDaoImpl ldapDao = new LdapDaoImpl();
ldapDao.setLdapUrl(ldapHost);
return ldapDao;
}