spring boot 扩展之AutoConfigurationImportListener

    最近阅读spring boot源码时发现,发现当spring使用ConfigurationClassParser加载使用@Configuration注解类后,会使用AutoConfigurationImportSelector对加载的

@Configuration注解的类进行一次过滤。当AutoConfigurationImportSelector过滤完成后会自动加载类路径下Jar包中META-INF/spring.factories文件中 AutoConfigurationImportListener的实现类

并触发fireAutoConfigurationImportEvents事件。

 

 实现AutoConfigurationImportListener接口,当fireAutoConfigurationImportEvents事件被触发时,打印出已经注册到spring上下文中的@Configuration注解的类,打印出被阻止注册到spring

上下文中的@Configuration注解类

 1 package com.torlight;
 2 
 3 import org.springframework.boot.autoconfigure.AutoConfigurationImportEvent;
 4 import org.springframework.boot.autoconfigure.AutoConfigurationImportListener;
 5 
 6 public class SysoutAutoConfigurationImportListener implements
 7         AutoConfigurationImportListener {
 8 
 9     @Override
10     public void onAutoConfigurationImportEvent(
11             AutoConfigurationImportEvent event) {
12         
13         System.out.println("================output print registered configurations");
14         for(String str:event.getCandidateConfigurations()){
15             System.out.println(">>> the configuration => "+ str +" has registered ");
16         }
17         
18         System.out.println("================output print exclusions configurations");
19         for(String str:event.getExclusions()){
20             System.out.println(">>> the configuration =>"+ str +" has exclusion ");
21         }
22 
23     }
24 
25 }

在src/main/resources/META-INF/spring.factories 文件中增加

org.springframework.boot.autoconfigure.AutoConfigurationImportListener=com.torlight.SysoutAutoConfigurationImportListener

 

 

控制台输出:

================output print registered configurations
>>> the configuration => org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration has registered
>>> the configuration => org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.CacheStatisticsAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.InfoContributorAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.EndpointMBeanExportAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.MetricExportAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration has registered
>>> the configuration => org.springframework.boot.actuate.cloudfoundry.CloudFoundryActuatorAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.aop.AopAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration has registered
>>> the configuration => org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration has registered
>>> the configuration => org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration has registered
>>> the configuration => org.springframework.boot.devtools.autoconfigure.LocalDevToolsAutoConfiguration has registered
>>> the configuration => org.springframework.boot.devtools.autoconfigure.RemoteDevToolsAutoConfiguration has registered
================output print exclusions configurations
>>> the configuration =>org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration has exclusion

posted @ 2017-05-14 21:44  来自非洲大草原的食人虎  阅读(3017)  评论(0编辑  收藏  举报