一.SpringBoot自动配置jar包下的condition
引入的SpringBoot版本为 :?2.1.0.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
二.条件注解以及条件注解解释类
1.@Conditional
这是Spring4.0添加的新注解,用来标识一个SpringBean或者Configuration配置文件 当满足指定的条件才开启配置。
package com.xiaoxiaowang.springbootDemo.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* @author: xxw
* @create: 2022-10
*/
public class MyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return true;
}
}
@Conditional(MyCondition.class)
public XiaoXiaoService xiaoXiaoService2() {
System.out.println("XiaoXiaoService2======>instanced...");
// @ConditionalOnCloudPlatform(CloudPlatform.CLOUD_FOUNDRY) 是不是运行在某个云平台
return new XiaoXiaoService();
}
2.@ConditionalOnBean
当ApplicationContext存在某些bean时条件才成立,组合@OnBeanCondition使用
3.@ConditionalOnMissingBean
ApplicationContext不存在某些bean时条件才成立,组合@OnBeanCondition使用
4.@ConditionalOnClass
classPath中不存在某些Class时条件才成立,组合@OnClassCondition使用
5.@ConditionalOnMissingClass
classPath中不存在某些Class时条件才成立,组合@OnClassCondition使用
6.@ConditionalOnExpression
SPEL表达式(spring的el表达式)成立时条件才成立,组合@OnExpressionCondition使用
?在application.properties配置文件中定义变量
xiaoxiao.type=server
xiaoxiao.user.enable=true
xiaoxiao.order.size=10
@ConditionalOnExpression("'${xiaoxiao.type}'.equals('server')&& (${xiaoxiao.user.enable})")
@Bean
public MultiBean m1() {
System.out.println("=========>m1 instanced...");
return new MultiBean();
}
7.@ConditionalOnJava
JDK某些版本条件才成立,结合@OnJavaCondition使用
它的默认Range范围是 >= JDK的某个版本
@ConditionalOnJava(JavaVersion.EIGHT)
@Bean
public MultiBean m1() {
System.out.println("=========>m1 instanced...");
return new MultiBean();
}
?8.@ConditionalOnProperty
?Environment中存在某些配置项时才成立,结合@OnPropertyCondition使用
9.@ConditionalOnResource
存在某些资源时条件才成立,结合@OnResourceCondition使用
10.@ConditionalOnSingleCandidate
ApplicationContext中存在且只有一个bean时条件成立,结合@OnBeanCondition使用
11.@ConditionalOnJndi (不常用)
JNDI路径下存在时条件才成立,结合@OnJndiCondition使用
12.@ConditionalOnCloudPlatform?(不常用)
在某些云平台(Kubemetes、Heroku、Cloud Foundry)下条件才成立,结合@OnCloudPlatformCondition使用
13.@ConditionalOnWebApplication
在Web环境下条件才成立,结合@OnWebApplicationCondition使用
14.@ConditionalOnNotWebApplication
在非Web环境下条件才成立,结合@OnWebApplicationCondition使用
三.代码片段
自定义条件注解类和条件注解解析类
package com.xiaoxiaowang.springbootDemo.condition;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
/**
*
* 自定义条件注解
*
*
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(XiaoXiaoOnClassCondition.class)
public @interface XiaoXiaoConditionalOnClass {
String value() default "";
}
package com.xiaoxiaowang.springbootDemo.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import java.util.Map;
/**
* @author: xxw
* @create: 2022-10-18
*
* 自定义条件注解解析类
*
*/
public class XiaoXiaoOnClassCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// 拿到条件注解
Map<String, Object> annotationAttributes =
metadata.getAnnotationAttributes(XiaoXiaoConditionalOnClass.class.getName());
// 拿到注解上的value值
String className = (String) annotationAttributes.get("value");
try {
// 类加载器加载类,加载成功,返回true,否则返回false
context.getClassLoader().loadClass(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
测试代码片段 :
package com.xiaoxiaowang.springbootDemo;
import com.xiaoxiaowang.springbootDemo.bean.BeanTest;
import com.xiaoxiaowang.springbootDemo.bean.ExistBean;
import com.xiaoxiaowang.springbootDemo.bean.MultiBean;
import com.xiaoxiaowang.springbootDemo.condition.MyCondition;
import com.xiaoxiaowang.springbootDemo.condition.XiaoXiaoConditionalOnClass;
import com.xiaoxiaowang.springbootDemo.service.XiaoXiaoService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.system.JavaVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: xxw
* @create: 2022-10-18
*/
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@ConditionalOnJava(JavaVersion.EIGHT)
@Bean
public MultiBean m1() {
System.out.println("=========>m1 instanced...");
return new MultiBean();
}
@Bean
@XiaoXiaoConditionalOnClass("com.xiaoxiaowang.springbootDemo.bean.BeanTest")
@ConditionalOnClass(BeanTest.class)
@ConditionalOnBean(ExistBean.class)
@ConditionalOnExpression("'${xiaoxiao.type}'.equals('server') && (${xiaoxiao.user.enable})")
@ConditionalOnJava(JavaVersion.EIGHT)
@ConditionalOnProperty(prefix = "xiaoxiao.user", name = "enable", havingValue = "true")
@ConditionalOnResource(resources = {"jdbc.properties"})
@ConditionalOnSingleCandidate(ExistBean.class)
@Conditional(MyCondition.class)
public XiaoXiaoService xiaoXiaoService2() {
System.out.println("XiaoXiaoService2======>instanced...");
// @ConditionalOnCloudPlatform(CloudPlatform.CLOUD_FOUNDRY) 是不是运行在某个云平台
return new XiaoXiaoService();
}
// @Bean
// @XiaoXiaoConditionalOnClass("com.xiaoxiaowang.springbootDemo.bean.BeanTest1")
// public XiaoXiaoService xiaoXiaoService(){
// System.out.println("XiaoXiaoService======>instanced...");
// return new XiaoXiaoService();
// }
//
// @Bean
// @XiaoXiaoConditionalOnClass("com.xiaoxiaowang.springbootDemo.bean.BeanTest")
// @ConditionalOnClass(BeanTest.class)
// @ConditionalOnBean(BeanTest.class)
// public XiaoXiaoService xiaoXiaoService3(){
// System.out.println("XiaoXiaoService3======>instanced...");
// return new XiaoXiaoService();
// }
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
测试结果:
四.参考文档
1.https://www.hxstrive.com/subject/spring_boot/478.htm?
2.https://blog.csdn.net/qq_42651904/article/details/120994557
3.https://blog.csdn.net/qq_40041868/article/details/123264571
|