I want to externalize the @SpringBootApplication(exclude...) option, to have a reusable class or annotation that I could throw in to exclude any database/hibernate initialization.
So, instead of writing:
@SpringBootApplication(
exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
public class MainApp {
}
I would like to create a annotation that I could apply to my @SpringBootApplication main class:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
@Configuration
public @interface ExcludeDataSources {
}
And then enable this feature by annotation:
@SpringBootApplication
@ExcludeDataSources
public class MainApp {
}
Problem: the annotation approach does not work, and spring still tries to load a database. Why?
My final goal is to have multiple startup classes, where only one loads the database.
@SpringBootApplicationcontains@EnableAutoConfigurationwithout excludes, I am not sure which annotation will be chosen by Spring's meta annotation logic, but I would replace@SpringBootApplicationwith its annotations (except@EnableAutoConfigurationof course)spring.autoconfigure.excludeto the list of auto-configuration classes to exclude? Set this configuration property inside a profile.