micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.05k stars 1.06k forks source link

Add Support for properties and @NonBinding in javax.inject.Qualifier annotations #4769

Closed ghillert closed 3 years ago

ghillert commented 3 years ago

I am trying to use a custom @Qualifier annotation which itself contains qualifying elements/properties together with InjectionPoint. Although the respective @Bean definition looks almost exactly the same, the observed behavior, however, is different from Spring Framework. It is actually quite contrary.

Micronaut is ignoring any elements/properties of the Qualifier annotation in determining the proper @Bean method. For instance the following (very contrived) code in Spring would work but fails in Micronaut:

@Bean
@Greeting(language = Language.DE)
@Prototype
public String greetingDE(InjectionPoint injectionPoint) {
    return handleInjectionPoint(injectionPoint);
}

@Bean
@Greeting(language = Language.EN)
@Prototype
public String greetingEN(InjectionPoint injectionPoint) {
    return handleInjectionPoint(injectionPoint);
}

@Bean
@Greeting(language = Language.ES)
@Prototype
public String greetingES(InjectionPoint injectionPoint) {
    return handleInjectionPoint(injectionPoint);
}

I wonder if Micronaut could be aligned more closely with CDI in that regard and not only consider properties but also the @NonBinding annotation to provide comprehensive qualifier support.

Ideally, it would be desirable if also Spring and Micronaut would align in this regards as the targeted functionality is de facto identical. For reference please see 2 issues I have created against Spring Framework:

Add Support for @NonBinding in javax.inject.Qualifier annotations https://github.com/spring-projects/spring-framework/issues/26302

Add ability to ignore certain Qualifier Annotations using CustomAutowireConfigurer https://github.com/spring-projects/spring-framework/issues/26305

It certainly does not help the matter that @nonbinding is not part of JSR-330 (Dependency Injection for Java). Rather it is part of JSR-299 (Java Contexts and Dependency Injection).

This issue might be related to issue CDI Alignment Tasks.

For further reference:

Java EE JavaDoc - Annotation Type Nonbinding https://javaee.github.io/javaee-spec/javadocs/javax/enterprise/util/Nonbinding.html

Advanced Using @Nonbinding to combine a configuration annotation and a qualifier annotation into one annotation https://dzone.com/articles/cdi-di-p2

graemerocher commented 3 years ago

Thanks for the feedback, I will include it in #4658

ghillert commented 3 years ago

Thanks! By the way, speaking of CDI Alignment Tasks: 6 years ago Juergen Hoeller mentioned some activity around a JSR-330 v2. This might technically be a good candidate for some improved standardization.

https://github.com/spring-projects/spring-framework/issues/17723

graemerocher commented 3 years ago

Fixed in https://github.com/micronaut-projects/micronaut-core/pull/5075