- Mastering Spring Boot 2.0
- Dinesh Rajput
- 170字
- 2021-06-25 21:29:14
Learning how auto-configuration works
Let's see what the @Conditional annotation does:
- It allows conditional bean creation. It only creates a bean if other beans exist (or don't exist) as follows:
@Bean @ConditionalOnBean(name={"dataSource"}) public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }
- Or @Conditional annotations allow us to create the bean by checking the type of other classes:
@Bean @ConditionalOnBean(type={DataSource.class}) public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }
- There are many other options available under @Conditional annotation, as follows:
- @ConditionalOnClass
- @ConditionalOnProperty
- @ConditionalOnMissingBean
- @ConditionalOnMissingClass
Let's see what the auto-configuration class looks like in Spring Boot.
It is a pre-written Spring configuration in the org.springframework.boot.autoconfigure package in the spring-boot-autoconfigure JAR file:
@Configuration public class DataSourceAutoConfiguration implements EnvironmentAware { ... @Conditional(...) @ConditionalOnMissingBean(DataSource.class) @Import(...) protected static class EmbeddedConfiguration { ... } ... }
Spring Boot defines many of these configuration classes. They are activated in response to dependencies on the classpath of your Spring application.
Let's see how to customize Spring Boot auto-configuration in your Spring application in the next section.
推薦閱讀
- PPT辦公高手應用技巧
- Getting Started with Simulink
- Word/Excel/PPT 2010辦公應用從入門到精通
- Excel 2010辦公應用實戰從入門到精通(超值版)
- Excel從小白到小能手
- 新編電腦選購、組裝、維護與故障處理從入門到精通
- 精通Excel數據統計與分析
- Excel/PPT 2016辦公專家從入門到精通
- 新手易學:Word 2010+Excel 2010+PowerPoint 2010三合一
- 零基礎學游戲UI設計
- WebGL開發與應用
- 新編Excel公式、函數與圖表應用大全(2016實戰精華版)
- INSTANT Prezi Starter
- 精P之道:高效溝通PPT
- Windows 10從入門到精通