2017-07-22 69 views
0

我正在嘗試混合使用xml和Java配置。xml資源和Java配置之間的彈簧配置拆分

我有一個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配置?或者這些參考文獻是一個問題。

謝謝

+0

春季安全假設Java配置信息,並將該[交](http://www.baeldung.com/spring-security-ldap)可能會有所幫助。更多信息請參見[documentation](https://docs.spring.io/spring-security/sit e/docs/current/reference/html/jc.html) –

回答

0

可以將其移動到Java配置 申報配置類

@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"/> 
+0

謝謝!這有助於 –

+0

不客氣! – fg78nc