自动装配(与配置文件的关系)
之前研究了springboot注解通过什么方式自动装配,并找到了自动装配的核心文件。那自动装配和我们配置文件之间是有什么样的联系呢?或者说我如何通过配置文件设置自动装配的各种功能呢?
打开spring.factories中任何一个类,都有一堆相似的注解:@ConditionalOnXxx
通过名字,可以理解为这种注解是用于判断的,满足条件是怎么样,点进去可以发现,他们有一个共同的注解@Conditional()
@Conditional(OnBeanCondition.class)
@Conditional(OnClassCondition.class)
在OnClassCondition类中有注释说“{@link Condition} and {@link AutoConfigurationImportFilter} that checks for the presence or absence of specific classes.”检查是否存在特定的类应该都是一类用法
关于@Conditionalxxx后面做了补充
于是就可以得出结论,这些自动装配的类是只有满足某些条件后才会自动装配。比如找到了某些相关基础类。
当点开spring.factories和我的配置文件对比的时候,我第一次设置的是server.port。
那么关于server的自动装配类是哪个呢,边猜边找,查询发现:
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ RSocketServer.class, RSocketStrategies.class, HttpServer.class, TcpServerTransport.class })
@ConditionalOnBean(RSocketMessageHandler.class)
@AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class)
@EnableConfigurationProperties(RSocketProperties.class)
public class RSocketServerAutoConfiguration {
}
当然,它是爆红的,也就是说么有生效。往下翻,可以发现它有一个内部类:
@ConditionalOnProperty(prefix = "spring.rsocket.server", name = "port")
@ConditionalOnClass(ReactorResourceFactory.class)
@Configuration(proxyBeanMethods = false)
static class EmbeddedServerConfiguration {
}
这个说,满足条件是有那个port 的prorperty配置,看样子是满足的,而且Configuration注释是spring中的配置类的注释,那么基本确定是这么回事。@EnableConfigurationProperties(RSocketProperties.class)那这个注解,应该是和自动配置类关联的properties类,点进去看,会发现他有server,port属性,应该就是我们使用的属性吧
@ConfigurationProperties("spring.rsocket")
public class RSocketProperties {
...
public Server getServer() {
return this.server;
}
public static class Server {
/**
* Server port.
*/
private Integer port;
...
}
...
}
简单来说,就是一个XxxAutoConfiguration类对应一个XxxProperties类,而Properties类中的属性就是我application配置文件可用的配置。比如port
接下来需要证明一下,我找一个比较熟悉的,比较简单的
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,
点进去看一下HttpEncodingAutoConfiguration 类
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ServerProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
private final Encoding properties;
...
}
ServerProperties类
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
...
private final Servlet servlet = new Servlet();
...
public static class Servlet {
...
@NestedConfigurationProperty
private final Encoding encoding = new Encoding();
...
}
...
}
一句话总结 :根据当前不同的条件判断,决定这个配置类是否生效!
一但这个配置类生效;这个配置类就会给容器中添加各种组件;
这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;
所有在配置文件中能配置的属性都是在xxxxProperties类中封装着;
配置文件能配置什么就可以参照某个功能对应的这个属性类
关于@ConditionalOnXxx扩展
有哪些
官方文档中有一张表
怎么判断
难道每次都要看源码吗(手动狗头)
我们可以通过启用 debug=true
属性;来让控制台打印自动配置报告,这样我们就可以很方便的知道哪些自动配置类生效;
Positive matches:(自动配置类启用的:正匹配)
Negative matches:(没有启动,没有匹配成功的自动配置类:负匹配)
Unconditional classes: (没有条件的类)
复习的时候看
1、SpringBoot启动会加载大量的自动配置类
2、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
3、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件存在在其中,我们就不需要再手动配置了)
4、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;
xxxxAutoConfigurartion:自动配置类;给容器中添加组件
xxxxProperties:封装配置文件中相关属性;