Spring Boot总结

(一) @SpringBootApplication的替代方案

对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;
    }

}

 

猜你喜欢

转载自blog.csdn.net/Asa_Prince/article/details/83090845