我正在尝试为ldap连接创建两个bean。
在我的context.xml文件中,我创建了两个LdapContextSource,然后使用相同的父类自动连接两个bean,这两个bean的派生类似于spring ldap示例中的PersonDao和PersonDaoImpl。
我一直遇到的问题是,两个bean最终都使用相同的上下文源。甚至在我使用@AutoWired@Qualifier之后。
调试显示,两个单例 bean 看到不同的 LdapContextSource,但最终两个 bean 具有相同的上下文。
是否有一个例子,演示如何通过context.xml和@autowed连接两个ldap?
我可以通过在每个类实例中以编程方式分配 ldapcontextsource 来做到这一点,但我正在尝试通过 @AutoWired 来做到这一点。
编辑:
为了更清楚地说明这一点,我从 https://today.java.net/pub/a/today/2006/04/18/ldaptemplate-java-ldap-made-simple.html 粘贴了一些代码示例 定义与 ldap 连接的 bean 可以定义如下:
<beans>
<context:annotation-config/>
<context:component-scan base-package="the proper location of my code"/>
<bean id="contextSource" class="net.sf.ldaptemplate.support.LdapContextSource">
<property name="url" value="ldap://localhost:389" />
<property name="base" value="dc=jayway,dc=se" />
<property name="userName" value="cn=Manager" />
<property name="password" value="secret" />
</bean>
<bean id="ldapTemplate" class="net.sf.ldaptemplate.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="personDao" class="se.jayway.dao.PersonDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
<bean id="contextSource2" class="net.sf.ldaptemplate.support.LdapContextSource">
<property name="url" value="ldap://localhost:1389" />
<property name="base" value="dc=jayway,dc=se" />
<property name="userName" value="cn=Manager" />
<property name="password" value="secret" />
</bean>
<bean id="ldapTemplate2" class="net.sf.ldaptemplate.LdapTemplate">
<constructor-arg ref="contextSource2" />
</bean>
<bean id="personDao2" class="se.jayway.dao.PersonDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate2" />
</bean>
</beans>
然后将java类定义为:
public class PersonDaoImpl implements PersonDao {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
}
注意,我定义了两个连接。一个用于端口389的ldap,另一个用于1389。然后我使用PersonDaoImpl两次。
问题就在这里。
PersonDao2 在 389 处获得与 PersonDao 相同的连接,而不是它应该获得的 1389。
是什么导致了这种情况?
编辑2:
这里是日志文件的一部分,我认为我们可以在这里看到问题
DEBUG - Creating shared instance of singleton bean 'personDao'
DEBUG - Creating instance of bean 'personDao'
DEBUG - Eagerly caching bean 'personDao' to allow for resolving potential circular references
DEBUG - Creating shared instance of singleton bean 'ldapTemplate'
DEBUG - Creating instance of bean 'ldapTemplate'
DEBUG - Creating shared instance of singleton bean 'contextSource'
DEBUG - Creating instance of bean 'contextSource'
DEBUG - Eagerly caching bean 'contextSource' to allow for resolving potential circular references
DEBUG - Invoking afterPropertiesSet() on bean with name 'contextSource'
DEBUG - AuthenticationSource not set - using default implementation
DEBUG - Not using LDAP pooling
DEBUG - Trying provider Urls: ldap://xxx.xx.xx.158:389/dc=domain,dc=com
DEBUG - Returning cached instance of singleton bean 'org.springframework.security.methodSecurityMetadataSourceAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Finished creating instance of bean 'contextSource'
DEBUG - Eagerly caching bean 'ldapTemplate' to allow for resolving potential circular references
DEBUG - Returning cached instance of singleton bean 'contextSource'
DEBUG - Added autowiring by name from bean name 'ldapTemplate' via property 'contextSource' to bean named 'contextSource'
DEBUG - Invoking afterPropertiesSet() on bean with name 'ldapTemplate'
DEBUG - Returning cached instance of singleton bean 'org.springframework.security.methodSecurityMetadataSourceAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Finished creating instance of bean 'ldapTemplate'
DEBUG - Got Ldap context on server 'ldap://xxx.xx.xx.158:389/dc=domain,dc=com'
DEBUG - Entered setLdapTemplate getContextSource() getReadOnlyContext() dc=domain,dc=com
DEBUG - Entered setLdaptreepathminusroot ou=people
DEBUG - Returning cached instance of singleton bean 'org.springframework.security.methodSecurityMetadataSourceAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Finished creating instance of bean 'personDao'
DEBUG - Creating shared instance of singleton bean 'PersonDao2'
DEBUG - Creating instance of bean 'PersonDao2'
DEBUG - Eagerly caching bean 'PersonDao2' to allow for resolving potential circular references
DEBUG - Creating shared instance of singleton bean 'ldapTemplate2'
DEBUG - Creating instance of bean 'ldapTemplate2'
DEBUG - Creating shared instance of singleton bean 'contextSource2'
DEBUG - Creating instance of bean 'contextSource2'
DEBUG - Eagerly caching bean 'contextSource2' to allow for resolving potential circular references
DEBUG - Invoking afterPropertiesSet() on bean with name 'contextSource2'
DEBUG - AuthenticationSource not set - using default implementation
DEBUG - Not using LDAP pooling
DEBUG - Trying provider Urls: ldap://xxx.xx.xx.147:389/dc=directory,dc=domain2,dc=com
DEBUG - Returning cached instance of singleton bean 'org.springframework.security.methodSecurityMetadataSourceAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Finished creating instance of bean 'contextSource2'
DEBUG - Eagerly caching bean 'ldapTemplate2' to allow for resolving potential circular references
DEBUG - Returning cached instance of singleton bean 'contextSource'
DEBUG - Added autowiring by name from bean name 'ldapTemplate2' via property 'contextSource' to bean named 'contextSource'
DEBUG - Invoking afterPropertiesSet() on bean with name 'ldapTemplate2'
DEBUG - Returning cached instance of singleton bean 'org.springframework.security.methodSecurityMetadataSourceAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Finished creating instance of bean 'ldapTemplate2'
DEBUG - Got Ldap context on server 'ldap://xxx.xx.xx.158:389/dc=domain,dc=com'
DEBUG - Entered setLdapTemplate getContextSource() getReadOnlyContext() dc=domain,dc=com
DEBUG - Entered setLdaptreepathminusroot ou=individualprovider
DEBUG - Returning cached instance of singleton bean 'org.springframework.security.methodSecurityMetadataSourceAdvisor'
DEBUG - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG - Finished creating instance of bean 'PersonDao2'
在我看来,相关的两行是
DEBUG - Added autowiring by name from bean name 'ldapTemplate' via property 'contextSource' to bean named 'contextSource'
然后呢?
DEBUG - Added autowiring by name from bean name 'ldapTemplate2' via property 'contextSource' to bean named 'contextSource'
请注意,这两个都得到了
via property 'contextSource' to bean named 'contextSource'
ldapTemplate2应该得到:
via property 'contextSource' to bean named 'contextSource2'
更新:我被要求提供用于连接到我的类的代码。
第一种方式是这样的:
@Controller
public class ManageAccountsController{
private PersonDaoImpl personDao2;
}
通过这种方式,我没有初始化personDao2
第二种方法是这样的:
@Controller
public class ManageAccountsController{
@Autowired PersonDaoImpl personDao2;
}
第三种方法是这样的:
@Controller
public class ManageAccountsController{
@Autowired @Qualifier("personDao2") PersonDaoImpl personDao2;
}
这最后两个方法我得到了错误的上下文源。人道的语境来源。