Spring Boot中自动配置原理-1
重点:
1. SpringBoot启动会加载大量的自动配置类;
2. 看我们需要的功能有没有SpringBoot默认写好的自动配置类;
3. 再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件有,我们就不需要再来配置了)
4. 给容器中自动配置类添加组件的时候,会从properties类中获取某些属性;我们可以在配置文件中指定这些属性的值。
**xxxxAutoConfigurartion:自动配置类:给容器中添加组件;
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({MessageDispatcherServlet.class})
@ConditionalOnMissingBean({WsConfigurationSupport.class})
@EnableConfigurationProperties({WebServicesProperties.class})
@AutoConfigureAfter({ServletWebServerFactoryAutoConfiguration.class})
public class WebServicesAutoConfiguration {
public WebServicesAutoConfiguration() {
}
@Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext, WebServicesProperties properties) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
xxxxProperties:封装配置文件中相关属性。**
@ConfigurationProperties(
prefix = "spring.webservices"
)
public class WebServicesProperties {
private String path = "/services";
private final WebServicesProperties.Servlet servlet = new WebServicesProperties.Servlet();
public WebServicesProperties() {
}
public String getPath() {
return this.path;
}