RCR
RCR

Reputation: 11

How would one use a feature toggle within the app config bean creation using Togglz Spring Autoconfiguration?

Example of using the feature toggle to create a bean within the app configuration:

@RefreshScope
@Configuration
@Order(1)
class AppConfig {
    @Bean
    @Autowired
    public FeatureProvider featureProvider() {
        return new EnumBasedFeatureProvider(FeatureToggle.class);
    }

    @Bean
    @Autowired
    ProjectAccess getProjectAccess(DataSource dataSource, HazelcastLocator hazelcastLocator) {
        if(FeatureToggle.MY_TOGGLE_NAME.isActive()) {
            return new MyTestClass();
        }
        else {
            return new YourTestClass();
        }
    }
}

Upvotes: 1

Views: 456

Answers (1)

chkal
chkal

Reputation: 5668

You shouldn't do it this way. ;-)

The problem with this code is that you are basically reading the toggle once at startup time and then cannot change it any more. That's not how Togglz is typically used.

Instead you should design your app so that it is possible to toggle the switch at runtime. Togglz provides the FeatureProxyFactoryBean which was designed exactly for this case.

You could also create do this manually by creating a common interface for both implementations and then create an implementation which basically checks the toggle on each method invocation and then delegates to the correct instance.

Upvotes: 1

Related Questions