Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-15266

Doc: Consistent @Profile declarations on overloaded @Bean methods

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 4.3.6, 5.0 M4
    • Fix Version/s: 4.3.8, 5.0 RC1
    • Component/s: Core:DI
    • Labels:
    • Last commented by a User:
      false

      Description

      When having bean factory methods with the same name but different arguments used for creating instance for different profiles the bean is not recognized for some profiles.
      See example below:

      @Configuration
      public class SomeConfiguration {
      
          @Bean(name = "something")
          @Profile("A")
          public String valueForTest() {
              return "aaa";
          }
      
          @Bean
          public String secondValue() {
              return "bbb";
          }
      
          @Bean(name = "something")
          @Profile("B")
          @Autowired
          public String valueForTest(final String secondValue) {
              return secondValue;
          }
      }
      

      Corresponding test will fail:

      @RunWith(SpringJUnit4ClassRunner.class)
      @ActiveProfiles("A")
      @ContextConfiguration(classes = { SomeConfiguration.class })
      public class SomeConfigurationTest_ProfileA {
      
          @Autowired
          @Qualifier("something")
          private String valueForTest;
      
          @Autowired
          private String secondValue;
      
          @Test
          public void test() {
              Assert.assertEquals("aaa", valueForTest);
          }
      
      }
      

      When using profile "B" dependencies for bean named "something" will be created successfully. Providing string value "bbb".
      For profile "A" it is not working properly.
      Behaviour depends on used Spring version. For releases 4.0.0. up to 4.1.4 outcome will be "bbb".
      I would expect "aaa".

      From release 4.1.5 on (including 5.x.x releases) a NoSuchBeanDefinitionException is thrown.

      See the attached gradle project to evaluate the behaviour.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                juergen.hoeller Juergen Hoeller
                Reporter:
                geoherdt Georg Herdt
                Last updater:
                St├ęphane Nicoll
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  1 year, 13 weeks, 2 days ago