Togglz with Kotlin: Problem to inject some dependency in my custom ActivationStrategy

184 Views Asked by At

I have defined the file that references my custom ActivationStrategy in META-INF/Services/ as explained by the Togglz library for custom strategy (https://www.togglz.org/documentation/activation-strategies.html), since I need to resolve whether or not it is activated through another logic that is in another Service. Now how do I inject this service that I need to consume? Since when trying the following:

@Component
class ActivationStrategyByProfile(private val profileService : ProfileService) : ActivationStrategy {

    override fun getId(): String {
        return ID
    }

    override fun getName(): String {
        return NAME
    }

    override fun isActive(
        featureState: FeatureState,
        user: FeatureUser?
    ): Boolean {
        
        val profileId = user?.name
        return profileService.validateProfile(profileId)
    }
    ...

My file in /META-INF/services/org.togglz.core.spi.ActivationStrategy contain:

com.saraza.application.config.ActivationStrategyByProfile

It returns the following error, mentioning that the reference that I have specified in the file, does not include parameters, I understand:

...ActivationStrategyByProfile Unable to get public no-arg constructor

How can I inject the dependency of my service? Can it be done by changing the extended file used by the Java ServiceLoader mechanism? Something like specifying the service as a parameter?

1

There are 1 best solutions below

0
On BEST ANSWER

The solution was quite simple, the configuration allows you to directly specify the strategy you want to implement:

 @Bean
fun activationStrategyProvider(
    activationStrategies: List<ActivationStrategy>,
    activationStrategyByProfile: ActivationStrategyByProfile
): ActivationStrategyProvider? {
     val activationStrategyProvider = DefaultActivationStrategyProvider()
     activationStrategyProvider.addActivationStrategy(activationStrategyByProfile)
     return activationStrategyProvider
}