Spring示例讲解条件注入方法

 更新时间:2022年06月21日 10:58:45   作者:IT利刃出鞘  
Spring支持按照条件来注入某些特定的bean,这也是Spring Boot实现自动化配置的底层方法,文中的示例代码讲解详细,需要的可以参考一下

简介

说明

本文用实例介绍Spring的条件注入的用法。

@Component、@Configuration+@Bean都可以与条件注入的注解结合。

@Component+条件注解

Bean

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

application.yml

custom:
  myComponent:
    enabled: true

运行结果:

[MyComponent#MyComponent]

若将application.yml的custom.myComponent.enabled去掉,或者设置为非true值,则不会输出上边的运行结果。

@Configuration+@Bean+条件注解

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

配置类

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
    @Bean
    @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

运行结果:

[MyComponent#MyComponent]

若将application.yml的custom.myComponent.enabled去掉,或者设置为非true值,则不会输出上边的运行结果。

@Configuration+条件注解+@Bean

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

配置类

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyConfig {
    @Bean
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

运行结果:

[MyComponent#MyComponent]

若将application.yml的custom.myComponent.enabled去掉,或者设置为非true值,则不会输出上边的运行结果。

自定义Condition

自定义的condition的matches方法返回值为true时,才会创建bean。

条件类

//判断当前系统是否是Mac

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, 
                           AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Mac");
    }
}
@Configuration
public class Config {
    @Conditional(MyCondition.class)
    @Bean
    public String condition() {
        System.err.println("This is mac");
        return "";
    }
}

到此这篇关于Spring示例讲解条件注入方法的文章就介绍到这了,更多相关Spring条件注入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Kafka中消息队列的两种模式讲解

    Kafka中消息队列的两种模式讲解

    这篇文章主要介绍了Kafka中消息队列的两种模式讲解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Idea去除方法形参参数提示的操作

    Idea去除方法形参参数提示的操作

    这篇文章主要介绍了Idea去除方法形参参数提示的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • Spring Boot中单例类实现对象的注入方式

    Spring Boot中单例类实现对象的注入方式

    这篇文章主要介绍了Spring Boot中单例类实现对象的注入方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • SpringBoot全局处理统一返回类型方式

    SpringBoot全局处理统一返回类型方式

    这篇文章主要介绍了SpringBoot全局处理统一返回类型方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 深入理解Java 线程通信

    深入理解Java 线程通信

    这篇文章主要介绍了Java 线程通信的的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • Java中Instant的使用及转换

    Java中Instant的使用及转换

    Instant是java.time包中的一个类,本文主要介绍了Java中Instant的使用及转换,具有一定的参考价值,感兴趣的可以了解一下
    2024-06-06
  • JavaEE中struts2实现文件上传下载功能实例解析

    JavaEE中struts2实现文件上传下载功能实例解析

    这篇文章主要为大家详细介绍了JavaEE中struts2实现文件上传下载功能实例,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 一篇文章带你入门Java之编程规范

    一篇文章带你入门Java之编程规范

    这篇文章主要介绍了如何养成良好java代码编码规范,规范需要平时编码过程中注意,是一个慢慢养成的好习惯,下面小编就带大家来一起详细了解一下吧
    2021-08-08
  • Java私有构造函数作用原理解析

    Java私有构造函数作用原理解析

    这篇文章主要介绍了Java私有构造函数作用原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Java结合EasyExcel构建复杂多级表头

    Java结合EasyExcel构建复杂多级表头

    在Java开发中,处理Excel文件时,构建复杂的多级表头是一项常见且具有挑战性的任务,下面小编就来和大家聊聊如何通过自定义方法实现多级表头的构建吧
    2025-03-03

最新评论