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

Clarify @Bean return type recommendation in case of multiple interfaces

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 4.3.11
    • Fix Version/s: 4.3.12, 5.0 GA
    • Component/s: Core:DI
    • Labels:
    • Last commented by a User:
      true

      Description

      I discovered than bean graph may depend on order of bean declaration. I always thought that assembly algorithm analyze injections and determine order of beans creation.

      However, example below shows that bean graph depend on order of declaration.

      public static void main(String[] args) {
      		SpringApplication.run(MyApp.class, args);
      	}
      	
      	public static interface A {}
      	
      	public static interface B {}
      	
      	public static interface C {}
      	
      	public static class ABC implements A,B,C {}
      	
      	@Bean Object checkB(@Autowired(required = false) B b) {
      		System.out.println("B is " + b);
      		return new Object();
      	}
      	
      	@Bean A a() {
      		System.out.println("abc");
      		return new ABC();
      	}
      	
      	@Bean Object checkC(@Autowired(required = false) C c) {
      		System.out.println("C is " + c);
      		return new Object();
      	}
      

      Actual:

      B is null
      abc
      C is com.sixdegreeshq.MyApp$ABC@17f7cd29

      Expected:
      Rather

      B is null
      abc
      C is null

      but without a doubt B and C should be symmetric

        Attachments

          Activity

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              michaldo Michal Domagala
              Last updater:
              Spring Issuemaster
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                27 weeks ago