栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring(SpringBoot)--条件注入(条件装配)--实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring(SpringBoot)--条件注入(条件装配)--实例

原文网址:Spring(SpringBoot)--条件注入(条件装配)--实例_IT利刃出鞘的博客-CSDN博客

简介

说明

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

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

相关网址

Spring(SpringBoot)--条件注入(条件装配)--详解_IT利刃出鞘的博客-CSDN博客_springboot 条件注入

@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 "";
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号