Skip to content

Support for @Order at the @Bean method level [SPR-11310] #15934

Closed

Description

@spring-projects-issues

Shaozhen Ding opened SPR-11310 and commented

This won't order the wired list. Since AnnotationAwareOrderComparator only scan the Type annotations instead of Method annotations.

@Bean
@Order(2)
public Store getStore2(){
	return new Store();
}

@Bean
@Order(1)
public Store getStore1(){
	return new Store();
}

@Bean
    @Order(3)
public Store getStore3(){
	return new Store();
}

}

@Autowired
List<Store> stores;

Affects: 4.0 GA

Issue Links:

Referenced from: commits 001d0e7, c6d29f1

Activity

  1. spring-projects-issues commented on Jan 14, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    I understand the confusion but @Order has a specific note about this use case. In your case, you want a different priority for the same Class. Your Store class should implement org.springframework.core.Ordered so that you can set the order instance per instance, e.g.

    @Bean
    public Store getStore3() {
      return new Store(3);
    }

    Hope that helps.

  2. spring-projects-issues commented on Jan 14, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Indeed, this is a traditional @Order limitation. Ordering injected bean arrays and collections based on such order values is a new feature in 4.0 now, but basically inherits that AnnotationAwareOrderComparator limitation.

    That said, with a little bit of extra context for the comparator, we should be able to respect @Order values on factory methods as well. Let's research this for 4.1.

    Juergen

  3. spring-projects-issues commented on Jun 4, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Stéphane Nicoll commented

    This is available on master in the next hour or so in 4.1.0.BUILD-SNAPSHOT for you to try.

    Introduced OrderProvider and OrderProviderComparator, the latter allowing to sort a collection of elements according to a given OrderProvider instance.

    In practice, we have a specific OrderProvider implementation that is able to retrieve the bean definition of a given value, checking on its factory method, if any, if an order has been specified. Such @Order declaration takes precedence and can be specified in a meta-annotation if necessary.

  4. added
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions