[英]Two beans with same name in different contexts
I have the following beans in my contexs: 我的顶点中包含以下bean:
<!-- Context 1 -->
<beans profile="ldap">
<bean id="activeDirectoryAuthProvider" class="com.production.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
</beans>
<!-- Context 2 -->
<bean id="activeDirectoryAuthProvider" class="com.test.TestActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
My goal is to use the first bean only for production version another one for test purposes. 我的目标是仅将第一个bean用于生产版本,将另一个bean用于测试目的。
Namely when I start test based on production context I expect that production bean would be replaced by test bean with needed configuration. 也就是说,当我基于生产环境开始测试时,我期望生产Bean将被具有所需配置的测试Bean取代。
But unfortunately when I tried to create two beans with same name only production bean is created and another one is ignored. 但是不幸的是,当我尝试创建两个具有相同名称的bean时,只会创建生产bean,而另一个将被忽略。 Another thing that I noticed that when I tried to change test bean name to: activeDirectoryAuthProvider1 then both beans are successfully created.
我注意到的另一件事是,当我尝试将测试bean名称更改为: activeDirectoryAuthProvider1时,两个bean均成功创建。 Can anyone explain why it happen and suggest possible solution how it can be bypassed?
谁能解释为什么会发生并提出可能的解决方案如何绕开它?
You need to use different contexts for development and production. 您需要使用不同的上下文进行开发和生产。 In each context you define only the relevant bean (ie only 1 bean with a certain name).
在每个上下文中,您仅定义相关的bean(即,只有一个具有特定名称的bean)。 If you use maven you can put the test/development context under src/test/resources and the production context under src/main/resources
如果使用Maven,则可以将测试/开发上下文放在src / test / resources下,将生产上下文放在src / main / resources下
If you do not use maven there are other approaches. 如果您不使用Maven,则还有其他方法。 You can find an example here: http://mrhaki.blogspot.it/2009/02/use-spring-configurator-to-support.html
您可以在此处找到示例: http : //mrhaki.blogspot.it/2009/02/use-spring-configurator-to-support.html
Take a look at Spring Profiles you can have one for test and one for prod. 看一下Spring Profiles,您可以选择其中一个进行测试,而其中一个进行测试。
<beans profile="test">
<!-- Context 1 -->
<bean id="activeDirectoryAuthProvider" class="com.production.ActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
</beans>
<beans profile="prod">
<!-- Context 2 -->
<bean id="activeDirectoryAuthProvider" class="com.test.TestActiveDirectoryLdapAuthenticationProvider">
<constructor-arg value="${ldap.login.provider.domain}"/>
<constructor-arg value="${ldap.login.provider.url}" />
<property name="useAuthenticationRequestCredentials" value="true" />
<property name="convertSubErrorCodesToExceptions" value="true" />
</bean>
</beans>
You can set the active profile in a various ways. 您可以通过多种方式设置活动配置文件。 Check the docs.
检查文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.