SpringBoot AutoConfiguration
SpringBoot AutoConfiguration
com
Consider a case where we want to use a spring application to query a database. We add a
‘spring-jdbc‘ dependency to the project and write XML configuration for creating a ‘datasource‘
and ‘jdbcTemplate‘.
Below is an extract of an actual Spring XML configuration which creates a datasource and
a jdbcTemplate.
</bean>
The jdbcTemplate bean depends on the dataSource bean, which depends on various external
configurations. Apart from the external variables, the bean configurations remain the same in
every project we use the JdbcTemplate instance.
What if the instances were created in Java instead? – We still have repeating code, boilerplate
code.
DriverManagerDataSource dataSource =
new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
If we don't want specific auto-configure classes to be applied, we can disable them with the
exclude attribute of @EnableAutoConfiguration.
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@Conditional
The @Conditional annotation indicates that a component is only registered into the application
context when all the specified conditions match. If a @Configuration class is marked
https://skolaparthi.com
@ConditionalOnProperty
@Bean
@ConditionalOnProperty(
value = "emp.address.enabled",
havingValue = "true",
matchIfMissing = false
)
public Address getAddress(){
return new Address();
}
Above bean will only be loaded if in config some where we have emp.address.enabled flag
as true. matchIfMissing says, if not config found then set true by default.
@ConditionalOnExpression
@Bean
@ConditionalOnExpression(
"${module.emp.enabled} and ${module.address.enabled:true}"
)
public Employee getEmployee(){
return new Employee();
}
@ConditionalOnBean
@Bean
@ConditionalOnBean(Address.class)
Employee bean will be build if Address bean class is there in application context. We can also
put bean name instead of bean class
@ConditionalOnMissingBean
@Bean
@ConditionalOnMissingBean(
NotABean.class
Name bean will be loaded if NotABean bean class is not found in application context or
classpath.
@ConditionalOnResource
@Bean
@ConditionalOnResource(
resources = "/application.properties"
https://skolaparthi.com
The above bean will only be loaded if we have application.properties in our classpath.
@ConditionalOnClass
@Bean
@ConditionalOnClass(
name = "org.springframework.boot.web.embedded.tomcat.TomcatWebServer")
@ConditionalOnMissingClass
@Bean
@ConditionalOnMissingClass(
value = "class.not.found"
MissingClassBean will be loaded only if given class in value attribute is not found in classpath.
https://skolaparthi.com
@ConditionalOnJndi
@Bean
@ConditionalOnJndi(
value = "java:comp/env/datasource"
Load the bean only if certain JNDI resource is found in JNDI config.
@ConditionalOnJava
@Bean
@ConditionalOnJava(
value = JavaVersion.EIGHT
@ConditionalOnSingleCandidate
@Bean
@ConditionalOnSingleCandidate(
https://skolaparthi.com
value = Employee.class
@ConditionalOnWebApplication
@Bean
@ConditionalOnWebApplication
Bean will be loaded only if our application is running in web application container.
@ConditionalOnNotWebApplication
@Bean
@ConditionalOnNotWebApplication