Unlocking the Power of Spring Boot: Writing Your Own Autoconfiguration Class for Effortless Application Setup

Sachintha Hewawasam
3 min readApr 24, 2023

--

By creating your own autoconfiguration class, you can encapsulate common setup tasks and dependencies for your application, making it easier to reuse and share across different projects.

Application Configuration with Spring Boot: https://medium.com/@sachinthah/spring-into-action-mastering-application-configuration-with-spring-boot-79e4b0bbb9d3

Example of a custom Spring Boot auto-configuration class that is conditional on the Postgres JDBC driver being present:

In this example, we have annotated our configuration class with @ConditionalOnClass(org.postgresql.Driver.class). This means that our auto-configuration will only be applied if the Postgres JDBC driver class is present on the classpath.

Inside our configuration class, we have defined a DataSource bean using DataSourceBuilder.create(). We set the driver class name, database URL, username, and password for the DataSource.

Now, when our application starts up, Spring Boot will automatically detect our auto-configuration class and apply it if the Postgres JDBC driver is present. This allows us to easily provide a DataSource bean for our application, without requiring the developer to manually configure it.

Note that the @ConditionalOnClass annotation can be used to check for the presence of any class on the classpath, not just the Postgres JDBC driver. You can use it to conditionally apply any auto-configuration based on the presence of a particular class.

In addition to @ConditionalOnMissingClass, @ConditionalOnMissingBean, @ConditionalOnProperty, and, @ConditionalOnResourcethere are several other @Conditional annotations in Spring Boot that you can use to conditionally configure your application. Here are a few examples:

  • @ConditionalOnClass: Configures a bean if a specified class is present on the classpath.
  • @ConditionalOnExpression: Configures a bean if a specified SpEL expression evaluates to true.
  • @ConditionalOnJava: Configures a bean based on the version of Java that is running the application.
  • @ConditionalOnWebApplication: Configures a bean based on whether the application is a web application or not.

These annotations, like the ones mentioned earlier, allow you to selectively enable or disable configuration based on various conditions in your application. By using these annotations, you can create more flexible and modular applications that adapt to different environments and use cases.

@ConditionalOnMissingClass

@Configuration
@ConditionalOnMissingClass("org.apache.commons.logging.LogFactory")
public class MyConfiguration {
// Beans defined in this configuration class will only be created if
// the class org.apache.commons.logging.LogFactory is not present in the classpath.
}

@ConditionalOnMissingBean

@Configuration
public class MyConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService() {
// This bean will only be created if no other bean of type MyService is already present.
return new MyServiceImpl();
}
}

@ConditionalOnProperty

@Configuration
@ConditionalOnProperty(
value="myapp.feature.enabled",
havingValue = "true",
matchIfMissing = false
)
public class MyConfiguration {
// Beans defined in this configuration class will only be created if
// the myapp.feature.enabled property is set to true.
}

@ConditionalOnResource

@Configuration
@ConditionalOnResource(resources = "classpath:/my-config-file.properties")
public class MyConfiguration {
// Beans defined in this configuration class will only be created if
// the file my-config-file.properties is present in the classpath.
}

In each of these examples, the @Conditional annotation is used to conditionally create beans or entire configuration classes based on some criteria. This allows for more fine-grained control over the application's configuration and behavior and helps to reduce unnecessary dependencies or configurations.

What is this classpath then?

Suppose you have a Spring application that uses an XML configuration file to define the beans in the application context. The XML file is located in a directory called config the root of your project. Here's how you can configure Spring to load the XML file from the classpath:

@Configuration
public class AppConfig {

@Bean
public BeanFactoryPostProcessor beanFactoryPostProcessor() {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("config/application.properties"));
return configurer;
}

// Other bean definitions here...
}

In this example, the PropertyPlaceholderConfigurer bean is used to load properties from a file called application.properties that is located in the config directory at the root of the classpath. The ClassPathResource class is used to locate the file on the classpath.

Note that the ClassPathResource class is just one of several ways to load resources from the classpath in Spring. You can also use the ResourceLoader interface, the ApplicationContext interface, or the ResourceUtils class to locate resources on the classpath.

Overall, the classpath is a fundamental concept in Spring that is used to locate configuration files, resources, and dependencies for your application. By understanding how the classpath works in Spring, you can build more modular and flexible applications that are easier to maintain and scale.

--

--