Logo

dev-resources.site

for different kinds of informations.

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

Published at
12/8/2024
Categories
spring
springboot
Author
Salad Lam
Categories
2 categories in total
spring
open
springboot
open
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 {
}

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})

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

Exclusions:
-----------
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

Featured ones: