Spring Boot 排除自动配置

Spring Boot 排除自动配置

Spring Boot 自动配置非常强大,有时需要排除/禁用 Spring Boot 某些类的自动化配置。

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
方式1

@SpringBootApplication 注解使用时,使用 exclude 属性进行排除指定的类:

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

当自动配置类不在类路径下时,使用 excludeName 属性进行排除指定的类名全路径:

@SpringBootApplication(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"})
public class Application {
	// ...
}
方式2

配置文件中指定参数 spring.autoconfigure.exclude 进行排除:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

spring:     
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
      - org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

你可能感兴趣的:(面试,学习路线,阿里巴巴,spring,boot,java,mybatis)