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

@Order does not work on (CommandLine|Application)Runner @Bean methods #37905

Closed
wants to merge 1 commit into from

Conversation

ttddyy
Copy link
Contributor

@ttddyy ttddyy commented Oct 17, 2023

Currently, when the order of an CommandLineRunner or ApplicationRunner bean is defined on the bean method it is not taken into account.

@Bean
@Order(10)
CommandLineRunner runner() {
  return (args) -> ...;
}

This is because SpringApplication sorts them by actual beans and does not consider the bean definitions.
This behavior may be due to the fact that the ordering is determined collectively for both CommandLineRunner and ApplicationRunner.
In other words, when a CommandLineRunner has order=1 and an ApplicationRunner has order=2, the CommandLineRunner runs first, even though they are two different classes.

The BeanFactory does not provide an API to retrieve ordered beans for two distinct types of beans. This is why it is currently limited to sorting them based solely on bean instances and not on their bean definitions.

This PR introduces a sealed marker interface for ApplicationRunner and CommandLineRunner.
Then, using the interface to retrieve the beans in an ordered fashion, it takes into account the @Order on the bean definition(@Bean method in this case).
This allows for the use of @Order on @Bean methods for both CommandLineRunner and ApplicationRunner.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 17, 2023
Prior to this commit, `@Order` annotation on `@Bean` method was not
considered for `CommandLineRunner` and `ApplicationRunner`.

This commit introduces a `Runner` marker interface and uses it to
retrieve the runner beans. As a result, it enables the use of `@Order`
annotations on `@Bean` methods for both `CommandLineRunner` and
`ApplicationRunner`.

Signed-off-by: Tadaya Tsuyukubo <tadaya@ttddyy.net>
@wilkinsona wilkinsona changed the title Support @Order on functional CommandLine/Application Runner bean definition Support @Order on (CommandLine|Application)Runner @Bean methods Oct 17, 2023
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 18, 2023
@philwebb philwebb added this to the 2.7.x milestone Oct 18, 2023
@wilkinsona wilkinsona changed the title Support @Order on (CommandLine|Application)Runner @Bean methods @Order does not work on (CommandLine|Application)Runner @Bean methods Oct 18, 2023
@wilkinsona wilkinsona modified the milestones: 2.7.x, 2.7.17 Oct 18, 2023
wilkinsona pushed a commit that referenced this pull request Oct 18, 2023
Prior to this commit, `@Order` annotation on `@Bean` method was not
considered for `CommandLineRunner` and `ApplicationRunner`.

This commit introduces a `Runner` marker interface and uses it to
retrieve the runner beans. As a result, it enables the use of `@Order`
annotations on `@Bean` methods for both `CommandLineRunner` and
`ApplicationRunner`.

Signed-off-by: Tadaya Tsuyukubo <tadaya@ttddyy.net>

See gh-37905
wilkinsona added a commit that referenced this pull request Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants