Spring Boot 运行原理之自动配置

   

  我们知道Springboot提倡的是零配置,但是在我们开发工作中又是不可避免出现第三方软件的结合,需要我们对这些第三方的软件进行一些结合性或必要的属性进行赋值,而这时Spring boot会帮我们自动进行默认值的配置,避免我们亲自操刀。

 

   SpringBoot关于自动配置的源码在Spring-boot-autoconfigure-1.3,可以在项目中直接查看:


                              


    我们可以通过在application.properties属性中输入:

Debug=true


   这个属性来使Spring Boot运行时打印出所有的配置属性,比如我有一个ORM使用Mybaite的项目,其中

全局文件中定义的属性为:

mybatis.type-aliases-package=com.neo.entity
 
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
//运行时输出自动配置信息
debug=true


 

    运行项目,在Console中打印出多余信息:

 

*****************************************************************************************

启动的自动配置

*****************************************************************************************

=========================
AUTO-CONFIGURATIONREPORT
=========================
 
 
Positive matches:
-----------------
 
   DataSourceAutoConfiguration matched
      - @ConditionalOnClass found requiredclasses 'javax.sql.DataSource','org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'(OnClassCondition)
 
  DataSourceAutoConfiguration#dataSourceInitializer matched
      - @ConditionalOnMissingBean (types:org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer;SearchStrategy: all) did not find any beans (OnBeanCondition)
 
  DataSourceAutoConfiguration.PooledDataSourceConfiguration matched
      - AnyNestedCondition 1 matched 1 did not;NestedCondition onDataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailablePooledDataSource found supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType@ConditionalOnProperty (spring.datasource.type) did not find property 'type'(DataSourceAutoConfiguration.PooledDataSourceCondition)
      - @ConditionalOnMissingBean (types:javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) did not findany beans (OnBeanCondition)
 
   DataSourceConfiguration.Tomcat matched
      - @ConditionalOnClass found requiredclass 'org.apache.tomcat.jdbc.pool.DataSource' (OnClassCondition)
      - @ConditionalOnProperty(spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource) matched(OnPropertyCondition)
 
  DataSourcePoolMetadataProvidersConfiguration.TomcatDataSourcePoolMetadataProviderConfigurationmatched
      - @ConditionalOnClass found requiredclass 'org.apache.tomcat.jdbc.pool.DataSource' (OnClassCondition)
 
  DataSourceTransactionManagerAutoConfiguration matched
      - @ConditionalOnClass found requiredclasses 'org.springframework.jdbc.core.JdbcTemplate','org.springframework.transaction.PlatformTransactionManager' (OnClassCondition)
 
  DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfigurationmatched
      - @ConditionalOnSingleCandidate (types:javax.sql.DataSource; SearchStrategy: all) found a primary bean from beans'dataSource' (OnBeanCondition)
 
  DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfiguration#transactionManagermatched
      - @ConditionalOnMissingBean (types:org.springframework.transaction.PlatformTransactionManager; SearchStrategy:all) did not find any beans (OnBeanCondition)
 
  DataSourceTransactionManagerAutoConfiguration.TransactionManagementConfigurationmatched
      - @ConditionalOnMissingBean (types:org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration;SearchStrategy: all) did not find any beans (OnBeanCondition)
 
   DevToolsDataSourceAutoConfiguration matched
      - DevTools DataSource Condition foundauto-configured DataSource(DevToolsDataSourceAutoConfiguration.DevToolsDataSourceCondition)
 
   DispatcherServletAutoConfiguration matched
      - @ConditionalOnClass found requiredclass 'org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - @ConditionalOnWebApplication (required)found StandardServletEnvironment (OnWebApplicationCondition)
 
  DispatcherServletAutoConfiguration.DispatcherServletConfigurationmatched
      - @ConditionalOnClass found requiredclass 'javax.servlet.ServletRegistration' (OnClassCondition)
      - Default DispatcherServlet did not finddispatcher servlet beans(DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)
 
  DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfigurationmatched
      - @ConditionalOnClass found requiredclass 'javax.servlet.ServletRegistration' (OnClassCondition)
      - DispatcherServlet Registration did notfind servlet registration bean(DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition)
 
  DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistrationmatched
      - @ConditionalOnBean (names:dispatcherServlet; types: org.springframework.web.servlet.DispatcherServlet;SearchStrategy: all) found beans 'dispatcherServlet', 'dispatcherServlet'(OnBeanCondition)
 
   EmbeddedServletContainerAutoConfigurationmatched
      - @ConditionalOnWebApplication (required)found StandardServletEnvironment (OnWebApplicationCondition)
 
  EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat matched
      - @ConditionalOnClass found requiredclasses 'javax.servlet.Servlet', 'org.apache.catalina.startup.Tomcat'(OnClassCondition)
      - @ConditionalOnMissingBean (types:org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;SearchStrategy: current) did not find any beans (OnBeanCondition)
 
   ErrorMvcAutoConfiguration matched
      - @ConditionalOnClass found requiredclasses 'javax.servlet.Servlet','org.springframework.web.servlet.DispatcherServlet' (OnClassCondition)
      - @ConditionalOnWebApplication (required)found StandardServletEnvironment (OnWebApplicationCondition)
 
  ErrorMvcAutoConfiguration#basicErrorController matched
      - @ConditionalOnMissingBean (types:org.springframework.boot.autoconfigure.web.ErrorController; SearchStrategy:current) did not find any beans (OnBeanCondition)
 
  ErrorMvcAutoConfiguration#conventionErrorViewResolver matched
      - @ConditionalOnBean (types:org.springframework.web.servlet.DispatcherServlet; SearchStrategy: all) foundbean 'dispatcherServlet'; @ConditionalOnMissingBean (types:org.springframework.boot.autoconfigure.web.DefaultErrorViewResolver;SearchStrategy: all) did not find any beans (OnBeanCondition)
 
   ErrorMvcAutoConfiguration#errorAttributesmatched
      - @ConditionalOnMissingBean (types:org.springframework.boot.autoconfigure.web.ErrorAttributes; SearchStrategy:current) did not find any beans (OnBeanCondition)
 
  ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration matched
      - ErrorTemplate Missing did not finderror template view (ErrorMvcAutoConfiguration.ErrorTemplateMissingCondition)
      - @ConditionalOnProperty(server.error.whitelabel.enabled) matched (OnPropertyCondition)
 
  ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration#beanNameViewResolvermatched
      - @ConditionalOnMissingBean (types:org.springframework.web.servlet.view.BeanNameViewResolver; SearchStrategy: all)did not find any beans (OnBeanCondition)
 
  ErrorMvcAutoConfiguration.WhitelabelErrorViewConfiguration#defaultErrorViewmatched
      - @ConditionalOnMissingBean (names:error; SearchStrategy: all) did not find any beans (OnBeanCondition)
 
   GenericCacheConfiguration matched
      - Cacheorg.springframework.boot.autoconfigure.cache.GenericCacheConfigurationautomatic cache type (CacheCondition)
 
   HttpEncodingAutoConfiguration matched
      - @ConditionalOnClass found requiredclass 'org.springframework.web.filter.CharacterEncodingFilter'(OnClassCondition)
      - @ConditionalOnWebApplication (required)found StandardServletEnvironment (OnWebApplicationCondition)
      - @ConditionalOnProperty(spring.http.encoding.enabled) matched (OnPropertyCondition)
 
  HttpEncodingAutoConfiguration#characterEncodingFilter matched
      - @ConditionalOnMissingBean (types:org.springframework.web.filter.CharacterEncodingFilter; SearchStrategy: all)did not find any beans (OnBeanCondition)
 
   HttpMessageConvertersAutoConfigurationmatched
      - @ConditionalOnCl
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值