对Spring Boot而言,@SpringBootApplication的作用就是@Configuration, @EnableAutoConfiguration与@ComponentScan的集合,所以也会存在@EnableAutoConfiguration的粒度管理问题。
添加@EnableAutoConfiguration时(请务必注意,一个Spring Boot程序最好只添加唯一一个这样的注解),由于Spring Boot是根据程序加载的jar包自动添加配置,所以就会导致自动配置一些不必要的配置,性能浪费倒是小事,关键是控制力度与问题难以追踪。
基于以上的原因,一般而言,@EnableAutoConfiguration只适用于初学者,对控制力与把控力要求的架构师或高级用户显然是不合适的,所以有必要找到@SpringBootApplication的替代方案,自己控制Bean创建的过程与数量,替代方案如下:
//@SpringBootApplication@Configuration@ImportResource({"classpath:/META-INF/service-context.xml", "classpath:/META-INF/mvc-context.xml"})public class AppBooter extends SpringBootServletInitializer { /** * 所有配置信息的入口 * @param application * @return */ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { // 激活配置信息 return application.sources(AppBooter.class); } /** * 启动应用程序 * @param args */ public static void main(String[] args) { SpringApplication.run(AppBooter.class, args); } /** * 非常重要,激活内置的Servlet容器 */ @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); // 可以操控到更细力度 factory.setPort(9000); factory.setSessionTimeout(10, TimeUnit.MINUTES); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); return factory; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
结论
其实,在实际生产环境中,我们很少会直接发布基于Spring Boot的WEB应用程序,因为为此单独开发一套集群管理方案还是开销还是太大,所以建议将Spring Boot作为开发调试环境使用。