Spring Boot Auto Configuration Tips and Tricks

  1. EnableAutoConfiguration annotation attempt to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on the presence or absence of, files or libraries in your classpath, beans you have defined, and properties.  Auto-configuration is always applied after user-defined beans have been registered.

  2. The package of the class that is annotated with @EnableAutoConfiguration has specific significance and is often used as a 'default'. For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration in a root package so that all sub-packages and classes can be searched.

  3. Spring boot auto configuration report shows all auto-configuration info: First shows all the positive matches (conditions evaluated to true), then negative matches (conditions evaluated to false), then the exclusions, and finally unconditional classes which will be always configured.  An auto configuration report can be generated in various ways such as –debug command line argument, -DDebug VM arg, DEBUG=true environment variable, debug=true or logging.level=true in application.properties etc. 

  4. You can manually exclude any auto-configuration that you don’t want to apply through the exclude parameter of EnableAutoConfiguration annotation. Use excludeName parameter if you don't have access to classes; it expects an array of Strings which are the fully qualified class names. You can exclude auto configuration via the spring.autoconfigure.exclude property, which also accept fully qualified class names.

  5. You can reconfigure auto-configuration using various application properties. You can find most common of such properties @ http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html. This is not an exhaustive list.

  6. You can exclude auto-configured servlets or filters making use of ServletRegistrationBean or FilterRegistrationBean.

  7. You can have completely custom auto-configuration by removing @EnableAutoConfiguration annotation and manually configuring an array of auto configuring classes with the @Configuration and @Import annotations; @Import imports all the auto configuration classes.

  8. Auto-configuration classes are regular Spring Configuration beans. They are located using the SpringFactoriesLoader mechanism that reads auto-configuration classes from META-INF/spring.factories file. A list of all auto-configuration classes provided by Spring Boot can be found @ http://docs.spring.io/spring-boot/docs/current/reference/html/auto-configuration-classes.html.

  9. Generally, auto-configuration beans are @Conditional beans, and we can also create one using @Configuration along with @ConditionalOn annotations over classeses, and/or a combination of @Bean along with @ConditionalOn annotations over a method. Most important @Conditional annotations are: @ConditionalOnClass, @ConditionalOnMissingClass, @ConditionalOnBean, @ConditionalOnMissingBean, @ConditionalOnProperty.  There are also additional annotations such as @ConditionalOnJava, @ConditionalOnJndi, @ConditionalOnRersource, @ConditionalOnExpression, @ConditionalOnWebApplication, @ConditionalOnNotWebApplication etc.

  10. SpringBootApplication annotation is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!