浅谈SpringBoot自动装配( 四 )

org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration

上面这个类在服务启动过程中,自动实现了相关WebServer的装配,

我们可以看到,内嵌的Tomcat是需要某些条件(Condition)才能注册成为一个Bean,假如我们把相关类排除再启动看一下,

@EnableAutoConfiguration(exclude = ServletWebServerFactoryAutoConfiguration.class)

public class AutoConfigurationBootStrap {

public static void main(String[] args) {

SpringApplication.run(AutoConfigurationBootStrap.class, args);

}

我们可以看到启动时出现异常。

总结:

从上面的文档简介或相关代码,可以看出SpringBoot的自动装配功能是通过 @EnableAutoConfiguration或@SpringBootApplication标注在某些类上,在服务启动过程中,会加载某一些类(META-INF/spring.factories)自动注册成为Bean,这样开发人员就无需写额外代码,就可以直接使用相关的Bean了。

推荐阅读