A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 梦想飞翔的猪 于 2018-3-30 14:06 编辑

【石家庄校区】Spring Boot 系列(一)快速入门

简介
  • Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者的。
  • 在以前的spring项目中,都会面对大量繁琐的配置,使用的时候基本上都是大量的复制黏贴。而Spring Boot 则能让我们在不需要过多的配置下,轻松快速地搭建Spring Web应用,开箱即用,没有代码生成,也无需XML配置,从而快速使用spring框架。一、构建简单spring boot 项目这里官网提供的生成器SPRING INITIALIZR 来创建简单的spring boot 项目。1. 访问 http://start.spring.io

    选项: 工程(maven) 语言(java) SpringBoot版本(1.5.4)Group填组名,Artifact填模块名,右侧Dependencies 可以选择相应的依赖,因为我们要构建web项目,所以可以添加web的依赖。点击 Generate Project 生成下载项目。2. 把下载的maven项目导入IDE并运行把下载的项目解压并导入到IDE中(这里使用IntelliJ IDEA)如下:

    直接运行 DemoApplication.java 的main方法。运行成功的截图:

    其中可以看到,项目的进程ID为:25642,可以通过java的jconsole工具查看详细信息。其中可以看到项目的启动端口为8080 (spring boot 默认端口,可以在application.properties中修改)

    3. 编写controller服务新建controller包,包下新建IndexController
  • [Java] 纯文本查看 复制代码
    package com.sam.demo.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class IndexController {
    
        @RequestMapping("/index")
        public String index() {
            return "index";
        }
    
    }
  • 注:@RestController 这个注解相当于同时添加@Controller和@ResponseBody注解。运行DemoApplication.java 启动项目,启动日志可以看到端口为8080打开浏览器访问:http://localhost:8080/index 可得结果。
  • 二、pom.xml 讲解打开pom.xml文件,查看配置信息
  • 继承 parent 父模块,spring-boot-starter-parent 模块中包含了自动配置、日志和YAML(参考:https://my.oschina.net/u/3235888/blog/887854) ,使构建spring项目变得简单。
  • [XML] 纯文本查看 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

  • pom.xml中依赖 spring-boot-starter-web 模块,包括了Tomcat和spring-webmvc(参考:https://my.oschina.net/u/3235888/blog/887854) ,不需要指定version 版本,因为父模块中已经有默认配置,如果需要指定版本可添加。

  • [XML] 纯文本查看 复制代码
    <!-- 构建web项目模块 包括了Tomcat和spring-webmvc -->
    <!-- spring-boot-starter-web 默认依赖了tomcat的starter 所以使得项目可以直接运行而不需要部署到tomcat中-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

  • pom.xml中依赖 spring-boot-starter-test 测试模块,包括JUnit、Hamcrest、Mockito

  • [XML] 纯文本查看 复制代码
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

  • 在spring-boot中,模块的依赖都是以starter的方式进行,以 spring-boot-starter-方式命名,指明了具体的模块。spring-boot生态中提供了丰富的starter供开发者使用(参考:https://my.oschina.net/u/3235888/blog/887854)这种模块化的依赖,简化了大量的依赖配置。使用starter的依赖方式,可以把相应需要依赖的包一起依赖到项目中,而不像传统的Spring 需要各自依赖包。
    如在spring-boot中web模块,在传统的Spring配置中需要依赖 org.springframework spring-web 等web服务的包,在spring-boot中,只需要依赖spring-boot-starter-web 就能把相应的包一并依赖进来,极大的简化了配置。
  • 参考地址:https://blog.csdn.net/sinat_14982831/article/details/75126936



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马