Spring 豆间注射不正确
我正在尝试为ldap连接创建两个bean 在context.xml文件中,我创建了两个LdapContextSource,然后自动连接两个具有相同父类的bean,就像SpringLDAP示例中的PersonDao和PersonDaoImpl一样派生 我经常遇到的问题是,两个bean最终都使用相同的上下文源。甚至在我使用@AutoWired@限定符之后 调试表明,两个单例bean看到的是不同的LdapContextSource,但最终这两个bean具有相同的上下文 是否有一个示例演示了如何通过context.xml和@autowired方法连接两个ldap连接? 我可以通过编程方式在每个类实例中分配ldapcontextsource来实现这一点,但我正试图通过@AutoWired实现这一点 编辑: 为了让这更清楚,我从中粘贴了一些代码示例 定义ldap连接的bean可以定义如下:Spring 豆间注射不正确,spring,spring-mvc,spring-ldap,Spring,Spring Mvc,Spring Ldap,我正在尝试为ldap连接创建两个bean 在context.xml文件中,我创建了两个LdapContextSource,然后自动连接两个具有相同父类的bean,就像SpringLDAP示例中的PersonDao和PersonDaoImpl一样派生 我经常遇到的问题是,两个bean最终都使用相同的上下文源。甚至在我使用@AutoWired@限定符之后 调试表明,两个单例bean看到的是不同的LdapContextSource,但最终这两个bean具有相同的上下文 是否有一个示例演示了如何通过co
<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>
请注意,我定义了两个连接。一个用于端口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'
及
请注意,这两个都是
via property 'contextSource' to bean named 'contextSource'
ldapTemplate2应获得:
via property 'contextSource' to bean named 'contextSource2'
更新:
我被要求提供我用来连接到我的类的代码
第一种方式是:
@Controller
public class ManageAccountsController{
private PersonDaoImpl personDao2;
}
@Controller
public class ManageAccountsController{
@Autowired PersonDaoImpl personDao2;
}
@Controller
public class ManageAccountsController{
@Autowired @Qualifier("personDao2") PersonDaoImpl personDao2;
}
使用这种方法,我没有初始化personDao2
第二种方式是:
@Controller
public class ManageAccountsController{
private PersonDaoImpl personDao2;
}
@Controller
public class ManageAccountsController{
@Autowired PersonDaoImpl personDao2;
}
@Controller
public class ManageAccountsController{
@Autowired @Qualifier("personDao2") PersonDaoImpl personDao2;
}
第三种方式是:
@Controller
public class ManageAccountsController{
private PersonDaoImpl personDao2;
}
@Controller
public class ManageAccountsController{
@Autowired PersonDaoImpl personDao2;
}
@Controller
public class ManageAccountsController{
@Autowired @Qualifier("personDao2") PersonDaoImpl personDao2;
}
最后两种方法我得到了不正确的contextSource。personDao的contextSource。所以听起来像是用xml显式地连接了
net.sf.ldaptemplate.ldaptemplate
,但是在ldaptemplate
中的contextSource
属性上也有一个@Autowired
注释,我相信这就是问题所在。我建议您继续删除LdapTemplate
类上的@Autowired
,这样只有基于xml的连接才会生效 所以听起来像是用xml显式地连接了net.sf.ldaptemplate.ldaptemplate
,但是在ldaptemplate
中的contextSource
属性上还有一个@Autowired
注释,我认为这就是问题所在。我建议您继续删除LdapTemplate
类上的@Autowired
,这样只有基于xml的连接才会生效 所以听起来像是用xml显式地连接了net.sf.ldaptemplate.ldaptemplate
,但是在ldaptemplate
中的contextSource
属性上还有一个@Autowired
注释,我认为这就是问题所在。我建议您继续删除LdapTemplate
类上的@Autowired
,这样只有基于xml的连接才会生效 所以听起来像是用xml显式地连接了net.sf.ldaptemplate.ldaptemplate
,但是在ldaptemplate
中的contextSource
属性上还有一个@Autowired
注释,我认为这就是问题所在。我建议您继续删除LdapTemplate
类上的@Autowired
,这样只有基于xml的连接才会生效 oh okie..据我所知,@qualifier
在构造函数注入方面有一些问题。。
您是否可以尝试使用单个注释@Resource(name=“personDao2”)
而不是
@Autowired
@Qualifier("personDao2")
…让我知道结果。哦,好的。
@qualifier
在构造函数注入方面有一些问题。。
您是否可以尝试使用单个注释@Resource(name=“personDao2”)
而不是
@Autowired
@Qualifier("personDao2")
…让我知道结果。哦,好的。
@qualifier
在构造函数注入方面有一些问题。。
您是否可以尝试使用单个注释@Resource(name=“personDao2”)
而不是
@Autowired
@Qualifier("personDao2")
…让我知道结果。哦,好的。
@qualifier
在构造函数注入方面有一些问题。。
您是否可以尝试使用单个注释@Resource(name=“personDao2”)
而不是
@Autowired
@Qualifier("personDao2")
…让我知道结果。我终于找到了解决办法 我必须明确定义contextSource,例如
<bean id="ldapTemplate2" class="net.sf.ldaptemplate.LdapTemplate">
<property name="contextSource" ref="contextSource2" />
</bean>
我终于找到了解决办法 我必须明确定义contextSource,例如
<bean id="ldapTemplate2" class="net.sf.ldaptemplate.LdapTemplate">
<property name="contextSource" ref="contextSource2" />
</bean>
我终于找到了解决办法 我必须明确定义contextSource,例如
<bean id="ldapTemplate2" class="net.sf.ldaptemplate.LdapTemplate">
<property name="contextSource" ref="contextSource2" />
</bean>
我终于找到了解决办法 我必须明确定义contextSource,例如
<bean id="ldapTemplate2" class="net.sf.ldaptemplate.LdapTemplate">
<property name="contextSource" ref="contextSource2" />
</bean>
你能展示一下你是如何为DAO2进行自动连接的吗?我已经更新了这个问题,以展示DAO2是如何连接的。你没有第二种方式的异常吗..因为自动连接是按类型进行的,但它有不止一种类型?正确。没有例外。我同意你的看法。这就是为什么我添加了@qualifier。你能展示一下你是如何为DAO2自动连接的吗?我已经更新了这个问题,以展示DAO2是如何连接的。你没有得到第二种方式的异常吗..因为自动连接是按类型进行的,但它有不止一种类型?正确。没有例外。我同意你的看法。这就是为什么我添加了@qualifier。你能展示一下你是如何为DAO2自动连接的吗?我已经更新了这个问题,以展示DAO2是如何连接的。你没有得到第二种方式的异常吗..因为自动连接是按类型进行的,但它有不止一种类型?正确。没有例外。我同意你的看法。这就是为什么我添加了@qualifier。你能展示一下你是如何为DAO2自动连线的吗?我已经更新了问题t