zoukankan      html  css  js  c++  java
  • Spring Boot -- Start Up

    做Java Web的同学,都知道项目启动需要放到servlet容器里面运行,无论是使用哪一款IDE,都是非常麻烦的一件事情。在很早之前,一个servlet容器下可以放下很多的项目,并一起运行,而到现在这个年代,很多服务一台机子都不够用了。所以很多时候,一个容器本来就只为一个项目服务,这样一来,容器式的服务器,还需要打包重启server这样的行为,看起来就非常繁琐,而且不利于持续集成。

    Spring Boot是一个可以让项目使用极少的配置,并且不需要额外配置到外部的容器中,就可以快速启动项目的微服务框架。看看它是怎么解放我们的生产力的。

    .   ____          _            __ _ _
    /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
    \/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot ::        (v1.3.1.RELEASE)
    

    添加配置到POM.XML

    目前 Spring Boot的最新版本是 1.3.1.RELEASE,这里我是使用maven作为项目的管理方案,当然你也可以使用gradle来作为项目管理方案添加Spring Boot到项目中。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    

    编写代码

    package hello;
    
    import org.springframework.boot.*;
    import org.springframework.boot.autoconfigure.*;
    import org.springframework.stereotype.*;
    import org.springframework.web.bind.annotation.*;
    
    @Controller
    @EnableAutoConfiguration
    public class SampleApplication {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(SampleApplication.class, args);
        }
    }
    

    然后直接执行这个类的main()方法,console里面就会打印出以下的信息。

    .   ____          _            __ _ _
    /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
    \/  ___)| |_)| | | | | || (_| |  ) ) ) )
    '  |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot ::        (v1.3.1.RELEASE)
    
    ......
    2016-01-03 20:40:15.732  INFO 12899 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f021e6c: startup date [Sun Jan 03 20:40:15 CST 2016]; root of context hierarchy
    2016-01-03 20:40:17.909  INFO 12899 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
    2016-01-03 20:40:19.232  INFO 12899 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2016-01-03 20:40:19.256  INFO 12899 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    ......
    

    这个时候,一个简单的web项目就启动完毕了。

    如果需要修改项目启动的配置『比如服务的端口,最大支持线程数量等等』,可以在resource目录下面新建一个application.properties,里面去填需要的配置。具体的参数可以参考官方的手册

    效果演示

    我们在命令访问这个127.0.0.1:8080这个地址。

    ➜  ~  http get 127.0.0.1:8080
    HTTP/1.1 200 OK
    Content-Length: 12
    Content-Type: text/plain;charset=UTF-8
    Date: Sun, 03 Jan 2016 12:52:03 GMT
    Server: Apache-Coyote/1.1
    
    Hello World!
    

    可以看到项目已经成功启动,并且可以访问了。

    其实是还是容器

    从console信息中不难发现,Spring Boot其实还是使用了tomcat,只不过tomcat变成了嵌入式的服务器了,当然我们也可以通过bean管理,把默认的tomcat方案换成其他的方式。

    写在后面

    过去我们写一些web小项目,是一个非常繁琐的过程,调试不方便,反复地启动tomcat,单元测试的内容和容器高度耦合,配置巨多,Spring Boot虽然不是什么银弹,但是很好地解决了一些问题,让开发者可以更加专注业务的开发,减少配置上时间的消耗。

  • 相关阅读:
    C# 大小写转换(非金额)
    DataService系列教程 (一)
    C# 大小写转换(金额)
    sql注入杂谈(一)union select
    sql注入杂谈(二)报错注入
    python正则提取txt文本
    sql注入杂谈(三)盲注
    对指定网站渗透的一些总结
    MSF的利用
    SQLMAP怎么拿shell
  • 原文地址:https://www.cnblogs.com/whthomas/p/start-web-project-with-spring-boot.html
Copyright © 2011-2022 走看看