简体   繁体   English

在不同上下文中具有相同名称的两个bean

[英]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.

相关问题 在春季,如何定义两个具有相同名称和不同类的Bean - In Spring, How to Define Two Beans, with Same Name and Different Class 2个同名但不同的bean,如何自动装配? - 2 beans with same name but different package, how to autowire? 自动装配同一类的两个不同的bean - Autowiring two different beans of same class @ConfigurationProperties 两个不同 bean 上 DB 的相同前缀 - @ConfigurationProperties same prefix for DB on two different beans 具有相同类名(不同包)的两个Spring Service Bean即使使用限定符也会抛出Error - Two Spring Service Beans with same Class Name (different package) are throwing Error even with qualifier AppContext中具有相同名称和相同类的两个bean(一个被嘲笑) - Two beans (one being mocked) of same name and same class in AppContext 是否可以创建多个同名的 spring bean(来自不同的接口)? - is it possible to create multiple spring beans (from different interfaces) with the same name? 为不同的配置文件定义具有相同方法名称的 Spring Bean - Defining Spring Beans with same method name for different profiles 当找到两个同名的bean时,Spring会做什么? - What Spring does when it finds two beans with same name? 尽管使用了@Primary,但两个具有相同名称的bean会导致ConflictingBeanDefinitionException - Two beans with the same name results in ConflictingBeanDefinitionException despite using @Primary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM