1. Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, if HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.

    You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes.

    Tip

    You should only ever add one @SpringBootApplication or @EnableAutoConfiguration annotation. We generally recommend that you add one or the other to your primary @Configuration class only.

  2. 16.1 Gradually(逐步) Replacing Auto-configuration

    Auto-configuration is non-invasive(非侵害的). At any point, you can start to define your own configuration to replace specific parts of the auto-configuration. For example, if you add your own DataSource bean, the default embedded database support backs away.

    If you need to find out what auto-configuration is currently being applied, and why, start your application with the --debug switch. Doing so enables debug logs for a selection of core loggers and logs a conditions report to the console.

    16.2 Disabling Specific Auto-configuration Classes

    If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of @EnableAutoConfiguration to disable them, as shown in the following example:

    import org.springframework.boot.autoconfigure.*;
    import org.springframework.boot.autoconfigure.jdbc.*;
    import org.springframework.context.annotation.*; @Configuration
    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
    public class MyConfiguration {
    }

    If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring.autoconfigure.exclude property.

    Tip

    You can define exclusions both at the annotation level and by using the property.

  3. @SpringBootApplication
    1. @SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
  4. @EnableAutoConfiguration
    1. 把 spring-boot-autoconfigure.jar/META-INF/spring.factories中每一个xxxAutoConfiguration文件都加载到容器中,spring.factories文件里每一个xxxAutoConfiguration文件一般都会有下面的条件注解:
      
      @ConditionalOnClass : classpath中存在该类时起效
      @ConditionalOnMissingClass : classpath中不存在该类时起效
      @ConditionalOnBean : DI容器中存在该类型Bean时起效
      @ConditionalOnMissingBean : DI容器中不存在该类型Bean时起效
      @ConditionalOnSingleCandidate : DI容器中该类型Bean只有一个或@Primary的只有一个时起效
      @ConditionalOnExpression : SpEL表达式结果为true时
      @ConditionalOnProperty : 参数设置或者值一致时起效
      @ConditionalOnResource : 指定的文件存在时起效
      @ConditionalOnJndi : 指定的JNDI存在时起效
      @ConditionalOnJava : 指定的Java版本存在时起效
      @ConditionalOnWebApplication : Web应用环境下起效
      @ConditionalOnNotWebApplication : 非Web应用环境下起效

        

  5. @ComponentScan
    1. @ComponentScan这个注解完成的是自动扫描的功能,相当于Spring XML配置文件中的:<context:component-scan>,可以使用basePackages属性指定要扫描的包,以及扫描的条件。如果不设置的话默认扫描@ComponentScan注解所在类的同级类和同级目录下的所有类,所以对于一个Spring Boot项目,一般会把入口类放在顶层目录中,这样就能够保证源码目录下的所有类都能够被扫描到。
    2. 总结一下@ComponentScan的常用方式如下

      • 自定扫描路径下边带有@Controller,@Service,@Repository,@Component注解加入spring容器;也会扫描 @Configuration

      • 通过includeFilters加入扫描路径下没有以上注解的类加入spring容器

      • 通过excludeFilters过滤出不用加入spring容器的类

  6. @Configuration
    1. 提到@Configuration就要提到他的搭档@Bean
    2. <beans>
      <bean id = "car" class="com.test.Car">
      <property name="wheel" ref = "wheel"></property>
      </bean>
      <bean id = "wheel" class="com.test.Wheel"></bean>
      </beans> @Configuration
      public class Conf {
      @Bean
      public Car car() {
      Car car = new Car();
      car.setWheel(wheel());
      return car;
      } @Bean
      public Wheel wheel() {
      return new Wheel();
      }
      }

        

Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)的更多相关文章

  1. Spring boot 官网学习笔记 - Configuration Class(@import)

    推荐使用 Java-based configuration ,也可以使用xml we generally recommend that your primary source be a single ...

  2. Spring boot 官网学习笔记 - logging

    commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...

  3. Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

  4. Spring boot 官网学习笔记 - Spring DevTools 介绍

    想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...

  5. Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利

    If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...

  6. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  7. Spring boot 官网学习笔记 - Spring Boot CLI 入门案例

    安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...

  8. Spring Boot的学习之路(02):和你一起阅读Spring Boot官网

    官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...

  9. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

随机推荐

  1. python 13 内置函数2

    目录 内置函数(二) 匿名函数 内置函数(三) 闭包 内置函数(二) abs() #返回绝对值--返回的是正数 enumerate("可迭代对象","序号起始值" ...

  2. 支持向量机 (一): 线性可分类 svm

    支持向量机(support vector machine, 以下简称 svm)是机器学习里的重要方法,特别适用于中小型样本.非线性.高维的分类和回归问题.本系列力图展现 svm 的核心思想和完整推导过 ...

  3. .NET CORE 怎么样从控制台中读取输入流

    .NET CORE 怎么样从控制台中读取输入流 从Console.ReadList/Read 的源码中,可学习到.NET CORE 是怎么样来读取输入流. 也可以学习到是如何使用P/Invoke来调用 ...

  4. 水果机抽奖(CocosCreator)

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321 一.前言       在前面给大家分享了大转盘的抽奖方式,这是现在游戏使用较多的一种抽奖方式,今天给大家介绍另一抽奖方式--水果 ...

  5. DOM选择器之元素选择器

    DOM中元素选择器 在DOM中我们可以将元素选择器分为两类:1.元素节点选择器:2.其它节点选择器.通过选择器选择HTML中的元素以对其进行操作,以此实现用JS对页面的操作. 一.元素节点选择器 1. ...

  6. Oracle数据库测试和优化最佳实践: OTest介绍 (转)

    当前Oracle数据库最佳测试工具OTest *  Otest是用于Oracle数据库测试.优化.监控软件. *  Otest是免费提供给Oracle客户和广大DBA工程师使用的软件.由原厂技术专家王 ...

  7. [Error] - Windows卸载程序时,提示错误2503

    1. 打开“任务管理器” 2. 切换到“详细信息”标签页,找到explorer.exe文件,并结束它. 3. 点击“任务管理器”上的文件->运行新任务,输入explorer.ext,勾选“以系统 ...

  8. HYSBZ - 4016 最短路径树问题 点分治 + 最短路径最小字典序

    题目传送门 题解:首先对于给定的图,需要找到那些从1好点出发然后到x号点的最短路, 如果有多条最短路就要找到字典序最小的路,这样扣完这些边之后就会有一棵树.然后再就是很普通的点分治了. 对于扣边这个问 ...

  9. JOBDU 1109 连通图

    题目1109:连通图 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4192 解决:2224 题目描述: 给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的. 输入: 每组数据 ...

  10. 牛客网 湖南大学2018年第十四届程序设计竞赛重现赛 A game

    链接:https://www.nowcoder.com/acm/contest/125/A来源:牛客网 Tony and Macle are good friends. One day they jo ...