Skip to content
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

Ordering inconsistency with beans defined in parent context #29105

Closed
bclozel opened this issue Sep 8, 2022 · 3 comments
Closed

Ordering inconsistency with beans defined in parent context #29105

bclozel opened this issue Sep 8, 2022 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@bclozel
Copy link
Member

bclozel commented Sep 8, 2022

I came across a case where beans resolved with context.getBeanProvider(MyService.class) are not ordered as they should, when:

  • there is a parent/child context setup
  • beans are ordered with @Order

The following sample application reproduces the problem

public class ContextTest {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
		parent.register(DefaultConfig.class);
		parent.register(CustomService.class);

		AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
		child.setParent(parent);
		child.register(DefaultConfig.class);

		parent.refresh();
		child.refresh();

		child.getBeanProvider(MyService.class).orderedStream().forEach(service -> {
			System.out.println(service.getClass().getSimpleName());
			// CustomService (order -1) should be before DefaultService (order 0)
			// prints beans in the wrong order: DefaultService, CustomService
		});
	}

	interface MyService { }

	static class CustomService implements MyService { }

	static class DefaultService implements MyService { }

	@Configuration
	static class CustomConfig {

		@Bean
		@Order(-1)
		CustomService customService() {
			return new CustomService();
		}

	}

	@Configuration
	static class DefaultConfig {

		@Bean
		@Order(0)
		DefaultService defaultService() {
			return new DefaultService();
		}

	}
}

Note that this problem doesn't happen if beans implement Ordered instead of being annotated with @Order.

@bclozel bclozel added status: waiting-for-triage An issue we've not yet triaged or decided on type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) labels Sep 8, 2022
@snicoll
Copy link
Member

snicoll commented Sep 8, 2022

So the problem is that FactoryAwareOrderSourceProvider has a check to verify that the instance is present in the local bean factory:

In the case of the CustomService, it is only defined in the parent so that check fails and no source is made available. I wonder if that's an oversight, considering that getMergedLocalBeanDefinition looks up in the parent.

@snicoll snicoll removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 9, 2022
@snicoll snicoll added this to the 5.3.x milestone Sep 9, 2022
@snicoll snicoll self-assigned this Sep 9, 2022
snicoll added a commit to snicoll/spring-framework that referenced this issue Sep 9, 2022
@snicoll
Copy link
Member

snicoll commented Sep 9, 2022

I've looked at this one with @jhoeller and we believe that looking the bean definition in the parent is what we should be doing for consistency.

Current status is available in snicoll@e425825.

@rishiraj88
Copy link

Very interesting issue! 👍

@snicoll snicoll modified the milestones: 5.3.x, 5.3.23 Sep 13, 2022
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) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants