SpringBoot conditional注解和自定义conditional注解使用
conditional注解是Springboot starter的基石,自動裝配的時候會根據條件確定是否需要注入這個類。
含義:基于條件的注解。
作用:根據是否滿足某個特定條件來決定是否創建某個特定的Bean。
意義:Springboot實現自動配置的關鍵基礎能力。
@Conditional相關注解
@ConditionalOnBean:僅僅在當前上下文中存在某個對象時,才會實例化一個Bean
@ConditionalOnClass:某個class位于類路徑上才會實例化一個Bean
@ConditionalOnExpression:當表達式為true的時候,才會實例化一個Bean
@ConditionalOnMissingBean:僅僅在當前上下文中不存在某個對象時,才會實例化一個Bean
@ConditionalOnMissingClass:某個class不位于類路徑上才會實例化一個Bean
@ConditionalOnNotWebApplication:不是web應用
?
配置文件有這個配置
spring.data.mongodb.uri=mongodb://paopaoedu:paopaoedu@localhost:27017/paopaoedu
判斷有這個配置才注入這個類?
package com.paopaoedu.springboot.condition;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;@Component
@ConditionalOnProperty("spring.data.mongodb.uri")
public class ConditionalTest {
}
?
測試文件
package com.paopaoedu.springboot;import com.paopaoedu.springboot.condition.ConditionalTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SpringBootLearningApplication.class})
//不用類似new ClassPathXmlApplicationContext()的方式,從已有的spring上下文取得已實例化的bean。通過ApplicationContextAware接口進行實現。
public class SpringBootLearningApplicationTests implements ApplicationContextAware {private ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext context) throws BeansException {applicationContext = context;}@Testpublic void testA() {System.out.println("testA>>>"+applicationContext.getBean(ConditionalTest.class));}}
關于ApplicationContextAware使用理解參考https://www.jianshu.com/p/4c0723615a52
可以看出這個ConditionalTest被注入了:
如果你去掉這個配置測試用例就會報錯找不到這個bean:
自定義conditional注解實現
?
自定義注解引入condition接口實現類
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(MyCondition.class)
public @interface MyConditionAnnotation {String[] value() default {};
}
實現一個自定義注解
實現Condition接口重寫matches方法,符合條件返回true
public class MyCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {String[] properties = (String[])metadata.getAnnotationAttributes("com.paopaoedu.springboot.condition.MyConditionAnnotation").get("value");for (String property : properties) {if (StringUtils.isEmpty(context.getEnvironment().getProperty(property))) {return false;}}return true;}
}
引入conditional注解
@Component
@MyConditionAnnotation({"spring.redis.master.host", "spring.redis.follow.host"})
public class ConditionalTest2 {
}
配置文件
# Redis服務器地址
spring.redis.master.host=r-xxx1.redis.rds.aliyuncs.com
# Redis服務器連接端口
spring.redis.master.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.master.password=# Redis服務器地址
spring.redis.follow.host=r-xxx2.redis.rds.aliyuncs.com
# Redis服務器連接端口
spring.redis.follow.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.follow.password=
修改測試用例
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SpringBootLearningApplication.class})
//不用類似new ClassPathXmlApplicationContext()的方式,從已有的spring上下文取得已實例化的bean。通過ApplicationContextAware接口進行實現。
public class SpringBootLearningApplicationTests implements ApplicationContextAware {private ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext context) throws BeansException {applicationContext = context;}@Testpublic void testA() {System.out.println("testA>>>"+applicationContext.getBean(ConditionalTest.class));System.out.println("testA>>>"+applicationContext.getBean(ConditionalTest2.class));}}
輸出
?
重要的調試斷點ClassPathScanningCandidateComponentProvider.scanCandidateComponents()
private Set<BeanDefinition> scanCandidateComponents(String basePackage) {Set<BeanDefinition> candidates = new LinkedHashSet<>();try {String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +resolveBasePackage(basePackage) + '/' + this.resourcePattern;Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);boolean traceEnabled = logger.isTraceEnabled();boolean debugEnabled = logger.isDebugEnabled();for (Resource resource : resources) {if (traceEnabled) {logger.trace("Scanning " + resource);}if (resource.isReadable()) {try {MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);if (isCandidateComponent(metadataReader)) {ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);sbd.setResource(resource);sbd.setSource(resource);if (isCandidateComponent(sbd)) {if (debugEnabled) {logger.debug("Identified candidate component class: " + resource);}candidates.add(sbd);}else {if (debugEnabled) {logger.debug("Ignored because not a concrete top-level class: " + resource);}}}else {if (traceEnabled) {logger.trace("Ignored because not matching any filter: " + resource);}}}catch (Throwable ex) {throw new BeanDefinitionStoreException("Failed to read candidate component class: " + resource, ex);}}else {if (traceEnabled) {logger.trace("Ignored because not readable: " + resource);}}}}catch (IOException ex) {throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);}return candidates;}
總結
以上是生活随笔為你收集整理的SpringBoot conditional注解和自定义conditional注解使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机换个主板要多少钱
- 下一篇: Springboot源码分析之内嵌tom