美文网首页
springBoot 优化(上)

springBoot 优化(上)

作者: 穷技术 | 来源:发表于2017-09-11 21:02 被阅读0次

    Spring Boot可以轻松的创建独立“运行”的程序,基于Spring构建的应用程序。 我们对Spring平台和第三方库融合一体,所以你可以轻松快速的开发。 大多数Spring Boot应用程序只需要很少Spring的配置。

    spring boot 特征

    1、创建独立的Spring应用程序

    2、直接嵌入Tomcat,Jetty或Undertow(不需要部署WAR文件),一般小程序建议使用jetty即可,tomcat相对耗资源比较多。

    3、提供简单的“启动”POM来简化您的Maven配置。

    4、尽可能可伸缩的自动化配置。

    5、提供生产就绪功能,如指标,运行状况检查和外部化配置。

    6、基于代码配置或注解,也不需要XML配置。

    springboot简单案例

    1、执行maven命令 生成maven骨架

    mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    1、引入pom

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.6.RELEASE</version></parent><dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

               <exclusions>

                   <artifactId>spring-boot-starter-tomcat</artifactId>

                   <groupId>org.springframework.boot</groupId>

                </exclusions>

          </dependency>

          <dependency>

              <artifactId>spring-boot-starter-jetty</artifactId> 

              <groupId>org.springframework.boot</groupId>

            </dependency>

           </dependencies>

    1.1、这里去除了springboot自带的tomcat,自己添加jetty的依赖。

    2、引用启动类

     /**

     * Spring Boot应用启动类

     */

    @SpringBootApplication

    public class ApplicationRun {

        public static void main(String[] args) {

            SpringApplication.run(ApplicationRun .class,args);

        }

    }

     @SpringBootApplication:Spring Boot 应用的标识

    2. Application很简单,一个main函数作为主入口。SpringApplication引导应,并将Application本身作为参数传递给run方法。具体run方法会启动嵌入式的jetty并初始化Spring环境及其各Spring组件。

    3、控制类

     @RestController

    @RequestMapping("/hello")

    public class HelloWorldController {

        @RequestMapping("/sayHello")

        public String sayHello() {

            return "Hello,World!";

        }

     @RestController和@RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。

    1. @RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。

    2. @RequestMapping:提供路由信息,”hello/sayHello“路径的HTTP Request都会被映射到sayHello方法进行处理。

    4、以上案例及其简单,没有任何复杂的功能,但是该程序启动过程耗时比较长和消耗内存都是比较大的。启动耗时一般都要20秒左右,内存一般都是4~500M左右。

    一个这么简单的程序为什么会启动这么慢,耗内存这么大呢?

    因为我们大家都默认采用了@SpringBootApplication该注解,该注解包含了@Configuration,@EnableAutoConfiguration,@ComponentScan这仨,其目的也是为了简化运用。

    采用@SpringBootApplication 注解时是通过读取spring-boot-1.5.2.RELEASE.jar中的META-INF/spring.factories文件中定义好的基础类,我们可以看看里面定义了那些类,分别又有什么作用:

    # PropertySource Loaders 默认导入资源配置

    org.springframework.boot.env.PropertySourceLoader=\

    org.springframework.boot.env.PropertiesPropertySourceLoader,\

    org.springframework.boot.env.YamlPropertySourceLoader

    # Run Listeners默认导入启动监听

    org.springframework.boot.SpringApplicationRunListener=\

    org.springframework.boot.context.event.EventPublishingRunListener

    # Application Context Initializers 默认到程序启动容器上下文初始化

    org.springframework.context.ApplicationContextInitializer=\

    org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\

    org.springframework.boot.context.ContextIdApplicationContextInitializer,\

    org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\

    org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer

    # Application Listeners 导入监听程序包

    org.springframework.context.ApplicationListener=\

    org.springframework.boot.ClearCachesApplicationListener,\

    org.springframework.boot.builder.ParentContextCloserApplicationListener,\

    org.springframework.boot.context.FileEncodingApplicationListener,\

    org.springframework.boot.context.config.AnsiOutputApplicationListener,\

    org.springframework.boot.context.config.ConfigFileApplicationListener,\

    org.springframework.boot.context.config.DelegatingApplicationListener,\

    org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\

    org.springframework.boot.logging.ClasspathLoggingApplicationListener,\

    org.springframework.boot.logging.LoggingApplicationListener

    # Environment Post Processors 导入环节配置

    org.springframework.boot.env.EnvironmentPostProcessor=\

    org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\

    org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor

    # Failure Analyzers

    org.springframework.boot.diagnostics.FailureAnalyzer=\

    org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\

    org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\

    org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\

    org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,\

    org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\

    org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\

    org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer

    # FailureAnalysisReporters

    org.springframework.boot.diagnostics.FailureAnalysisReporter=\

    org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter

    以上这些都是springboot默认加载的,如果我们有自己第一的监听或者熟悉配置等等类都可以在这里添加,使其通过@SpringBootApplication默认加载到环境中。

    相关文章

      网友评论

          本文标题:springBoot 优化(上)

          本文链接:https://www.haomeiwen.com/subject/huzfsxtx.html