• SpringBoot学习(四)-->SpringBoot快速入门,开山篇


    Spring Boot简介

    Spring Boot的目的在于创建和启动新的基于Spring框架的项目。Spring Boot会选择最适合的Spring子项目和第三方开源库进行整合。大部分Spring Boot应用只需要非常少的配置就可以快速运行起来

    Spring Boot是伴随着Spring4.0诞生的,旨在简化开发。
    Spring Boot提供了一种快速使用Spring的方式

    SpringBoot官方文档:http://spring.io/projects/spring-boot

    >>Spring Boot特点

    1:为基于Spring的开发提供更快的入门体验
    2:创建可以独立运行的Spring应用
    3:直接嵌入Tomcat或Jetty服务器,不需要打包成WAR文件
    4:提供推荐的基础POM文件(starter)来简化Apache Maven配置
    5:尽可能的根据项目依赖来自动配置Spring框架
    6:Spring Boot使编码、配置、部署、监控变简单
    7:提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查 8:开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求 9:其他大量的项目都是基于Spring Boot之上的,如Spring Cloud

    >>Spring Boot缺点

    1:依赖太多,随便的一个Spring Boot应用都有好几十M
    2:缺少服务的注册和发现等解决方案
    3:缺少监控集成方案、安全管理方案
    4:中文的文档和资料太少且不够深入

    >>Spring Boot应用场景

    1:Spring能够应用的场景
    2:java web应用
    3:微服务

    写个示例:Hello SpringBoot

    1、创建Maven工程

    工程结构如下:

    2、配置pom.xml文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mmzs</groupId>
        <artifactId>springBoot00</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>springBoot00 Maven Webapp</name>
        <url>http://maven.apache.org</url>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <!-- 一定要有spring-boot-starter-parent,其中包含了spring的各种插件版本号 -->
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath /><!-- lookup parent from repository -->
        </parent>
    
        <!-- 父类统一管理版本信息 -->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <!-- springboot 默认java版本是1.6,这里显示给它指定为1.7 -->
            <java.version>1.7</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <!-- 导入spring boot的web支持,可以不写版本号,在spring-boot-starter-parent已经包含 -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
    
        <build>
            <finalName>spring_boot</finalName>
            <!-- 添加Spring boot的maven插件,可以不写版本号,在spring-boot-starter-parent已经包含  -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
    
        </build>
    </project>
     

    3、编写代码

     1 package com.mmzs.springboot;
     2  
     3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestParam;
     6 import org.springframework.web.bind.annotation.ResponseBody;
     7 import org.springframework.web.bind.annotation.RestController;
     8 import java.util.ArrayList;
     9 import java.util.HashMap;
    10 import java.util.List;
    11 import java.util.Map;
    12  
    13 /**
    14  * Created by mmzs 2018年4月2日 11:50:57
    15  * springboot注解详解:http://www.cnblogs.com/mmzs/p/8874349.html
    16  */
    17 //用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集,
    18 //这样子获取的数据返回前台时也会自动转发为json格式。 
    19 @RestController
    20 //Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。
    21 @EnableAutoConfiguration
    22 public class HelloController {
    23  
    24     @RequestMapping("/hello")
    25     @ResponseBody //会使用详细转换器输出结果
    26     public String hello() {
    27         return "Hello Spring-Boot";
    28     }
    29  
    30     @RequestMapping("/info")
    31     public Map<String, String> getInfo(@RequestParam String name) {
    32         Map<String, String> map = new HashMap<>();
    33         map.put("name", name);
    34         return map;
    35     }
    36  
    37     @RequestMapping("/list")
    38     public List<Map<String, String>> getList() {
    39         List<Map<String, String>> list = new ArrayList<>();
    40         Map<String, String> map = null;
    41         for (int i = 1; i <= 5; i++) {
    42             map = new HashMap<>();
    43             map.put("name", "mmzs+" + i);
    44             list.add(map);
    45         }
    46         return list;
    47     }
    48 }
    49  
    HelloController
     1 package com.mmzs.springboot;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 
     6 /**
     7  * Created by mmzs 2018年4月2日 11:48:38
     8  * springboot注解详解:http://www.cnblogs.com/mmzs/p/8874349.html
     9  */
    10 //Spring Boot项目的核心注解,主要目的是开启自动配置。
    11 //包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。
    12 //其中@ComponentScan默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。
    13 @SpringBootApplication
    14 public class Application {
    15     public static void main(String[] args) {
    16         SpringApplication.run(Application.class, args);
    17     }
    18 }
    19  
    Application启动类

    4、测试效果

    运行Application类,结果如下:

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.2.RELEASE)
    
    2018-07-02 12:17:03.434  INFO 5988 --- [           main] com.mmzs.springboot.Application          : Starting Application on CVHOPASEHFXPHFV with PID 5988 (D:ProjectspringBoot00	argetclasses started by Administrator in D:ProjectspringBoot00)
    2018-07-02 12:17:03.442  INFO 5988 --- [           main] com.mmzs.springboot.Application          : No active profile set, falling back to default profiles: default
    2018-07-02 12:17:03.562  INFO 5988 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27fe3806: startup date [Mon Jul 02 12:17:03 CST 2018]; root of context hierarchy
    2018-07-02 12:17:06.724  INFO 5988 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2018-07-02 12:17:06.751  INFO 5988 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2018-07-02 12:17:06.753  INFO 5988 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
    2018-07-02 12:17:06.971  INFO 5988 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2018-07-02 12:17:06.972  INFO 5988 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3415 ms
    2018-07-02 12:17:07.260  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2018-07-02 12:17:07.266  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2018-07-02 12:17:07.267  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2018-07-02 12:17:07.267  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2018-07-02 12:17:07.267  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2018-07-02 12:17:07.788  INFO 5988 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27fe3806: startup date [Mon Jul 02 12:17:03 CST 2018]; root of context hierarchy
    2018-07-02 12:17:07.928  INFO 5988 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/list]}" onto public java.util.List<java.util.Map<java.lang.String, java.lang.String>> com.mmzs.springboot.HelloController.getList()
    2018-07-02 12:17:07.929  INFO 5988 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.mmzs.springboot.HelloController.hello()
    2018-07-02 12:17:07.931  INFO 5988 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/info]}" onto public java.util.Map<java.lang.String, java.lang.String> com.mmzs.springboot.HelloController.getInfo(java.lang.String)
    2018-07-02 12:17:07.936  INFO 5988 --- [           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)
    2018-07-02 12:17:07.937  INFO 5988 --- [           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)
    2018-07-02 12:17:08.012  INFO 5988 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-07-02 12:17:08.013  INFO 5988 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-07-02 12:17:08.103  INFO 5988 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-07-02 12:17:08.403  INFO 5988 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2018-07-02 12:17:08.531  INFO 5988 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2018-07-02 12:17:08.538  INFO 5988 --- [           main] com.mmzs.springboot.Application          : Started Application in 5.736 seconds (JVM running for 6.592)
    启动成功界面

    访问界面:

  • 相关阅读:
    Linux-解决putty无法直接使用root用户远程登录linux主机的问题
    Linux-安装jdk
    Linux-安装谷歌浏览器
    Linux-安装ssh服务
    java正则表达式匹配文本中想要的字符串
    日常技术资源收集
    《java编程思想》:散列的原理
    《java编程思想》:字符串
    win10完美去除快捷方式小箭头
    WS_2012
  • 原文地址:https://www.cnblogs.com/mmzs/p/9253276.html
Copyright © 2020-2023  润新知