• spring-boo hello world程序


    作为一个程序猿,使用了spring好多年,现在有了spring-boot,也想尝尝鲜。

    初听spring-boot,觉得很神秘,实际上就是集合了很多组件,再加上一些boot开发的启动和粘合程序。 个人见解,不一定对。

    构建过程

    使用 Spring Initializr ,我使用idea构建的,在新项目里面有一个Spring Initializr 选项,如图:

    设置项目属性,如图:

    选择spring-boot组件: 

    设置项目名: 

    完成就可以了

    第一个程序

    新建Java程序

    package com.sun.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @EnableAutoConfiguration
    public class Application {
    
        @RequestMapping("/helloworld")
        public String home() {
            return "Hello World!";
        }
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    查看build.gradle文件

    buildscript {
        ext {
            springBootVersion = '1.5.4.RELEASE'
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'org.springframework.boot'
    
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web')
        runtime('mysql:mysql-connector-java')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    

    程序结构如下: 

    运行

    运行Application,输出如下:

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.4.RELEASE)
    
    2017-07-19 08:22:36.552  INFO 8372 --- [           main] com.sun.boot.Application                 : Starting Application on sunzuoquan-Inspiron-7466 with PID 8372 (/home/sunzuoquan/IdeaProjects/demo/build/classes/java/main started by sunzuoquan in /home/sunzuoquan/IdeaProjects/demo)
    2017-07-19 08:22:36.583  INFO 8372 --- [           main] com.sun.boot.Application                 : No active profile set, falling back to default profiles: default
    2017-07-19 08:22:37.041  INFO 8372 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43bd930a: startup date [Wed Jul 19 08:22:36 CST 2017]; root of context hierarchy
    2017-07-19 08:22:42.185  INFO 8372 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2017-07-19 08:22:42.210  INFO 8372 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2017-07-19 08:22:42.211  INFO 8372 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
    2017-07-19 08:22:42.747  INFO 8372 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-07-19 08:22:42.748  INFO 8372 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 5760 ms
    2017-07-19 08:22:42.877  INFO 8372 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2017-07-19 08:22:42.880  INFO 8372 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2017-07-19 08:22:42.880  INFO 8372 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2017-07-19 08:22:42.880  INFO 8372 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2017-07-19 08:22:42.881  INFO 8372 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2017-07-19 08:22:43.338  INFO 8372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43bd930a: startup date [Wed Jul 19 08:22:36 CST 2017]; root of context hierarchy
    2017-07-19 08:22:43.401  INFO 8372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/helloworld]}" onto public java.lang.String com.sun.boot.Application.home()
    2017-07-19 08:22:43.405  INFO 8372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    2017-07-19 08:22:43.405  INFO 8372 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2017-07-19 08:22:43.421  INFO 8372 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-19 08:22:43.421  INFO 8372 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-19 08:22:43.440  INFO 8372 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-19 08:22:43.634  INFO 8372 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2017-07-19 08:22:44.043  INFO 8372 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2017-07-19 08:22:44.065  INFO 8372 --- [           main] com.sun.boot.Application                 : Started Application in 9.217 seconds (JVM running for 15.872)
    

    从日志可以看出,Tomcat started on port(s): 8080,spring-boot内部启动了tomcat,监听8080端口。
    打开浏览器,访问:http://localhost:8080/helloworld
    浏览器输出如下:

  • 相关阅读:
    mode
    文件操作
    深浅拷贝
    基础数据类型补充
    再谈编码 decode和encode
    Python练习题 015:一颗自由落地的球
    Python练习题 014:完数
    Python练习题 013:求解a+aa+aaa……
    Python练习题 012:字符统计
    Python练习题 011:成绩打分
  • 原文地址:https://www.cnblogs.com/panda2/p/7204450.html
Copyright © 2020-2023  润新知