DEV Community

Salad Lam
Salad Lam

Posted on

1

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

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay