• SpringBoot入门1—简介及helloworld


    Spring Boot简介

    Spring Boot让我们的Spring应用变的更轻量化。比如:你可以仅仅依靠一个Java类来运行一个Spring引用。你也可以打包你的应用为jar并通过使用java -jar来运行你的Spring Web应用。

    Spring Boot的主要优点:

    • 为所有Spring开发者更快的入门
    • 开箱即用,提供各种默认配置来简化项目配置
    • 内嵌式容器简化Web项目
    • 没有冗余代码生成和XML配置的要求


    用Spring Boot编写helloworld

    1.首先创建一个maven项目

    2.添加Spring Boot依赖

    通常,让你的Maven POM文件继承 spring-boot-starter-parent,并声明一个或多个 Starter POMs依赖即可。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      </dependencies>

    3.创建java类,编写helloworld代码

    package com.xzh.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @SpringBootApplication
    public class DemoApplication {
    
        @RequestMapping("/hello")
        public String index() {
            return "Hello World!!!";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(com.xzh.demo.DemoApplication.class, args);
        }
    }

    核心注解类说明

    @RestController

    就是@Controller+@ResponseBody组合,支持RESTful访问方式,返回结果都是json字符串

    @SpringBootApplication

    就是@SpringBootConfiguration+@EnableAutoConfiguration+ @ComponentScan等组合在一起,非常简单,使用也方便

    @EnableAutoConfiguration注解

    这个注解告诉Spring Boot“猜测”将如何配置Spring。@EnableAutoConfiguration是借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器。

    @ComponentScan

    用注解配置实现自动扫描,默认会扫描当前包和所有子包,并加载符合条件的组件(比如@Component和@Repository等),和xml配置自动扫描效果一样

    @SpringBootConfiguration

    说明这是一个配置文件类(SpringBoot社区推荐使用基于JavaConfig的配置形式)

    4.运行创建的java类

    2017-07-21 16:18:35.787  INFO 19984 --- [           main] com.xzh.demo.DemoApplication             : Starting DemoApplication on LAPTOP-T56QDVQ2 with PID 19984 (F:workspaceIDEAhelloworld	argetclasses started by lenovo in F:workspaceIDEAhelloworld)
    2017-07-21 16:18:35.789  INFO 19984 --- [           main] com.xzh.demo.DemoApplication             : No active profile set, falling back to default profiles: default
    2017-07-21 16:18:35.837  INFO 19984 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@8646db9: startup date [Fri Jul 21 16:18:35 CST 2017]; root of context hierarchy
    2017-07-21 16:18:37.293  INFO 19984 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2017-07-21 16:18:37.305  INFO 19984 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2017-07-21 16:18:37.306  INFO 19984 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
    2017-07-21 16:18:37.375  INFO 19984 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-07-21 16:18:37.375  INFO 19984 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1542 ms
    2017-07-21 16:18:37.477  INFO 19984 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2017-07-21 16:18:37.481  INFO 19984 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2017-07-21 16:18:37.481  INFO 19984 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2017-07-21 16:18:37.481  INFO 19984 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2017-07-21 16:18:37.481  INFO 19984 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2017-07-21 16:18:37.735  INFO 19984 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@8646db9: startup date [Fri Jul 21 16:18:35 CST 2017]; root of context hierarchy
    2017-07-21 16:18:37.769  INFO 19984 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.xzh.demo.DemoApplication.index()
    2017-07-21 16:18:37.772  INFO 19984 --- [           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-21 16:18:37.772  INFO 19984 --- [           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-21 16:18:37.789  INFO 19984 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-21 16:18:37.789  INFO 19984 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-21 16:18:37.810  INFO 19984 --- [           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-21 16:18:37.887  INFO 19984 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2017-07-21 16:18:37.916  INFO 19984 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2017-07-21 16:18:37.919  INFO 19984 --- [           main] com.xzh.demo.DemoApplication             : Started DemoApplication in 2.324 seconds (JVM running for 2.719)
    2017-07-21 16:18:41.874  INFO 19984 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
    2017-07-21 16:18:41.874  INFO 19984 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
    2017-07-21 16:18:41.883  INFO 19984 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 9 ms

    5.访问http://localhost:8080/hello

    参考:

    Spring Boot 官方文档学习(一)入门及使用

    Spring Boot学习

    Spring Boot应用程序开发入门

    Spring Boot快速入门

    Spring Boot参考指南

  • 相关阅读:
    JVM的生命周期、体系结构、内存管理和垃圾回收机制
    JVM的ClassLoader过程分析
    MySQL Cluster配置概述
    tomcat下bin文件夹下shell文件分析
    Eclipse环境下使用Maven注意事项
    mysql服务器的字符集
    判断文件中是否存在中文字符
    Tomcat/JSP中文编码配置
    Java内存泄露的原因
    Python 开发轻量级爬虫08
  • 原文地址:https://www.cnblogs.com/Jason-Xiang/p/7218601.html
Copyright © 2020-2023  润新知