一、@Conditional派生类
作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;
@Conditional扩展注解 | 作用(判断是否满足当前指定条件) |
---|---|
@ConditionalOnJava | 系统的java版本是否符合要求 |
@ConditionalOnBean | 容器中存在指定Bean; |
@ConditionalOnMissingBean | 容器中不存在指定Bean; |
@ConditionalOnExpression | 满足SpEL表达式指定 |
@ConditionalOnClass | 系统中有指定的类 |
@ConditionalOnMissingClass | 系统中没有指定的类 |
@ConditionalOnSingleCandidate | 容器中只有一个指定的Bean,或者这个Bean是首选Bean |
@ConditionalOnProperty | 系统中指定的属性是否有指定的值 |
@ConditionalOnResource | 类路径下是否存在指定资源文件 |
@ConditionalOnWebApplication | 当前是web环境 |
@ConditionalOnNotWebApplication | 当前不是web环境 |
@ConditionalOnJndi | JNDI存在指定项 |
自动配置类必须在一定的条件下才能生效。
我们怎么知道哪些自动配置类生效?下面讲解
二、Debug模式
1、定义
我们怎么知道哪些自动配置类生效了,哪些没生效呢?怎么调试呢?运用SpringBoot提供的debug可以清晰的发现。
2、步骤
2.1、配置文件(application.properties)
debug=true
2.2、启动类
@SpringBootApplication
public class Springboot03AutoconfigApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot03AutoconfigApplication.class, args);
}
}
2.3、运行结果
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:(自动配置类启用的)
-----------------
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)
Negative matches:(没有启动,没有匹配成功的自动配置类)
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)
AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)
PS:主要两大部分:Positive matches:(自动配置类启用的)和Negative matches:(没有启动,没有匹配成功的自动配置类)。
默认配置启动的时候,会发现HttpEncodingAutoConfiguration类在Positive matches:中出现。
若配置了:spring.http.encoding.enabled=false;
则会发现HttpEncodingAutoConfiguration类在Negative matches:中出现。
这样就很完美的能发现出我们的配置类到底生效没生效。
三、广告
-
QQ群【Java初学者学习交流群】:458430385
-
微信公众号【Java码农社区】
![](https://img.haomeiwen.com/i4582242/ca4a357ae859b1aa.jpg)
- 今日头条号:编程界的小学生
网友评论