Skip to content

Clarify @Bean return type recommendation in case of multiple interfaces [SPR-15973] #20524

@spring-projects-issues

Description

@spring-projects-issues

Michal Domagala opened SPR-15973 and commented

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


Affects: 4.3.11

Referenced from: commits ffe80ff, 963dd3f

Backported to: 4.3.12

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions