首页 > 其他分享 >条件注解之@ConditionalOnProperty注解:通过配置文件的配置来控制配置类是否加入spring的IOC容器

条件注解之@ConditionalOnProperty注解:通过配置文件的配置来控制配置类是否加入spring的IOC容器

时间:2023-07-04 18:14:31浏览次数:47  
标签:配置文件 spring ConditionalOnProperty 配置 生效 注解 public

一、条件注解分类

常见的@ConditionalOnxxx开头的注解我们称之为条件注解,常见的条件注解有

  • class条件注解:@ConditionalOnClass
  • bean条件注解:@ConditionalOnBean
  • 属性条件注解:@ConditionalOnProperty

@ConditionalOnProperty:如果有指定的配置,条件生效;

@ConditionalOnBean:如果有指定的Bean,条件生效;

@ConditionalOnMissingBean:如果没有指定的Bean,条件生效;

@ConditionalOnMissingClass:如果没有指定的Class,条件生效;

@ConditionalOnWebApplication:在Web环境中条件生效;

@ConditionalOnExpression:根据表达式判断条件是否生效。

 这几个注解通常会结合使用,一般都是在配置类中使用,SpringBoot各种xxxxAutoCconfiguration都用到了这些注解,这也是SpringBoot自动装配的重要工具。

二、@ConditionalOnProperty注解

简单来讲,一般是在配置类上或者是@Bean修饰的方法上,添加此注解表示一个类是否要被Spring上下文加载,若满足条件则加载,若不满足条件则不加载。

我们在application.properties中配置的各种配置,添加配置之后即生效,就是这么控制的。

prefix:配置文件中的前缀。

name:配置的名字。

havingValue:它的值与配置文件的值对比,当两个值相同,类会被加载到spring的IOC容器中。

matchIfMissing:缺少该配置时是否可以加载,如果为true,没有该配置属性时也会正常加载,反之则不会生效。

@ConditionalOnProperty源码

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({OnPropertyCondition.class})
public @interface ConditionalOnProperty {
    String[] value() default {};

    String prefix() default "";

    String[] name() default {};

    String havingValue() default "";
boolean matchIfMissing() default false; }

首先看matchIfMissing属性,用来指定如果配置文件中未进行对应属性配置时的默认处理:默认情况下matchIfMissing为false,也就是说如果未进行属性配置,则自动配置不生效。如果matchIfMissing为true,则表示如果没有对应的属性配置,则自动配置默认生效。

如:http编码的自动配置类中,当配置文件中没有配置spring.http.encoding.enabled,自动配置仍然会生效。

@Configuration
@EnableConfigurationProperties({HttpProperties.class})
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({CharacterEncodingFilter.class})
@ConditionalOnProperty(
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {...}

三、使用@ConditionalOnProperty注解

@ConditionalOnProperty注解是控制被注解的类中其他注解(@Component和@Configuration两个注解)是否生效。

1、在配置类中控制@Configuration是否生效

在spring boot中有时候需要控制配置类中的Bean是否生效,可以使用@ConditionalOnProperty注解来控制@Configuration是否生效.

@Configuration被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

@Configuration
@ConditionalOnProperty(prefix = "filter",name = "loginFilter",havingValue = "true")
public class FilterConfig {
    @Bean
    public FilterRegistrationBean getFilterRegistration() {
        FilterRegistrationBean filterRegistration  = new FilterRegistrationBean(new LoginFilter());
        filterRegistration.addUrlPatterns("/*");
        return filterRegistration;
    }
}

 配置文件中代码

filter.loginFilter=true

通过@ConditionalOnProperty控制配置类是否生效,可以将配置与代码进行分离,实现了更好的控制配置。

@ConditionalOnProperty实现是通过havingValue与配置文件中的值对比,相同则配置类生效,反之失效。

2、在切面类中控制@Component是否生效

@Slf4j
@Aspect
@Component
@ConditionalOnProperty(prefix = "system.log", name = "enabled", havingValue = "true")
public class LogAspect {

    //成功的标志
    private final static boolean SUCCESS = true;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private JWTService jwtService;
    @Autowired
    private SystemLogService systemLogService;

    /**
     * 配置切点,拦截Controller中添加@Log注解的方法
     */
    @Pointcut("@annotation(com.ljxx.common.annotation.Log)")
    public void logPointCut() {
    }

    /**
     * 前置通知
     *
     * @param joinPoint
     */
    @Before("logPointCut()")
    public void doBeforeAdvice(JoinPoint joinPoint) {

    }

    /**
     * 后置通知
     *
     * @param obj
     * @throws IOException
     */
    @AfterReturning(returning = "obj", pointcut = "logPointCut()")
    public void doAfterReturning(JoinPoint joinPoint, Object obj) throws Exception {
        handleLog(joinPoint, obj, null);
    }

    /**
     * 异常通知
     *
     * @param e
     */
    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) throws Exception {
        handleLog(joinPoint, null, e);
    }

    /**
     * 日志处理
     *
     * @param result
     * @param e
     */
    private void handleLog(JoinPoint joinPoint, Object result, Exception e) throws Exception {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        ...
        systemLogService.addLog(systemLog);
    }

    /**
     * 截取字符串
     *
     * @param val
     * @return
     */
    private String getText(String val) {
        return StrUtil.sub(val, 0, 4000);
    }


}

