Skip to content

Provide programmatical means to get beans of type in order #24882

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
jxblum opened this issue Apr 8, 2020 · 3 comments
Closed

Provide programmatical means to get beans of type in order #24882

jxblum opened this issue Apr 8, 2020 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@jxblum
Copy link

jxblum commented Apr 8, 2020

Currently, the Spring Framework API (e.g. ListableBeanFactory) provides no programmatic means to acquire beans in order.

It is possible, however, to acquire beans in order, for beans in/directly annotated with the @Order annotation or beans in/directly implementing the Ordered interface, using dependency injection (DI) with the @Autowired or @Resource annotations.

For example, given the following bean classes...

class SomeBean { ... }

class X extends SomeBean implements Ordered {

  @Override
  public int getOrder() {
    return -1;
}

@Order(-2)
class Y extends SomeBean { }

class Z extends ZeroOrderedBean { }

@Order(0)
abstract class ZeroOrderedBean extends SomeBean { }

And configuration...

@Configuration
class MyConfiguration {

  @Bean("A")
  @Order(3)
  SomeBean a() {
    return  new SomeBean();
  }

  @Bean("B")
  @Order(1)
  SomeBean b() {
    return new SomeBean();
  }

  @Bean("C")
  @Order(2)
  SomeBean c() {
    return new SomeBean();
  }

  @Bean("D")
  @Order(4)
  SomeBean d() {
    return new SomeBean();
  }

  @Bean("U")
  SomeBean unorderedBean() {
    return new SomeBean();
  }

  @Bean("X")
  X x() {
    return new X();
  }

  @Bean("Y")
  Y y() {
    return new Y();
  }

  @Bean("Z")
  Z z() {
    return new Z();
  }
}

Then, an application component can inject an array or List of SomeBean in order, like so...

@Component
class MyApplicationComponent {

  @Autowired
  SomeBean[] someBeans;

  ...
}

All SomeBean objects will be added to the array in the following order: Y, X, Z, B, C, A, D, U. This is aptly documented and described in the Order annotation Javadoc.

However, as a developer, if I wanted to acquire the beans using some programmatic means in the same order, this is not possible via the API (e.g. such as ListableBeanFactory.getBeansOfType(..)), AFAIK.

ListableBeanFactory.getBeansOfType(..) returns beans in bean definition declaration order as far as possible, as described in the Javadoc. However, this is not exactly the outcome I was looking to achieve in my particular case.

There are a few utility classes, such as BeanFactoryUtils and BeanFactoryAnnotationUtils containing a few extended container functions. However, none contain a function quite like I describe here.

The AutowireCapableBeanFactory.resolveDependency(..) container method handles dependency injection points, but is not exactly the API I am looking to call. Rather, I prefer something more like getBeansOfType(..).

To better demonstrate what I am asking for, I have built a utility class method in SDG to perform the desired function. This can be seen in the SpringUtils.getBeansOfTypeOrdered(..) method along with the associated helper methods.

I have also written a test to demonstrate and assert the desired behavior.

If this function would be useful in the core Spring Framework, as part of a utility class or something, please consider. If not, feel free to ignore and close this ticket.

Thank you for your consideration.

@snicoll
Copy link
Member

snicoll commented Apr 8, 2020

Currently, the Spring Framework API (e.g. ListableBeanFactory) provides no programmatic means to acquire beans in order.

If I understood properly what you're trying to do, beanFactory.getBeanProvider(SomeBean.class).orderedStream() does exactly that.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Apr 8, 2020
@jxblum
Copy link
Author

jxblum commented Apr 8, 2020

Interesting. Let me try that in my test case, and then I will report back. Thank you @snicoll.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 8, 2020
@jxblum
Copy link
Author

jxblum commented Apr 8, 2020

Indeed, that is the case! Very awesome! Thank you much.

@jxblum jxblum closed this as completed Apr 8, 2020
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: feedback-provided Feedback has been provided labels Apr 8, 2020
bclozel added a commit that referenced this issue May 12, 2025
Prior to this commit, the new `ClassFileAnnotationMetadata` would fail
when reading `Class<T>` annotation attributes when values are primitive
types.

This commit uses `java.lang.constant.ClassDesc` to better parse type
descriptors from the bytecode.

Fixes gh-24882
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants