Auto configuration is the automatic configuration of a Spring application using Spring Boot, which load default values and modules that are in the class path, according to preset conditions and rules. There is a lot of boilerplate code and configuration in a Spring application, and that's the power of Spring Boot.
Three annotations are applied for that purpose:
The annotation @SpringBootApplication can replace these three annotations with only one.
Here's an example of annotated class:
@EnableAutoConfiguration @Configuration @ComponentScan(basePackages={"com.davisfiore"}) public class UserConfiguration { @Bean CustomBean customBean() { // Code to override or create a new bean } // Other beans here }
Next we need to register the class by adding the name to the Spring Boot spring.factories configuration file:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.davisfiore.demo.ClientConfiguration,\ com.davisfiore.demo.UserConfiguration
There a number of annotations to load the bean conditionally.
@Bean @ConditionalOnBean(value = {OtherBean.class}) MyBean myBean() { // Creation code }
Here are the most common ones.
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.