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

做打折的淘宝小卖家的网站重慶网站开发

做打折的淘宝小卖家的网站,重慶网站开发,网站的空间的提供商,wordpress应用教程引言 springboot的自动装配是其重要特性之一#xff0c;在使用中我们只需在maven中引入需要的starter#xff0c;然后相应的Bean便会自动注册到容器中。例如#xff1a; dependencygroupIdorg.springframework.boot/groupIdartifactIdspr…引言 springboot的自动装配是其重要特性之一在使用中我们只需在maven中引入需要的starter然后相应的Bean便会自动注册到容器中。例如 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-amqp/artifactId /dependencyResource private RabbitTemplate rabbitTemplate;此时我们便可以注入rabbitTemplate使用相应的功能了。本篇文章将探究springboot如何实现的自动装配(基于2.2.5.RELEASE)。 EnableAutoConfiguration 在SpringBootAppilication的注解上有一个EnableAutoConfiguration注解这个注解完成了自动装配的功能。 Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented Inherited AutoConfigurationPackage Import(AutoConfigurationImportSelector.class) public interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY spring.boot.enableautoconfiguration;/*** Exclude specific auto-configuration classes such that they will never be applied.* return the classes to exclude*/Class?[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* return the class names to exclude* since 1.3.0*/String[] excludeName() default {};}EnableAutoConfiguration通过Import注解将AutoConfigurationImportSelector中selectImports方法返回的类注册到容器中。EnableAutoConfiguration 会尝试猜测并配置那些你需要的bean。通常根据classpath和你定义的bean来决定注册哪些类。例如如果你的classpath有tomcat-embed相关的jar那么你就可能想要TomcatServletWebServerFactory这个类的实例。 因为当使用SpringBootApplication时自动装配就会自动生效所以再使用这个注解将不会再起作用。 自动装配bean的过程将在用户自定义的bean被注册到容器之后进行 每个starter对应一个XXXAutoConfiguration类其中定义了一些需要的bean 如何不让某些类自动装配 可以通过exclude属性将那些不想自动装配的类排除在外如果你不能访问那些想排除的类可以使用exclueName属性指定类全名也可以使用spring.autoconfigure.exclude.property属性 AutoConfigurationImportSelector selectImports() 将selectImports方法重写为 Override public String[] selectImports(AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return NO_IMPORTS;}AutoConfigurationMetadata autoConfigurationMetadata AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AutoConfigurationEntry autoConfigurationEntry getAutoConfigurationEntry(autoConfigurationMetadata,annotationMetadata);return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); }入参AnnotationMetadata里面记录着Import注解的元数据例如该注解所标记的类判断自动装配是否关闭如果关闭就返回空数组自动装配可以通过spring.boot.enableautoconfigurationfalse关闭默认是开启关闭后将无法自动装配加载AutoConfigurationMetadata其实就是将META-INF/spring-autoconfigure-metadata.properties文件下的属性加载进来过滤时使用。 存储着待自动装配候选类的过滤计算规则框架会根据文件中的规则逐个判断候选类是否需要自动装配进容器 作用TODO 调用getAutoConfigurationEntry获取需要加载入容器的类 getAutoConfigurationEntry() protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return EMPTY_ENTRY;}AnnotationAttributes attributes getAttributes(annotationMetadata);ListString configurations getCandidateConfigurations(annotationMetadata, attributes);configurations removeDuplicates(configurations);SetString exclusions getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationEntry(configurations, exclusions); }获得EnableAutoConfiguration注解的属性即exclude和excludeName属性的值获得那些可能会被考虑的XXXAutoConfiguration类的类名从META-INF/spring.factory的org.springframework.boot.autoconfigure.EnableAutoConfiguration属性获得将获得的XXXAutoConfiguration类名去重去掉1中获得的exclude和excludeName属性中的类另外将配置文件中的spring.autoconfigure.exclude属性配置的类也去掉过滤触发自动配置事件 filter() 过滤即是通过规则将待注入的AutoConfiguration类进行筛选将符合条件的留下。过滤的过程如下 private ListString filter(ListString configurations, AutoConfigurationMetadata autoConfigurationMetadata) {long startTime System.nanoTime();String[] candidates StringUtils.toStringArray(configurations);boolean[] skip new boolean[candidates.length];boolean skipped false;for (AutoConfigurationImportFilter filter : getAutoConfigurationImportFilters()) {invokeAwareMethods(filter);boolean[] match filter.match(candidates, autoConfigurationMetadata);for (int i 0; i match.length; i) {if (!match[i]) {skip[i] true;candidates[i] null;skipped true;}}}if (!skipped) {return configurations;}ListString result new ArrayList(candidates.length);for (int i 0; i candidates.length; i) {if (!skip[i]) {result.add(candidates[i]);}}if (logger.isTraceEnabled()) {int numberFiltered configurations.size() - result.size();logger.trace(Filtered numberFiltered auto configuration class in TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) ms);}return new ArrayList(result); }入参configurations为待过滤的所有AutoConfigration类autoConfigurationMetadata为过滤规则getAutoConfigurationImportFilters()找出spring.factory中org.springframework.boot.autoconfigure.AutoConfigurationImportFilter对用的属性值作为过滤器,包括以下三个 # Auto Configuration Import Filters org.springframework.boot.autoconfigure.AutoConfigurationImportFilter\ org.springframework.boot.autoconfigure.condition.OnBeanCondition,\ org.springframework.boot.autoconfigure.condition.OnClassCondition,\ org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition如果有哪个XXAutoConfiguration类没有通过过滤则剔除出去。 下面将介绍这3个过滤器的作用 OnClassCondition 指定的类在已经被加载到jvm中通过classLoader.loadClass(className)或Class.forName(className)获取到类即可OnBeanCondition 指定的bean已经被加载到容器中OnWebApplicationCondition 当是某种web应用时才能通过条件目前都是SERVLET类型的web应用 以OnClassCondition为例其核心代码为 private ConditionOutcome getOutcome(String candidates) {try {if (!candidates.contains(,)) {return getOutcome(candidates, this.beanClassLoader);}for (String candidate : StringUtils.commaDelimitedListToStringArray(candidates)) {ConditionOutcome outcome getOutcome(candidate, this.beanClassLoader);if (outcome ! null) {return outcome;}} } catch (Exception ex) {// Well get another chance later } return null; }private ConditionOutcome getOutcome(String className, ClassLoader classLoader) {if (ClassNameFilter.MISSING.matches(className, classLoader)) {return ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnClass.class).didNotFind(required class).items(Style.QUOTE, className));}return null; }getOutCome即是获取匹配结果的方法最终通过ClassNameFilter.MISSING.matches(className, classLoader)使用反射获得是否有类加载到jvm中 protected static Class? resolve(String className, ClassLoader classLoader) throws ClassNotFoundException {if (classLoader ! null) {return classLoader.loadClass(className);}return Class.forName(className); }总结 springboot的自动装配本质上是通过Import(AutoConfigurationImportSelector.class)注解将spring.factories文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration属性下的XXXAutoConfig类加载到容器中当然不是全部加载要通过spring-autoconfigure-metadata.properties文件下
http://www.tj-hxxt.cn/news/136579.html

