Replies: 1 comment
Solution : Use @factory with @EachBean (Recommended)Create a factory that finds all classes with import io.micronaut.context.annotation.Bean
import io.micronaut.context.annotation.EachBean
import io.micronaut.context.annotation.Factory
import io.micronaut.core.annotation.AnnotationMetadata
import io.micronaut.inject.BeanDefinition
import io.micronaut.inject.qualifiers.Qualifiers
import jakarta.inject.Singleton
@Factory
class AggregateBeanFactory(
private val beanDefinitions: List<BeanDefinition<*>>
) {
@EachBean(AggregateClassHolder::class)
fun aggregateService(holder: AggregateClassHolder): AggregateService {
// Create a singleton for each @Aggregate-annotated class
return AggregateServiceImpl(holder.aggregateClass)
}
}
// Holder class for EachBean (required because @EachBean works on a bean, not directly on types)
@Singleton
class AggregateClassHolder(
val aggregateClass: Class<*>,
val annotation: Aggregate
)
// Producer that creates a holder for each @Aggregate-annotated class
@Factory
class AggregateClassScanner(
val beanDefinitions: List<BeanDefinition<*>>
) {
@Bean
fun aggregateClassHolders(): List<AggregateClassHolder> {
return beanDefinitions
.filter { it.hasAnnotation(Aggregate::class.java) }
.mapNotNull { beanDef ->
val annotation = beanDef.getAnnotation(Aggregate::class.java)
if (annotation != null) {
AggregateClassHolder(beanDef.beanType, annotation)
} else null
}
}
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Is have a custom annotation which is also annotated with
@PrototypeI need to create a Singleton for each class annotated with
@AggregateI don't see how I can do this gradefully.
If only
@EeachBeanworked on annotations or something that would solve my issue i guess.Am I missing something?
All reactions