Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

Simply put, the Spring Boot auto-configuration helps us automatically configure a Spring application based on the dependencies that are present on the classpath.

This can make development faster and easier by eliminating the need to define certain beans included in the auto-configuration classes.

In the following section, we’ll look at creating our custom Spring Boot auto-configuration.

2. Maven Dependencies

Let’s start with the dependencies:


The latest versions of spring-boot-starter-data-jpa and mysql-connector-java can be downloaded from Maven Central.

3. Creating a Custom Auto-Configuration

In order to create a custom auto-configuration, we need to create a class annotated as @Configuration and register it.

Let’s create a custom configuration for a MySQL data source:

public class MySQLAutoconfiguration {

Next, we need to register the class as an auto-configuration candidate.

We do this by adding the name of the class in the standard file resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:


If we want our auto-configuration class to have priority over other candidates, we can add the @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) annotation.

We design the auto-configuration using classes and beans marked with @Conditional annotations so that we can replace the auto-configuration or specific parts of it.

Note that the auto-configuration is only in effect if we don’t define the auto-configured beans in the application. If we define our bean, it will override the default one.

3.1. Class Conditions

Class conditions allow us to specify that we want to include a configuration bean if a specified class is present using the @ConditionalOnClass annotation, or if a class is absent using the @ConditionalOnMissingClass annotation.

Let’s specify that our MySQLConfiguration will load only if the class DataSource is present, in which case we can assume the application will use a database:

public class MySQLAutoconfiguration {

3.2. Bean Conditions

If we want to include a bean only if a specified bean is present or not, we can use the @ConditionalOnBean and @ConditionalOnMissingBean annotations.

To look at this, let’s add an entityManagerFactory bean to our configuration class.

First, we’ll specify that we only want to create this bean if a bean called dataSource is present and if a bean called entityManagerFactory is not already defined:

@ConditionalOnBean(name = "dataSource")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em
      = new LocalContainerEntityManagerFactoryBean();
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    if (additionalProperties() != null) {
    return em;

Let’s also configure a transactionManager bean that will load only if we haven’t already defined a bean of type JpaTransactionManager:

@ConditionalOnMissingBean(type = "JpaTransactionManager")
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    return transactionManager;

3.3. Property Conditions

We use the @ConditionalOnProperty annotation to specify if a configuration loads based on the presence and value of a Spring Environment property.

First, let’s add a property source file for our configuration that will determine where the properties will be read from:

public class MySQLAutoconfiguration {

We can configure the main DataSource bean that we’ll use to create connections to the database so that it will load only if a property called usemysql is present.

We can use the attribute havingValue to specify certain values of the usemysql property that have to be matched.

Now let’s define the dataSource bean with default values that connect to a local database called myDb if we set the usemysql property to local:

  name = "usemysql", 
  havingValue = "local")
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    return dataSource;

If we set the usemysql property to custom, we’ll configure the dataSource bean using custom properties values for the database URL, user and password:

@Bean(name = "dataSource")
  name = "usemysql", 
  havingValue = "custom")
public DataSource dataSource2() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setUsername(env.getProperty("mysql.user") != null 
      ? env.getProperty("mysql.user") : "");
    dataSource.setPassword(env.getProperty("mysql.pass") != null 
      ? env.getProperty("mysql.pass") : "");
    return dataSource;

The file will contain the usemysql property:


An application that uses the MySQLAutoconfiguration may need to override the default properties. In this case, it just needs to add different values for the mysql.url, mysql.user and mysql.pass properties and the usemysql=custom line in the file.

3.4. Resource Conditions

Adding the @ConditionalOnResource annotation means that the configuration loads only when a specified resource is present.

Let’s define a method called additionalProperties() that will return a Properties object containing Hibernate-specific properties to be used by the entityManagerFactory bean, only if the resource file is present:

  resources = "")
Properties additionalProperties() {
    Properties hibernateProperties = new Properties();

      env.getProperty("mysql-hibernate.show_sql") != null 
      ? env.getProperty("mysql-hibernate.show_sql") : "false");
    return hibernateProperties;

We can add the Hibernate-specific properties to the file:


3.5. Custom Conditions

Let’s say we don’t want to use any of the conditions available in Spring Boot.

We can also define custom conditions by extending the SpringBootCondition class and overriding the getMatchOutcome() method.

Let’s create a condition called HibernateCondition for our additionalProperties() method that will verify whether a HibernateEntityManager class is present on the classpath:

static class HibernateCondition extends SpringBootCondition {

    private static String[] CLASS_NAMES
      = { "org.hibernate.ejb.HibernateEntityManager", 
          "org.hibernate.jpa.HibernateEntityManager" };

    public ConditionOutcome getMatchOutcome(ConditionContext context, 
      AnnotatedTypeMetadata metadata) {
        ConditionMessage.Builder message
          = ConditionMessage.forCondition("Hibernate");
          .filter(className -> ClassUtils.isPresent(className, context.getClassLoader()))
          .map(className -> ConditionOutcome
            .items(Style.NORMAL, className)))
          .orElseGet(() -> ConditionOutcome
            .noMatch(message.didNotFind("class", "classes")
            .items(Style.NORMAL, Arrays.asList(CLASS_NAMES))));

Then we can add the condition to the additionalProperties() method:

Properties additionalProperties() {

3.6. Application Conditions

We can also specify that the configuration can load only inside/outside a web context. In order to do this, we can add the @ConditionalOnWebApplication or @ConditionalOnNotWebApplication annotation.

4. Testing the Auto-Configuration

Let’s create a very simple example to test our auto-configuration.

We will create an entity class called MyUser and a MyUserRepository interface using Spring Data:

public class MyUser {
    private String email;

    // standard constructor, getters, setters
public interface MyUserRepository 
  extends JpaRepository<MyUser, String> { }

In order to enable auto-configuration, we can use one of the @SpringBootApplication or @EnableAutoConfiguration annotations:

public class AutoconfigurationApplication {
    public static void main(String[] args) {, args);

Next, let’s write a JUnit test that saves a MyUser entity:

  classes = AutoconfigurationApplication.class)
  basePackages = { "com.baeldung.autoconfiguration.example" })
public class AutoconfigurationLiveTest {

    private MyUserRepository userRepository;

    public void whenSaveUser_thenOk() {
        MyUser user = new MyUser("[email protected]");;

Since we didn’t define our DataSource configuration, the application will use the auto-configuration we created to connect to a MySQL database called myDb.

The connection string contains the createDatabaseIfNotExist=true property, so the database does not need to exist. However, the user mysqluser, or the one specified through the mysql.user property if it is present, needs to be created.

We can check the application log to see that we’re using the MySQL data source:

web - 2017-04-12 00:01:33,956 [main] INFO  o.s.j.d.DriverManagerDataSource - Loaded JDBC driver: com.mysql.cj.jdbc.Driver

5. Disabling Auto-Configuration Classes

Let’s say we want to exclude the auto-configuration from loading.

We could add the @EnableAutoConfiguration annotation with exclude or excludeName attribute to a configuration class:

public class AutoconfigurationApplication {

We can also set the spring.autoconfigure.exclude property:


6. Conclusion

In this article, we’ve shown how to create a custom Spring Boot auto-configuration.

The full source code of the example can be found over on GitHub.

The JUnit test can be run using the autoconfiguration profile mvn clean install -Pautoconfiguration.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.