主要提供一下方法
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
true:表示装配
false:表示不装配
注解:Conditional() 参数是数组,数组内的都是 true 才装配
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
/**
* All {@link Condition}s that must {@linkplain Condition#matches match}
* in order for the component to be registered.
*/
Class<? extends Condition>[] value();
}
View Code
通常配合使用.
示例:
接口:EncodingConvert
package com.lhx.spring.springboot_auto_config;
public interface EncodingConvert {
}
View Code
接口实现一:UTF8EncodingConvert
package com.lhx.spring.springboot_auto_config;
public class UTF8EncodingConvert implements EncodingConvert {
}
View Code
接口实现二:GBKEncodingConvert
package com.lhx.spring.springboot_auto_config;
public class GBKEncodingConvert implements EncodingConvert {
}
View Code
配置类:
package com.lhx.spring.springboot_auto_config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@SpringBootConfiguration
public class EncodingConvertConfiguration {
@Bean
public EncodingConvert createGBKEncodingConvert() {
return new GBKEncodingConvert();
}
@Bean
public EncodingConvert createUTF8EncodingConvert() {
return new UTF8EncodingConvert();
}
}
View Code
App:
package com.lhx.spring.springboot_auto_config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.Conditional;
@SpringBootApplication
public class App {
@Bean
public Runnable createRunnable() {
return () -> {
System.out.println("spring boot is running");
};
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
context.getBean(Runnable.class).run();
//可以通过启动参数修改-Dfile.encoding=GBK
System.out.println(System.getProperty("file.encoding"));
System.out.println(context.getBeansOfType(EncodingConvert.class));
context.close();
}
}
View Code
此时,会发现连个接口实现都会被装配进来.
UTF8Condition 实现 Condition 接口
package com.lhx.spring.springboot_auto_config;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class UTF8Condition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String encoding = System.getProperty("file.encoding");
if (encoding != null) {
return "utf-8".equalsIgnoreCase(encoding);
}
return false;
}
}
View Code
GBKCondition 实现 Condition 接口
package com.lhx.spring.springboot_auto_config;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class GBKCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String encoding = System.getProperty("file.encoding");
if (encoding != null) {
return "gbk".equalsIgnoreCase(encoding);
}
return false;
}
}
View Code
修改配置类
package com.lhx.spring.springboot_auto_config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@SpringBootConfiguration
public class EncodingConvertConfiguration {
@Bean
@Conditional(GBKCondition.class)
public EncodingConvert createGBKEncodingConvert() {
return new GBKEncodingConvert();
}
@Bean
@Conditional(UTF8Condition.class)
public EncodingConvert createUTF8EncodingConvert() {
return new UTF8EncodingConvert();
}
}
View Code
此时,会发现会根据 file.encoding 值来装配接口类.
可在启动参数增加
-Dfile.encoding=GBK
然后调试,发现装配类也变了
注意:@Conditional 也可以作用在类上
二,Spring 提供的 Conditional 自动配置
jar:spring-boot-autoconfigure 中,org.springframework.boot.autoconfigure.condition; 即 spring-boot 提供
ConditionalOnBean:当存在某个 bean 时候装配
ConditionalOnMissingBean:当不存在某个 bean 时候装配
ConditionalOnClass:当 classpath 有才装配
ConditionalOnExpression:
ConditionalOnJava:JDK 版本符合时候才装配
ConditionalOnNotwebApplication:不是 web 环境才装配
ConditionalOnWebApplication:是 web 环境才装配
ConditionalOnResource:资源存在才装配
ConditionalOnProperty:配置存在才装配
示例一:ConditionalOnProperty 配置存在匹配时候才配置
增加配置类
package com.lhx.spring.springboot_auto_config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@SpringBootConfiguration
public class UserConfiguration {
@Bean
@ConditionalOnProperty(name = "runnable.enable", havingValue = "true")
public Runnable createRunnable() {
return () -> {
};
}
}
View Code
App2 编写
package com.lhx.spring.springboot_auto_config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.Conditional;
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App2.class, args);
System.out.println(context.getBeansOfType(Runnable.class));
context.close();
}
}
View Code
默认是不能装配的
可以再 application.properties 中添加 runnable.enable=true 即可装配
或者 @ConditionalOnProperty(name ="runnable.enable", havingValue ="true") 增加
matchIfMissing=true,表示配置没有的时候也生效
示例二:ConditionalOnClass classpath 有某个类才装配
增加或删除 maven,查看效果
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
使用代码
@Bean
@ConditionalOnClass(name="com.google.gson.Gson")
public Runnable createGsonRunnable() {
return () -> {
};
}
示例三,ConditionalOnBean:根据容器中是否存在某个 Bean 进行装配
@Bean
@ConditionalOnBean(name="user")
public Runnable createOnBeanRunnable() {
return () -> {
};
}
来源: http://www.bubuko.com/infodetail-2471191.html