Spring支持按照条件来注入某些特定的bean,这也是SpringBoot实现自动化配置的底层方法,文中的示例代码讲解详细,需要的可以参考一下
简介
说明
本文用实例介绍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条件注入内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Spring示例讲解条件注入方法


- JSP页面间传值问题实例简析 2023-08-03
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- 深入了解Spring的事务传播机制 2023-06-02
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- Java中的日期时间处理及格式化处理 2023-04-18
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- Java实现顺序表的操作详解 2023-05-19
- JSP 制作验证码的实例详解 2023-07-30