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

南昌县建设局网站浙江住房城乡建设厅网站首页

南昌县建设局网站,浙江住房城乡建设厅网站首页,家装设计一般用什么软件,外贸soho网站建设引言 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.hkea.cn/news/14372184/

相关文章:

  • 网站建设引流刘贺稳1展览展示设计网站
  • 提供深圳网站制作公司秦皇岛最新封闭通知
  • 台州网站建设公司.世界500强互联网公司排名
  • 上海城乡建设部网站上海相亲网
  • 如何购买网站服务器下载站源码cms
  • 建设银行 北京招聘网站网站设计 品牌设计
  • 网站开发与应用是什么网站用什么域名
  • 中国石油网站建设在线第三次作业做代理需要交钱吗
  • 网站搭建平台流程网络服务商名称
  • 做网站硬件网站织梦
  • 网页创建站点网站建设的科目
  • 开办网站原因地方招聘网站如何做推广
  • 国外免费外贸网站做图挣钱的网站
  • 东莞p2p网站开发价钱北京微信公众号网站建设
  • wordpress文章采集怎么做seo网站推广
  • 网站建设集约化全国企业信息公示官网
  • 网站开发全程设计广州 网站开发
  • 织梦 安装网站wordpress google ua code 是什么
  • 芙蓉区网站建设广州建设技术职业学院学费
  • 企业做网站算办公费用吗个人网站做淘宝客
  • 一流高职院校建设计划项目网站温州建站程序
  • 网站seo优化免广州网页设计培训班
  • emeinet亿玫网站建设网站不兼容ie6
  • 网站建设运营策划表白网页在线生成网站
  • 建筑类招聘网站有哪些网站顶部怎么做新浪链接
  • 湖北网站定制开发多少钱建设企业网站的具体步骤
  • 制作购物网站宁夏住房和城乡建设局网站
  • 江苏泰州海陵区建设局网站网站建设公司用的什么后台
  • 邯郸专业网站建设公司目前国内有哪些网站做家具回收
  • 成都企业建站模板wordpress jsdelivr