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条件注入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java线性结构中栈、队列和串的基本概念和特点详解

    Java线性结构中栈、队列和串的基本概念和特点详解

    前几天小编给大家介绍了Java线性结构中的链表,除了链表这种结构之外,实际上还有栈、队列、串等结构,那么这些结构又有哪些特点呢,本文就给大家详细的介绍一下,感兴趣的小伙伴跟着小编一起来看看吧
    2023-07-07
  • Java中一个for语句导致无穷大死循环的例子

    Java中一个for语句导致无穷大死循环的例子

    这篇文章主要介绍了Java中一个for语句导致无穷大死循环的例子,本文给出的是一个很特别的例子,这个例子会跟你所想的结果不一样,需要的朋友可以参考下
    2015-06-06
  • Java文件拒绝访问问题及解决

    Java文件拒绝访问问题及解决

    这篇文章主要介绍了Java文件拒绝访问问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • mybatisplus中返回Vo的案例讲解

    mybatisplus中返回Vo的案例讲解

    这篇文章主要介绍了mybatisplus中返回Vo的案例,mybatisplus内置的几个方法使用泛型限制了方法的返回类型,所以实现返回Vo还是得自定义方法, 这个方法名尽量不要和原有的名字类似,本文通过实例代码给大家详解讲解,需要的朋友可以参考下
    2023-03-03
  • Java之HashMap.values()方法的误用解读

    Java之HashMap.values()方法的误用解读

    这篇文章主要介绍了Java之HashMap.values()方法的误用解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • java连接MySQL数据库的代码

    java连接MySQL数据库的代码

    这篇文章主要为大家详细介绍了java连接MySQL数据库的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Java高级之HashMap中的entrySet()方法使用

    Java高级之HashMap中的entrySet()方法使用

    这篇文章主要介绍了Java高级之HashMap中的entrySet()方法使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • SpringBoot整合MongoDB的实现代码

    SpringBoot整合MongoDB的实现代码

    自己本科时候一直使用的是Mysql,目前的课题组使用的是MongoDB,因此就花了一部分时间整理了一下,实现springboot与MongoDB的整合,并且实现基本的增删改查操作,从头到尾给出一个完整的案例。
    2021-05-05
  • Java之网络编程案例讲解

    Java之网络编程案例讲解

    这篇文章主要介绍了Java之网络编程案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • Java通过正则表达式捕获组中的文本

    Java通过正则表达式捕获组中的文本

    这篇文章主要给大家介绍了关于利用Java如何通过正则表达式捕获组中文本的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧下
    2019-09-09

最新评论