Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射。
org.springframework.core.io.support.
SpringFactoriesLoader
.loadFactoryNames(
Class
>,
ClassLoader
)
public
static
List
<
String
> loadFactoryNames(
Class
> factoryClass,
ClassLoader
classLoader) {
String
factoryClassName = factoryClass.getName();
try
{
Enumeration
<URL> urls = (classLoader !=
null
? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
lassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
List
<
String
> result =
new
ArrayList
<
String
>();
while
(urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties
properties =
PropertiesLoaderUtils
.loadProperties(
new
UrlResource
(url));
String
factoryClassNames = properties.getProperty(factoryClassName);
result.addAll(
Arrays
.asList(
StringUtils
.commaDelimitedListToStringArray(factoryClassNames)));
}
return
result;
}
catch
(
IOException
ex) {
throw
new
IllegalArgumentException
(
"Unable to load ["
+ factoryClass.getName() +
"] factories from location ["
+ FACTORIES_RESOURCE_LOCATION +
"]"
, ex);
}
}
这个方法会加载类路径及所有jar包下META-INF/spring.factories配置中映射的自动配置的类。
/**
* The location to look for factories.
* Can be present in multiple JAR files.
*/
public
static
final
String
FACTORIES_RESOURCE_LOCATION =
"META-INF/spring.factories"
;
查看Spring Boot自带的自动配置的包: spring-boot-autoconfigure-1.5.6.RELEASE.jar,打开其中的META-INF/spring.factories文件会找到自动配置的映射。
# Auto Configure
org.springframework.boot.autoconfigure.
EnableAutoConfiguration
=\
org.springframework.boot.autoconfigure.admin.
SpringApplicationAdminJmxAutoConfiguration
,\
org.springframework.boot.autoconfigure.aop.
AopAutoConfiguration
,\
org.springframework.boot.autoconfigure.amqp.
RabbitAutoConfiguration
,\
org.springframework.boot.autoconfigure.batch.
BatchAutoConfiguration
,\
org.springframework.boot.autoconfigure.cache.
CacheAutoConfiguration
,\
org.springframework.boot.autoconfigure.cassandra.
CassandraAutoConfiguration
,\
org.springframework.boot.autoconfigure.cloud.
CloudAutoConfiguration
,\
...
再来看看数据源自动配置的实现注解
@Configuration
@ConditionalOnClass
({
DataSource
.
class
,
EmbeddedDatabaseType
.
class
})
@EnableConfigurationProperties
(
DataSourceProperties
.
class
)
@Import
({
Registrar
.
class
,
DataSourcePoolMetadataProvidersConfiguration
.
class
})
public
class
DataSourceAutoConfiguration
{
...
@Configuration,@ConditionalOnClass就是自动配置的核心,首先它得是一个配置文件,其次根据类路径下是否有这个类去自动配置。
添加配置类:
添加自动配置类:
创建META-INF/spring.factories文件,添加自动配置映射:
这样就搞定了。
怎么查看自己加的自动配置类有没有被加载,或者查看所有自动配置激活的和未激活的可以通过以下几种试查看。
如果集成了spring-boot-starter-actuator监控,通过autoconfig端点也可以查看。
启动后会在控制台看到以下自动配置报告信息:
Positive matches:已经启用的自动配置
Negative matches:未启用的自动配置
从报告中看到自己添加的EnvAutoConfig已经自动配置了。
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.
You should only ever add one |
|
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.
You can define exclusions both at the annotation level and by using the property. |
|