Spring Boot 3 Configuration

1、导入配置额外类

You need not put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

不需要将所有的@Configuration放入单个类中。@Import注释可用于导入其他配置类。或者,您可以使用@ComponentScan来自动挑选所有Spring组件,包括@Configuration类。

2、导入XML配置文件

If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an @ImportResource annotation to load XML configuration files.

 如果必须使用基于XML的配置,我们建议您仍然从@Configuration类开始。然后可以使用@ImportResource注释来加载XML配置文件。

3、Auto-configuration

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.

3.1 禁用特定的Auto-configuration类

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

如果想要禁用特定 auto-configuration 类,可以使用 @SpringBootApplicationexclude属性

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

}
3.2 逐步取代自动配置

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.

Auto-configuration 是非侵入式的。 在任何时候都可以使用自定义的配置来取代 auto-configuration。例如,如果添加自己的DataSource bean,默认的嵌入式数据库支持就会消失。

如果需要找出当前正在应用的自动配置以及原因,请使用--debug开关启动应用程序。这样做可以为选定的核心记录器启用调试日志,并将条件报告记录到控制台。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值