配置中的代码

#开启日志存储
system.log.enabled=true

 @ConditionalOnProperty是通过havingValue与配置文件中的值对比,如果返回为true则@Component注解生效,反之失效。

3、在配置类中控制@Component是否生效

(1)、配置文件配置

zs.name=张三
zs.age=14
config.isActive=1

(2)、配置类

@Component
@ConfigurationProperties(prefix = "zs")
@ConditionalOnProperty(prefix = "config",name = "isActive",havingValue = "1")
@Data
public class ConfigurationBean {
    private String name;
    private Integer age;
}

@ConditionOnPropertiy注解的意思是:如果配置文件中config.isActive=1,则该@Component注解生效。

通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射

(3)、测试配置类的@Component注解是否生效

@RestController
public class HelloController {
    @Autowired
    private ConfigurationBean config;

    @GetMapping("/config")
    private String testConfigurationProperties(){
        System.out.println(config);
        return "SUCCESS!!!";
    }
}

启动项目,浏览器访问:http://localhost:8080/config,控制台打印如下:

ConfigurationBean(name=张三, age=14)

修改配置文件如下:

config.isActive=2

启动项目,发现启动失败,如下所示:

即spring的IOC容器中没有找到ConfigurationBean对象。

 

标签:配置文件,spring,ConditionalOnProperty,配置,生效,注解,public
From: https://www.cnblogs.com/zwh0910/p/15459737.html

相关文章

  • rabbitmq在springboot中实战技巧
    一.简介rabbitmq是基于AMQP(AdvancedMessageQueuingProtocol:高级消息队列协议),采用Erlang语言编写的消息队列。二、mq能用来做什么异步处理:将非核心业务(比如日志、邮件、监控等)从主流程剥离,提升主流程的响应时效。削峰:当并发大的情况下,可以将消息暂存在消息队列中,消费者按照......
  • Springboot : 连接ldap超时问题
    Err:java.net.ConnectException:Connectiontimedoutwhenconnectingtoldap使用springbootldap连接账号所属ldap目录验证时,出现如上报错经检查,host,username,password等信息均无误,如下为代码中的配置信息示例hashEnv.put(Context.SECURITY_AUTHENTICATION,"simple"......
  • 面试题-Spring 面试集
    Spring面试集基于SpringFramework4.x总结的常见面试题,系统学习建议还是官方文档走起:https://spring.io/projects/spring-framework#learn一、一般问题开发中主要使用Spring的什么技术?IOC容器管理各层的组件使用AOP配置声明式事务整合其他框架Spring有哪些优......
  • 面试题-SpringBoot
    概述说说SpringBoot和Spring的关系springboot我理解就是把springspringmvcspringdatajpa等等的一些常用的常用的基础框架组合起来,提供默认的配置,然后提供可插拔的设计,就是各种starter,来方便开发者使用这一系列的技术,套用官方的一句话,spring家族发展到今天,已经......
  • 通过Spring Boot三分钟创建Spring Web项目
    SpringBoot简介接下来我们所有的Spring代码实例将会基于SpringBoot,因此我们先来了解一下SpringBoot这个大杀器。Spring早期使用XML配置的方式来配置SpringBeans之间的关系,比如AOP和依赖注入的配置。随着功能以及业务逻辑的日益复杂,应用便会伴随大量的XML配置文件以及复杂的Bean......
  • 实战项目:构建基于Spring Boot和Vue.js的金融项目分享
    学习SpringBoot和Vue.js结合的前后端分离项目可以按照以下步骤进行:掌握SpringBoot:学习SpringBoot的基本概念和核心特性,如自动配置、起步依赖、注解驱动等。了解Spring框架的基本知识,如IoC容器、AOP、MVC模式等。学习Vue.js:学习Vue.js的基本语法、指令和组件,理解Vue实例、数据绑......
  • springboot封装redission的分布式锁逻辑为注解
    场景概述使用分布式锁的时候,每次都需要使用trycatch处理方法中的逻辑。考虑是否可以这块逻辑抽离出来。实现在自定义的注解中添加属性来设置锁的等待时间、租赁时间和时间单位importjava.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTI......
  • Spring容器获取Bean的9种方式
    1前言随着SpringBoot的普及,Spring的使用也越来越广,在某些场景下,我们无法通过注解或配置的形式直接获取到某个Bean。比如,在某一些工具类、设计模式实现中需要使用到Spring容器管理的Bean,此时就需要直接获取到对应的Bean。本文为大家整理汇总了常见的获取Bean的方式,并提供一些优......
  • SpringBoot教学资料6-SpringBoot登录注册功能实现(带简单前端)
     项目样式:      SQL:CREATETABLE`t_user`(`id`int(11)NOTNULLAUTO_INCREMENT,`username`varchar(32)NOTNULL,`password`varchar(32)NOTNULL,PRIMARYKEY(`id`),UNIQUEKEY`username`(`username`))ENGINE=InnoDBAUTO_INCR......
  • springMVC入门
    定义Controller//定义Controller//使用@Controller定义bean@ControllerpublicclassUserController{//设置当前操作的访问路径@RequestMapping("/save")//设置当前操作的返回值类型@ResponseBodypublicStringsave(){System.out.p......