- 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.
推薦閱讀
- 電腦辦公(Windows 7 + Office 2013)入門與提高(超值版)
- Word/Excel 2016高效辦公從入門到精通
- WPS Office 2019 高效辦公
- 我的世界:Minecraft模組開發(fā)指南
- 財(cái)務(wù)必須知道的115個(gè)Excel函數(shù)
- 和秋葉一起學(xué):秒懂Word(全彩版)
- 快·易·通:2天學(xué)會(huì)Word/Excel綜合辦公應(yīng)用(2016版)
- Learning AWS IoT
- 巧學(xué)巧用Excel函數(shù):掌握核心技能,秒變數(shù)據(jù)分析高手
- 電腦辦公直通車
- Office2010從入門到精通(視頻講解+圖解+技巧)
- 文秘辦公自動(dòng)化培訓(xùn)教程
- 快速編碼:高效使用MicrosoftVisualStudio
- Excel 2007使用大全
- 如何高效辦公:Excel 2013經(jīng)典應(yīng)用技巧大全