What does springboot autoconfiguration does??

Kablumndl
3 min readOct 25, 2021

Springboot uses autoconfiguration classes defined in starter module and according to that springboot knows what to configure or not. Springboot searches for spring.factories file defined in the META-INF, whenever if any entry found in the spring.factories files with org.springframework.boot.autoconfigure.EnableAutoConfiguration
then autoconfiguration classes pointed by this property will be loaded.
Autoconfiguraton classes are simple classes annotated with @ConditionOn that tells under which condition autoconfiguration classes will
be loaded and provide beans that will be used by other beans.

There are different types of conditional annotation for autoconfiguration classes mention some of them:-
1. ConditionalOnBean
2. ConditionalOnMissingBean
3. ConditionalOnClass — Presence of class in classPath
4. ConditionalOnMissingClass — Absence of class in classPath
6. ConditionalOnProperty
7. ConditionalOnResource

Lets take a real use case to illustrate this concept in a better manner.

We have crated 2 project named ds-autoconfiguration-app and ds-autoconfiguration ds-autoconfiguration-app is a simple springboot CLI application that will insert some record in DB and then fetch all data from database.In this application we have not created any datasource, this all happens through the aitoconfiguration concept. Added 2 sql scripts file for insertion and fetch data. Now created a ds-autoconfiguration as a separate module that will acts as a dependency in the ds-autoconfiguration-app application. When springboot found dependency of ds-autoconfiguration it will searches for spring.factories file, if EnableAutoConfiguration found
then it point to the classes to be loaded as a autoconfiguration process. In our case DsAutoconfigurationApplication is the autoconfiguration class
annotated with @ConditionOnClass, so if condition will satisfy i.e org.hsqldb.Databasee class present in the classPath then Bean will be created
and available for the beans/classes.

Project — ds-autoconfiguration

This is a acting as maven module with packaging as POM so if this dependecny present in the some other project then autoconfiguration start scanning and will create specific bean for that project and inject the same.

  1. Create a file is META-INF named spring.factories
# Auto Configureorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.auto.config.ds.DsAutoconfigurationApplication

Create a DsAutoconfigurationApplication java class

package com.auto.config.ds;import javax.sql.DataSource;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;import org.springframework.transaction.annotation.EnableTransactionManagement;@Configuration@EnableTransactionManagement@ConditionalOnClass({org.hsqldb.Database.class})public class DsAutoconfigurationApplication implements CommandLineRunner{@Beanpublic DataSource dataSource() {return new EmbeddedDatabaseBuilder().generateUniqueName(true).addScript(“embedded-database-schema.sql”).addScript(“embedded-database-data.sql”).build();}@Overridepublic void run(String… args) throws Exception {// TODO Auto-generated method stub}}

ds-autoconfiguration-app — 2nd springboot project

package com.ds.app;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.jdbc.core.JdbcTemplate;@SpringBootApplicationpublic class DsAutoconfigurationAppApplication implements CommandLineRunner {@Autowiredprivate JdbcTemplate jdbcTemplate;public static void main(String[] args) {SpringApplication.run(DsAutoconfigurationAppApplication.class, args);}@Overridepublic void run(String… args) {System.out.println(“Fetching employees e-mails”);jdbcTemplate.queryForList(“select email from employee”, String.class).forEach(System.out::println);}}

Full demo project are uploaded in github.

Learning always feels good!!!

========================================

--

--

Kablumndl

Java Developer, Software Engineer, Spring, Spark, MicroService, PostgresSQL