springboot auto-configuration 自动装配功能

springboot 自动配置介绍
在springboot官网给出了如下介绍:
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.

springboot的auto-configuration设计目的是自动的为你添加的依赖jar进行装配,例如:在你的环境中使用了HSQLDB,你自己不需要手动的进行任何数据连接配置,springboot会默认一个配置一个内存数据库

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

你要开启auto-configuration配置,只需要在你的配置类上添加@EnableAutoConfiguration或者@SpringBootApplication注解即可

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.

提醒:
你只需要添加起一个@SpringBootApplication 或者@EnableAutoConfiguration注解即可,我们推荐你只在你主要的配置类上添加这两个注解的其中一个就行

替换springboot的自动配置
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.

Auto-configguration 是非入侵性的,在任何时候,你都可以自定义你自己的配置信息,来替代springboot自动配置信息某一部分,例如,你添加了你自己的DataSource实体,那么默认的datasource将会失效

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.

如果你想知道哪些自动装配生效了,为什么生效,你可以使用debug模式启动你的项目,这样做会在控制台打印一些配置核心日志。

禁用某些自动配置
If you find that specific auto-configuration classes that you do not want are being applied, you can use the exclude attribute of @SpringBootApplication to disable them, as shown in the following example:

如果有些类是你不想他们在程序中通过auto-configuration应用的,你也可以在@SpringBootApplication 使用exclude属性来禁用他们,就像下边一样:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MyApplication {

}

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

如果这个类不在类路径中,你同样可以使用注解中excludeName 属性,并需要指定具体类的全路径加载进来。当然,如果你不喜欢使用@SpringBootApplication 比较喜欢使用@EnableAutoConfiguration也没有关系,exclude和excludeName属性依然支持,最后,你同样可以通过配置文件中spring.autoconfigure.exclude的配置来控制auto-configuration 装配的类。

后续会深入介绍springboot自动装配的执行过程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值