相关文章:

  • 网站制作报价维持地建网络挣钱最快的小游戏
  • 北京百度糯米团购有做网站的电话吗wordpress多文章
  • 网站建设的经费东台网络推广
  • 网站搜索优化怎么做网络电商培训课程网站设计
  • 京东在线购物网站php 手机网站开发教程
  • 公司网站属于信息化建设吗做网站如何分工
  • 建设田达摩托车官方网站小说百度搜索风云榜
  • 做公司网站都需要什么博山信息港
  • 响应式外贸网站价格朝阳网站建设 高碑店
  • 微网站开发不用模板网站的百度地图怎么做
  • 一个网站需要服务器吗桂林论坛网app
  • 黔东南手机网站建设国外教做蛋糕的网站
  • 怎么查网站注册信息浙江建站优化品牌
  • 南康建设局官方网站抖音seo优化系统招商
  • 贵州安顺建设主管部门网站ui界面
  • 城市建设者官方网站wordpress域名地址设置方法
  • 宁波慈溪网站建设电力行业企业网站建设
  • 可以做锚文本链接的网站搭建一个微信小程序要多少钱
  • 嘉祥做网站如何做电影网站
  • flash国外网站wordpress实现视频播放
  • 抚州律师网站建设哪个网站找到做箱包厂外发的
  • 一个网站怎么做软件网站空间多少
  • 宁夏众擎达网站建设小程序开发教程
  • 淘宝建设网站虚拟主机评测
  • 江苏营销型网站推广网站跳出率是什么意思
  • 满天星建设网站名人朋友圈网页版qq登录入口
  • 做旅游网站挣钱吗学网站开发的软件
  • 网站策划专有技术久久建筑网cad
  • 北京网站优化页面做网站卖大闸蟹
  • 网站运营策划提案静态网站入侵