当前位置: 首页 > news >正文

租房网站深圳市外贸网站

租房网站,深圳市外贸网站,彩页设计模板,广州网站建设设计公司信息Conditional条件装配 Conditional是Spring Framework提供的一个核心功能注解#xff0c;这个注解的作用是提供自动装配的条件限制#xff0c;一般我们在用Configuration#xff0c;Bean的时候使用它。也就是我们在自定义Bean的注入的时候#xff0c;我们可以通过Condition…Conditional条件装配 Conditional是Spring Framework提供的一个核心功能注解这个注解的作用是提供自动装配的条件限制一般我们在用ConfigurationBean的时候使用它。也就是我们在自定义Bean的注入的时候我们可以通过Condition来对bean的注入增加逻辑判断符合我们要求的我们才让他自动装配 Conditional 的使用 如下注解源码是Condition FunctionalInterface public interface Condition {/*** Determine if the condition matches.* param context the condition context* param metadata metadata of the {link org.springframework.core.type.AnnotationMetadata class}* or {link org.springframework.core.type.MethodMetadata method} being checked* return {code true} if the condition matches and the component can be registered,* or {code false} to veto the annotated components registration*/boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);}condition是一个函数式接口提供了一个matches方法主要提供一个条件匹配的规则返回表示是否可以注入Bean。 Conditional 的注解来声明如下他可以接受接收一个Condition的数组 Target({ElementType.TYPE, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented public interface Conditional {/*** All {link Condition Conditions} that must {linkplain Condition#matches match}* in order for the component to be registered.*/Class? extends Condition[] value();}Conditional使用Demo 我们通过对以上Conditional的了解来自己实现一个Bean的按自定义条件的装配定义一个Condition判断系统来返回是否装配 /*** Created by jiamin5 on 2022/3/10.*/ public class GpCondition implements Condition{private static final Logger logger LoggerFactory.getLogger(GpCondition.class);Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {String os context.getEnvironment().getProperty(os.name);logger.info(GpCondition matthes osName:{}, os);if(os.contains(Windows)){return true;}return false;} }定义配置类装载一个BeanTestClass /*** Created by jiamin5 on 2022/3/10.*/ Configuration public class ConditionConfig {BeanConditional(GpCondition.class)public BeanTestClass beanTestClass(){return new BeanTestClass();} }如上我们在BeanTestClass上增加了一个Conditional(GpCondition.class)其中的具体条件就是我们自定义的注入限制条件类。意思就是当条件满足我们定义的GpCondition的时候我们就注入否则不注入。依然在Application启动类中添加对新定义Bean的获取如下 /*** author liaojiamin* Date:Created in 11:08 2022/3/7*/ SpringBootApplication EnableAutoImport public class Application {public static void main(String[] args) {ConfigurableApplicationContext ca SpringApplication.run(Application.class, args);System.out.println(ca.getBean(FilterFirstObj.class));BeanTestClass beanTestClass ca.getBean(BeanTestClass.class);System.out.println(beanTestClass);} } 当我们在Mac os上运行有如下结果 Spring Boot 中的Conditional 在SpringBoot中有针对Conditional的扩展提供了更简单的使用方式扩张了各种类型的注解如下 ConditionalOnBean/ConditionalOnMissBean 容器中存在或者不存在某个类的时候进行Bean加载ConditionalOnClass/ConditionalOnMissClassclasspath下存在或者不存在指定类的时候进行Bean加载ConditionalOnCloudPlatform只允许在指定的云平台上才加载指定BeanConditionalOnExpression基于SpEl表达式的条件判断ConditionalOnJava只允许在指定版本Java才加载BeanConditionalOnJndi只有指定资源通过JNDI加载后才加载BeanConditionalOnWebApplication/ConsitionalOnNotWebApplication如果是活着不是web应用才加载指定的BeanConditionalOnProperty系统中指定对呀的属性是否有对应值ConditionOnResource要加载的Bean依赖指定资源是否存在于classpathConditionOnSingleCandidate只有在确定了给定Bean的单个候选项时候才会加载Bean 以上这些都在spring-boot-autoconfiguration.jar 中 其他注入方式spring-autoconfigure-metadata 除了Conditional注解。Spring Boot中提供了spring-autoconfigure-metadata.properties文件来实现批量自动装配条件配置 与Conditional意义只是将条件放在的文件我们可以在spring-boot-autoconfigure.jar中找到这种配置 同样遵循“约定由于配置”通过这种配置实现条件过滤需保证两个前提条件 如上图中文件路径名称必须是/META-INF/spring-autoconfigure-metadata.properties配置文件的key’的配置格式自动配置类的类全路径名.条件 值 这种配置优点在于可以有效降低Spring Boot启动时间通过这种过滤方式减少配置陪的加载数量因为这个过滤发生在配置类的装载之前所以他可以降低Spring Boot启动时装载Bean的耗时 上一篇SpringBoot自动装配源码解析
http://www.sadfv.cn/news/195220/

相关文章:

  • h5购物网站模板免费注册163邮箱帐号
  • 15年做那些网站能致富中国风电商网站建设
  • 写作挣钱的网站公司简介模板word
  • 会所网站模板网站基础三要素
  • 沈阳网站建设 房小二网站建设的会计分录
  • 手机自助建站平台免费wordpress 提交插件
  • wordpress开启自定义字段青岛网站建设优化
  • 做网站的电话网站建设工具哪家好
  • 做图片推广的网站有哪些石家庄站内换乘示意图
  • 酒店专业培训网站建设百度登陆
  • 北京模板网站建站英文医疗网站建设
  • 网页网站开发项目设计湖南网站seo营销多少费用
  • 安康市城市建设局网站网站的首页怎么做
  • 做招投标应该了解的网站技术外包
  • 免费做网站推广引擎搜索器
  • 有名的淘宝客网站网页网站原型图占位符怎么做
  • 上传宝贝网站建设属于什么类目乐陵seo外包信德
  • 网站可以制作ios做住宿的有几个网站
  • 深圳龙华新区网站建设室内设计接单网站
  • 毕设代做网站大气宽屏网站模板
  • 加密的网站使用jmeter做压测c2c定义
  • 网站建设 微盘下载益阳营销网站建设
  • 做1688网站运营工资怎么样网站开发天津网站开发
  • 网站页头尺寸网站建设主持词
  • 网站logo是什么吉林刷关键词排名优化软件
  • 电影院网站建设方案网站制作的重要流程
  • 做时时的网站html5教程百度云
  • wordpress任务网站无线网络管理平台
  • 网站建设公司广告语自己做的美食分享到网站
  • 随州程力网站建设莱阳网站开发