Mastering Spring Boot 2.0
上QQ阅读APP看书,第一时间看更新

Replacing generated beans

You can also customize Spring Boot auto-configuration by defining certain beans yourself in your Spring application, so Spring Boot won't use default configuration for those beans.

Normally, beans you declare explicitly disable any auto-created ones. Let's see the following example:

@Bean 
public DataSource dataSource() { 
   return new EmbeddedDatabaseBuilder(). 
         setName("AccountDB").build(); 
} 

In the preceding code, we have explicitly defined a DataSource bean; your DataSource bean configuration stops Spring Boot from creating a default DataSource. Bean names are often not important. It works with XML-based configuration, annotation, and/or Java-based configuration.

Let's see another way of customizing Spring Boot autoconfiguration.