Spring Framework
  1. Spring Framework
  2. SPR-5611

Preserve custom annotations present on @Bean methods for interrogation during bean post processing

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: 3.0 M2
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      With Spring using more and more annotations, it would be good if the bean definitions allow registering custom annotations for use with Bean/Factory post processors.

      Specifically, in JavaConfig one can annotate a method in a configuration class with @Bean annotation to create a bean from the result of the method. However, it is not possible to add custom annotations on the method so as to process them with a bean post processor.

      The use case is for integrating with spring-dm. Specifically, I would like to annotate a "regular" @Bean method with @OsgiService annotation so the bean is published as a service.

      NOTE: another approach is to allow me to replace @Bean with @OsgiServiceBean which would do both. The current JavaConfig code is very tightly bound to the internal annotations and it is had to add custom ones (specifically, i can't create a custom annotation and reuse BeanRegistrar since it uses @Bean directly)

        Activity

        Hide
        Chris Beams added a comment -

        Hi Ittay,

        It's been quite a while since entering this issue, and we haven't had additional interest since in preserving custom annotations when processing @Bean methods.

        While not impossible, there are a number of reasons why it would be problematic to do so. I'm resolving this as won't fix for now, however keep in mind that there is a workaround that may meet your needs. Since you were planning to employ a BeanPostProcessor anyway, you could:

        • use the beanName provided to the BPP methods to look up the associated BeanDefinition from the enclosing BeanFactory;
        • query that BeanDefinition for its factoryBeanName (the @Configuration bean) and factoryMethodName (the @Bean name);
        • use reflection to get hold of the Method the bean originated from
        • use reflection to interrogate any custom annotations from that method

        If this is still relevant to you, I hope that helps!

        Show
        Chris Beams added a comment - Hi Ittay, It's been quite a while since entering this issue, and we haven't had additional interest since in preserving custom annotations when processing @Bean methods. While not impossible, there are a number of reasons why it would be problematic to do so. I'm resolving this as won't fix for now, however keep in mind that there is a workaround that may meet your needs. Since you were planning to employ a BeanPostProcessor anyway, you could: use the beanName provided to the BPP methods to look up the associated BeanDefinition from the enclosing BeanFactory; query that BeanDefinition for its factoryBeanName (the @Configuration bean) and factoryMethodName (the @Bean name); use reflection to get hold of the Method the bean originated from use reflection to interrogate any custom annotations from that method If this is still relevant to you, I hope that helps!

          People

          • Assignee:
            Chris Beams
            Reporter:
            Ittay Dror
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              2 years, 44 weeks, 5 days ago