豆之间的注射不正确(Incorrect injection between beans)

2019-11-03 03:58发布

我试图创建两个豆LDAP连接。

在我的context.xml文件我创建两个LdapContextSource然后自动线两个bean具有相同的父类,在春天LDAP例子派生类的PersonDao的和PersonDaoImpl。

我不断收到的问题是,无论是豆类结束了相同的上下文源。 即使我使用@Autowired @Qualifier。

调试表明,无论单豆类看到一个不同的LdapContextSource但最终都豆有相同的背景。

有一个例子某处演示如何通过context.xml中和@Autowired方法连线两个LDAP连接?

我可以通过编程方式分配在每个类实例ldapcontextsource,但我想通过@Autowired做到这一点做到这一点而已。

编辑:

为了使这个更清楚我粘贴一些代码样本https://today.java.net/pub/a/today/2006/04/18/ldaptemplate-java-ldap-made-simple.html豆来定义的连接LDAP可被定义为如下:

<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;
}

最后这2种方法我得到不正确的好的ContextSource。 的PersonDAO的好的ContextSource。

Answer 1:

哦okie..as我知道@qualifier有一些问题constrcutor注射..你可以尝试单注释@Resource(name="personDao2")而不是

 @Autowired
 @Qualifier("personDao2")

...让我知道结果..



Answer 2:

所以,它听起来就像你有你的有线net.sf.ldaptemplate.LdapTemplate在XML中明确,但你也有@Autowired的注解contextSource物业LdapTemplate ,这个我认为这是问题。 我建议你继续和删除@Autowired上共LdapTemplate类,这种方式仅基于XML的布线生效。



Answer 3:

我终于找到了解决办法。

我必须明确地定义好的ContextSource如

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


文章来源: Incorrect injection between beans