bean之间的注入不正确

时间:2014-04-24 20:07:47

标签: spring spring-mvc spring-ldap

我正在尝试为ldap连接创建两个bean。

在我的context.xml文件中,我创建了两个LdapContextSource,然后使用相同的父类自动连接两个bean,派生类似于spring ldap示例中的PersonDao和PersonDaoImpl。

我不断得到的问题是两个bean都以相同的上下文源结束。甚至在我使用@AutoWired @Qualifier之后。

调试显示两个单例bean都看到不同的LdapContextSource,但最后两个bean都有相同的上下文。

是否有某个示例演示了如何通过context.xml和@autowired方法连接两个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;
}

最后两种方法我得到了不正确的contextSource。 personDao的contextSource。

3 个答案:

答案 0 :(得分:2)

哦,好吧..我知道@qualifier有一些构造函数注入的问题.. 你可以尝试单注释@Resource(name="personDao2")而不是

 @Autowired
 @Qualifier("personDao2")

...让我知道结果..

答案 1 :(得分:1)

所以听起来你已经在{xml}中明确地连接了net.sf.ldaptemplate.LdapTemplate,但是你在@Autowired的{​​{1}}属性上也有一个contextSource注释,我相信是问题。我建议你继续在LdapTemplate类上完全删除@Autowired,这样只有基于xml的连线才会生效。

答案 2 :(得分:0)

我终于找到了解决方案。

我必须明确定义contextSource,例如

<bean id="ldapTemplate2" class="net.sf.ldaptemplate.LdapTemplate">
    <property name="contextSource" ref="contextSource2" />
</bean>