在xml资源和Java配置

时间:2017-07-22 01:08:04

标签: spring spring-boot

我正在尝试混合使用xml和Java Configuration。

我有一个 spring-security.xml 资源,我在应用程序启动时导入。

说这是初始xml的一部分:

<bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
    <property name="url"  value="${ldap.url}" />
    <property name="base" value="${ldap.base}" />
    <property name="userDn" value="${ldap.user}" />
    <property name="password" value="${ldap.password}" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
    <constructor-arg ref="ldapContextSource" />
</bean>

我可以将此部分移动为Java配置吗?或者引用是一个问题。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将其移至Java Config 声明配置类

@Configuration
public class AppConfig {
    @Bean
    public LdapContextSource ldapContextSource(){
        LdapContextSource lcontext = new LdapContextSource();
        lcontext.setUrl("${ldap.url}");
        lcontext.setBase("${ldap.base}");
        lcontext.setUserDn("${ldap.user}");
        lcontext.setPassword("${ldap.password}");
        return lcontext;
    }

    @Bean
    public LdapTemplate LdapTemplate(){
        LdapTemplate lTemplate = new LdapTemplate(ldapContextSource());
        return lTemplate;
    }    
}

在您的XML添加

<context:annotation-config/>  
<bean class="com.mypackage.AppConfig"/>
相关问题