I saw a spring boot project which had @EnableAutoConfiguration defined in the file that main method similar to all Spring Boot projects that I have seen. However I saw this annotation being defined in other java files (file for including Swagger config) within the same project. Should this annotation be defined in multiple files? Will there be any adverse impact in doing this?
@EnableAutoComfiguration should only appear once.
In its simplest form, it won't cause any harm if it's declared multiple times, but there's no benefit. However, if you configure any excludes, those excludes will have to be configured on every occurrence of the annotation as they're not cumulative.
even it is allowed to have multiples @EnableAutoConfiguration I would recommend to have only one @EnableAutoConfiguration if it is possible. Just because to be able to exclude any configurations which you don't need in the single place. Otherwise the following issue could happen:
here is primary config of spring boot application:
@SpringBootApplication
@Import(value = {WebSecurityConfiguration.class})
public class Application {
public static void main(String... args) {
SpringApplication.run(Application.class, args);
}
}
here is content of WebSecurityConfiguration class
@Configuration
public class WebSecurityConfiguration {
@Configuration
@EnableAutoConfiguration(exclude ={SecurityAutoConfiguration.class,
SpringBootWebSecurityConfiguration.class})
@Profile("dev")
protected static class DefaultWebSecurityConfig {
}
@Configuration
@EnableAutoConfiguration
@EnableWebSecurity
protected static class LocalWebSecurityConfig extends WebSecurityConfigurerAdapter {
//implementation
}
}
so, I expected to have security auto configuration disabled for dev profile. But just because @SpringBootApplication defines @EnableAutoConfiguration implicitly appropriate configs were scanned as part of 'primary' @EnableAutoConfiguration and default security settings were applied. as result for dev profile security was enabled.
It would not happened if I defined @EnableAutoConfiguration with excludes just once for Application class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With