Spring Framework

Spring auto-configuration

What is auto-configuration?

Auto-configuration is a key feature of Springboot that contributes to rapid development of applications by eliminating the need to pre-configure resources or properties required by the application in runtime.

@EnableAutoConfiguration is a powerful construct that drives Springboot applications by allowing configuration classes to be dynamically scanned into the application context and is based on JARs that are loaded onto the class path through possibly Maven/Gradle dependency management. Ultimately, @EnableAutoConfiguration is driven off the spring.factories file in the meta.inf directory of each jar that gets loaded into the application itself via the dependency management system in use. You can control the order of auto-configuration using @AutoConfigureBefore and @AutoConfigureAfter annotations.

@SpringbootApplication is a stereotype of @EnableAutoConfiguration and therefore includes this annotation.

Conditional Loading

Some conditional loading allows for configurations to only be loaded when or if there are classes specifically on the class path, such as the @ConditionalOnClass annotation.

You can drive configuration off the presence of a bean being defined in the application context using @ConditionalOnBean or based on specific properties being defined with the @ConditionalOnProperty annotation.

Configuration can also be driven from missing classes, beans or properties using the @ConditionalOnMissingClass, @ConditionalOnMissingBean, or @ConditionalOnMissingProperty annotations.

Properties

Properties are a big part of the default configuration for Springboot. Many dependencies include pre-configured default properties for the auto-configuration classes. The @EnableConfigurationProperties annotation is used to specify and load these property sets. Properties can always be overridden if required.

Application type based conditionals are also available if for example you want to define whether an application is web-based or not. Resource or file based annotations are available for use as well as expression based conditionals using the Spring expression language to drive conditional expectations of your class to control what gets loaded during auto-configuration.

Configuring Spring

Part of the operational power of Springboot comes from the various ways that you can change and control the configuration of an application during startup or runtime.

Basic configuration required for development or demo purposes are contained in the application.properties or application.yaml file. Environment variables are usually required for modifying the profiles that will be loaded and is a way of modifying the runtime environment for specific configuration properties. Properties can also be injected into the application through command-line parameters.

The most common way of modifying properties of Springboot applications is through cloud configurations. It is important to consider externalizing common configuration properties that are shared among multiple cloud applications through the use of Config Server, Consul, etc in a cloud environment.