赞
踩
我们在跟踪SpringBoot自动配置的源码的时候,在自动配置类声明bean的时候,除了在方法上加了一个@Bean注解以外,还会经常用到一个注解,就是以Conditional开头的这一类的注解。以Conditional开头的这些注解都是条件装配的注解。下面我们就来介绍下条件装配注解。
下面我们通过代码来演示下Conditional注解的使用:
- ~~~java
- @Configuration
- public class HeaderConfig {
-
- @Bean
- @ConditionalOnClass(name="io.jsonwebtoken.Jwts")//环境中存在指定的这个类,才会将该bean加入IOC容器
- public HeaderParser headerParser(){
- return new HeaderParser();
- }
- }
- ~~~
pom.xml
- ~~~java
- <!--JWT令牌-->
- <dependency>
- <groupId>io.jsonwebtoken</groupId>
- <artifactId>jjwt</artifactId>
- <version>0.9.1</version>
- </dependency>
- ~~~
测试类
- ~~~java
- @SpringBootTest
- public class AutoConfigurationTests {
- @Autowired
- private ApplicationContext applicationContext;
-
- @Test
- public void testHeaderParser(){
- System.out.println(applicationContext.getBean(HeaderParser.class));
- }
- }
- ~~~
执行testHeaderParser()测试方法:
因为io.jsonwebtoken.Jwts字节码文件在启动SpringBoot程序时已存在,所以创建HeaderParser对象并注册到IOC容器中。
- ~~~java
- @Configuration
- public class HeaderConfig {
-
- @Bean
- @ConditionalOnMissingBean //不存在该类型的bean,才会将该bean加入IOC容器
- public HeaderParser headerParser(){
- return new HeaderParser();
- }
- }
执行testHeaderParser()测试方法:
SpringBoot在调用@Bean标识的headerParser()前,IOC容器中是没有HeaderParser类型的bean,所以HeaderParser对象正常创建,并注册到IOC容器中。
再次修改@ConditionalOnMissingBean注解:
- ~~~java
- @Configuration
- public class HeaderConfig {
-
- @Bean
- @ConditionalOnMissingBean(name="deptController2")//不存在指定名称的bean,才会将该bean加入IOC容器
- public HeaderParser headerParser(){
- return new HeaderParser();
- }
- }
- ~~~
执行testHeaderParser()测试方法:
因为在SpringBoot环境中不存在名字叫deptController2的bean对象,所以创建HeaderParser对象并注册到IOC容器中。
再次修改 注解:
- ~~~java
- @Configuration
- public class HeaderConfig {
-
- @Bean
- @ConditionalOnMissingBean(HeaderConfig.class)//不存在指定类型的bean,才会将bean加入IOC容器
- public HeaderParser headerParser(){
- return new HeaderParser();
- }
- }
- ~~~
- ~~~java
- @SpringBootTest
- public class AutoConfigurationTests {
- @Autowired
- private ApplicationContext applicationContext;
-
- @Test
- public void testHeaderParser(){
- System.out.println(applicationContext.getBean(HeaderParser.class));
- }
- }
- ~~~
执行testHeaderParser()测试方法:
因为HeaderConfig类中添加@Configuration注解,而@Configuration注解中包含了@Component,所以SpringBoot启动时会创建HeaderConfig类对象,并注册到IOC容器中。
当IOC容器中有HeaderConfig类型的bean存在时,不会把创建HeaderParser对象注册到IOC容器中。而IOC容器中没有HeaderParser类型的对象时,通过getBean(HeaderParser.class)方法获取bean对象时,引发异常:NoSuchBeanDefinitionException。
先在application.yml配置文件中添加如下的键值对:
name: itheima
在声明bean的时候就可以指定一个条件@ConditionalOnProperty
- ~~~java
- @Configuration
- public class HeaderConfig {
-
- @Bean
- @ConditionalOnProperty(name ="name",havingValue = "itheima")//配置文件中存在指定属性名与值,才会将bean加入IOC容器
- public HeaderParser headerParser(){
- return new HeaderParser();
- }
-
- @Bean
- public HeaderGenerator headerGenerator(){
- return new HeaderGenerator();
- }
- }
执行testHeaderParser()测试方法:
修改@ConditionalOnProperty注解: havingValue的值修改为"itheima2"
- ~~~java
- @Bean
- @ConditionalOnProperty(name ="name",havingValue = "itheima2")//配置文件中存在指定属性名与值,才会将bean加入IOC容器
- public HeaderParser headerParser(){
- return new HeaderParser();
- }
- ~~~
再次执行testHeaderParser()测试方法:
因为application.yml配置文件中,不存在: name: itheima2,所以HeaderParser对象在IOC容器中不存在。
我们再回头看看之前SpringBoot源码时提到的一个配置类:GsonAutoConfiguration
最后再给梳理一下自动配置原理:
自动配置的核心就在@SpringBootApplication注解上,SpringBootApplication这个注解底层包含了3个注解,分别是:
@EnableAutoConfiguration这个注解才是自动配置的核心。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。