I am using spring-boot 2.0.4; I have a bunch of services and they have a common configuration class marked with @Configuration. I want to move this to a common dependency which will have this @Configuration, and based on the need, any micro-service can use @ComponentScan to activate this configuration from dependency.
I have done this for @Component classes, and it's working fine. I activate any particular component I need by adding it into @ComponentScan. How can I activate the configuration in a similar manner(based on need).
Here are the code examples:
Common Configuration:
package abc.department.common.configs.mongo
@Component
public class AbcMongo {
@Bean
public MongoTemplate mongoTemplate() {
// ... create MongoTemplate.
return createdMongoTemplate;
}
}
Here is a class which uses the above dependency:
@Configuration
@ComponentScan("abc.department.common.configs.mongo")
public class MyServiceConfigs {
}
Similarly, I want to do something like this:
package abc.department.common.configs.security.web
@Configuration
@EnableWebSecurity
public class AbcWebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// ... do common configs;
}
}
and now, if a service would need web-security config, it could get like:
@Configuration
@ComponentScan({"abc.department.common.configs.mongo","abc.department.common.configs.security.web"})
public class MyServiceConfigs {
}