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

租车网站制作新网域名注册

租车网站制作,新网域名注册,北京商场skp,网站备案主体黑名单引言 springboot的自动装配是其重要特性之一&#xff0c;在使用中我们只需在maven中引入需要的starter&#xff0c;然后相应的Bean便会自动注册到容器中。例如&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spr…

引言

springboot的自动装配是其重要特性之一,在使用中我们只需在maven中引入需要的starter,然后相应的Bean便会自动注册到容器中。例如:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
@Resource
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

如何不让某些类自动装配

  1. 可以通过exclude属性将那些不想自动装配的类排除在外
  2. 如果你不能访问那些想排除的类可以使用exclueName属性指定类全名
  3. 也可以使用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());
}
  1. 入参AnnotationMetadata里面记录着@Import注解的元数据,例如该注解所标记的类
  2. 判断自动装配是否关闭,如果关闭就返回空数组,自动装配可以通过spring.boot.enableautoconfiguration=false关闭,默认是开启,关闭后将无法自动装配
  3. 加载AutoConfigurationMetadata,其实就是将META-INF/spring-autoconfigure-metadata.properties文件下的属性加载进来,过滤时使用。

存储着待自动装配候选类的过滤计算规则,框架会根据文件中的规则逐个判断候选类是否需要自动装配进容器 作用TODO

  1. 调用getAutoConfigurationEntry获取需要加载入容器的类

getAutoConfigurationEntry()

protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,AnnotationMetadata annotationMetadata) {if (!isEnabled(annotationMetadata)) {return EMPTY_ENTRY;}AnnotationAttributes attributes = getAttributes(annotationMetadata);List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);configurations = removeDuplicates(configurations);Set<String> exclusions = getExclusions(annotationMetadata, attributes);checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations = filter(configurations, autoConfigurationMetadata);fireAutoConfigurationImportEvents(configurations, exclusions);return new AutoConfigurationEntry(configurations, exclusions);
}
  1. 获得@EnableAutoConfiguration注解的属性,即exclude和excludeName属性的值
  2. 获得那些可能会被考虑的XXXAutoConfiguration类的类名,从META-INF/spring.factory的org.springframework.boot.autoconfigure.EnableAutoConfiguration属性获得
  3. 将获得的XXXAutoConfiguration类名去重
  4. 去掉1中获得的exclude和excludeName属性中的类,另外将配置文件中的spring.autoconfigure.exclude属性配置的类也去掉
  5. 过滤
  6. 触发自动配置事件

filter()

过滤即是通过规则将待注入的AutoConfiguration类进行筛选,将符合条件的留下。过滤的过程如下:

private List<String> filter(List<String> 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;}List<String> 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);
}
  1. 入参configurations为待过滤的所有AutoConfigration类,autoConfigurationMetadata为过滤规则
  2. 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个过滤器的作用:

  1. OnClassCondition 指定的类在已经被加载到jvm中,通过classLoader.loadClass(className)或Class.forName(className)获取到类即可
  2. OnBeanCondition 指定的bean已经被加载到容器中
  3. 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) {// We'll 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/238389/

相关文章:

  • 阿里巴巴网站国际站建设seo托管服务
  • 企业网站优化之如何做需求分析网奇seo赚钱培训
  • 施工企业会计制度收入确认规定百度自然排名优化
  • 校园网站建设意义网络营销的特点有哪些
  • 内江做网站哪里便宜google搜索关键词热度
  • 福建省建设银行招聘网站网络推广员压力大吗
  • 动态网站订单怎么做搜索引擎优化营销
  • html5行业网站最近有哪些新闻
  • 做网站业务的怎么寻找客户在哪里打广告效果最好
  • 广东深圳seo服务内容
  • 做网站怎么备案网络服务有限公司
  • 网站主页特效欣赏百度官网下载电脑版
  • php mysql开发网站开发任何小说都能搜到的软件
  • the7 wordpress主题宁波seo外包费用
  • 云南建筑培训网seo刷点击软件
  • 男女做暖网站h5页面制作平台
  • 可以做puzzle的网站百度关键词排名提升工具
  • 竞网网站建设南宁网站seo大概多少钱
  • 114黄页信息网宝鸡seo培训
  • 东南亚做棋牌网站挖掘爱站网
  • 中国工程建设招标网官方网站谷歌查询关键词的工具叫什么
  • wordpress管理员密码忘记成都seo招聘
  • 武汉企业建站系统模板下载官方正版百度
  • 上海做网站国际财经新闻
  • 用废旧盒子做家用物品网站seo排名工具
  • 企业铭做网站域名解析在线查询
  • 怎么注册自己的小程序网站优化分析
  • 荆州网站建设流程网站设计培训
  • 网站支付怎么做的seo职业技能培训班
  • 做csgo直播网站上海知名网站制作公司