DEV Community

Salad Lam
Salad Lam

Posted on

Spring Boot: how to disable build-in auto-configuration

The objective of the Spring Boot project is to pre-define essential beans for you in order to start up an application with minimum configuration. This is done by a set of auto configuration classes bundled in Spring Boot's package. And if you define a bean, Spring Boot is also able to recognize your bean, then will not create the predefined bean.

For example org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration class of spring-boot-autoconfigure:2.6.11.

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ DataSource.class, JdbcTemplate.class })
@ConditionalOnSingleCandidate(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(JdbcProperties.class)
@Import({ DatabaseInitializationDependencyConfigurer.class, JdbcTemplateConfiguration.class,
        NamedParameterJdbcTemplateConfiguration.class })
public class JdbcTemplateAutoConfiguration {
}
Enter fullscreen mode Exit fullscreen mode

This class will enable only if

  1. DataSource and JdbcTemplate class files can be found in class path
  2. Only one DataSource bean is created

For some time, it is necessary to disable the entire auto configuration class. For example, if you want to disable an auto configuration class org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration. Just specify the annotation attribute "exclude" of org.springframework.boot.autoconfigure.SpringBootApplication annotation. i.e.

@SpringBootApplication(exclude = {HibernateJpaAutoConfiguration.class})
Enter fullscreen mode Exit fullscreen mode

And in CONDITIONS EVALUATION REPORT, you can see the class is excluded in Exclusions section.

Exclusions:
-----------
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
Enter fullscreen mode Exit fullscreen mode

Top comments (0)