SpringBoot之@Conditional注解的派生类以及debug模式

本文介绍了SpringBoot中@Conditional注解的派生类原理,阐述了其在自动配置类生效条件中的作用。同时,详细探讨了SpringBoot的Debug模式,包括如何定义和调试,通过步骤解析了如何查看生效与未生效的自动配置类,以帮助开发者理解配置类的启用状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、@Conditional派生类

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;

@Conditional扩展注解作用(判断是否满足当前指定条件)
@ConditionalOnJava系统的java版本是否符合要求
@ConditionalOnBean容器中存在指定Bean;
@ConditionalOnMissingBean容器中不存在指定Bean;
@ConditionalOnExpression满足SpEL表达式指定
@ConditionalOnClass系统中有指定的类
@ConditionalOnMissingClass系统中没有指定的类
@ConditionalOnSingleCandidate容器中只有一个指定的Bean,或者这个Bean是首选Bean
@ConditionalOnProperty系统中指定的属性是否有指定的值
@ConditionalOnResource类路径下是否存在指定资源文件
@ConditionalOnWebApplication当前是web环境
@ConditionalOnNotWebApplication当前不是web环境
@ConditionalOnJndiJNDI存在指定项

自动配置类必须在一定的条件下才能生效。

我们怎么知道哪些自动配置类生效?下面讲解

二、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:中出现。

这样就很完美的能发现出我们的配置类到底生效没生效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值