Skip to content

Overloaded @Bean methods in @Configration class are processed in inconsistent order #31906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
littlesky007 opened this issue Dec 25, 2023 · 3 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@littlesky007
Copy link

littlesky007 commented Dec 25, 2023

for example:

@Configuration
public class MyConfigurauion {

	@Bean
	@Lazy
	public Mytest mytest() {
		System.out.println("lazy");
		return new Mytest();
	}

	@Bean
	public Mytest mytest(MyConfigurauion m) {
		System.out.println("not Lazy");
		return new Mytest();
	}

	public static void main(String[] args) {
		AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext();
		app.setAllowBeanDefinitionOverriding(false);
		app.register(MyConfigurauion.class);

		app.refresh();
		MyConfigurauion bean = app.getBean(MyConfigurauion.class);
		System.out.println("init done");
		bean.mytest();
	}
}

spring analysis @Bean in retrieveBeanMethodMetadata method, Set<MethodMetadata> beanMethods = original.getAnnotatedMethods(Bean.class.getName()) line disorder,so get different result;

@littlesky007
Copy link
Author

spring version 5.3.29

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 25, 2023
@snicoll
Copy link
Member

snicoll commented Dec 26, 2023

I don't know what you mean by disorder. Perhaps you're expecting that calling mytest with no argument is going to return you the @Lazy version. This configuration isn't configuring two beans, it's the same bean with two factory methods and the container chooses the one with the most argument it can honor. In this case, the method that takes the configuration will be chosen to create the bean.

Having multiple factory methods for the same bean is actually going to be deprecated in 6.2.x, see #31073.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Dec 26, 2023
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 26, 2023
@littlesky007
Copy link
Author

yes, but there also has a problem. the result is not regular, it maybe 'init done/n/r not lazy',and it also maybe 'not lazy /n/r init done'. I think retrieveBeanMethodMetadata(SourceClass sourceClass) has a little bug in Set beanMethods = original.getAnnotatedMethods(Bean.class.getName()); because Class.getDeclareMethods is disorder.

@sbrannen sbrannen changed the title the configration class by @Configration have overload methos by @Bean,spring read those methods disorder Overloaded @Bean methods in @Configration class are processed in inconsistent order Dec 27, 2023
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants