SpringBoot(一):初章

 

什么是spring boot?(废话一堆的blablabla)

    Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.

    Most Spring Boot applications need very little Spring configuration.

    Spring Boot是由Pivotal团队提供的"全新框架",其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

  • 个人理解:

    Spring Boot 并不是一个新的框架,而是它默认集成了很多框架的使用方式,可以近似的比喻为Maven在Java编程中的作用。Spring Boot整合了现在Java生态中大部分的框架,以“约定优于配置(convention over configuration)”软件设计范式,使得搭建开发环境变得简单、快速、便捷。

 

 

使用spring boot的好处

  • SpringBoot官方文档,是这么介绍SpringBoot的好处的(PS:我实在想不到比 Spring爸爸 的官方特性描述 更好的词汇了=_=):
    • Create stand-alone Spring applications
    • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
    • Provide opinionated 'starter' POMs to simplify your Maven configuration
    • Automatically configure Spring whenever possible
    • Provide production-ready features such as metrics, health checks and externalized configuration
    • Absolutely no code generation and no requirement for XML configuration

  其实总结起来就是:Spring Boot会让你的环境搭建更加简单、快速、便捷!那么如果我们平时搭建一个Spring Web的项目时,需要怎么做呢?

  1. 配置web.xml(加载Spring容器、SpringMVC容器)

  2. 配置DB, 配置Spring事务

  3. 配置日志文件

  ......

  x. Servlet容器部署运行(Tomcat、JBOSS等)

  

  烦躁不烦躁?

  大家都知道, 现在坊间流行微服务。如果我们的项目, 就算只是一个临时项目, 都需要这么折腾一遍, 是不是要炸锅了?

  但是如果使用Spring Boot呢

  1. 修改application.yml(application.properties)

  2. run

 

  此时的Spring Boot  此时的我  

  

  

快速入门

  废话一堆, 我们上手搞一搞

  所有DEMO, 均基于IDEA和MAVEN

  使用Eclipse的同学, 可以打开 http://start.spring.io/, 根据官方指引, 选择依赖后下载压缩包, 使用Eclipse打开即可

新建项目

  1. IDEA创建工程
  2. 填写Project Metadata基本信息
  3. 选择依赖, 本例中仅demo演示HelloWorld使用, 仅选择了Spring Boot 2.0版本的web基础依赖
  4. 选择项目保存地址

  到此, 我们的项目基础结构就搭建好了......

 

项目结构介绍

  如上图所示, Spring Boot的基础结构共三个文件夹

    • src/main/java(主程序入口以及程序开发)
    • src/main/resource(配置文件, 页面等)
    • src/test/java(测试用例)

  另外,建议的Spring Boot目录如下:

com
  +- wangyanrui
    +- demo
      +- XxxApplication.java
      |
      +- domain
      |  +- Entity.java
      |  +- EntityRepository.java
      |
      +- service
      |  +- XxxService.java
      |
      +- controller
      |  +- XxxController.java
      |
  1. XxxApplication(本例中为DemoApplication)建议放置在项目根目录下, 主要用于做一些框架配置, component扫描(Application默认扫描本包及本包的递归子包), 程序启动等
  2. controller层负责页面访问控制
  3. service层负责业务类代码
  4. domain目录用于实体(Entity)与数据访问层(Repository)

  最后, 启动DemoApplication的main方法, 至此, 一个基础的JAVA WEB项目脉络搭建好了(仅限于可运行, 尚无法调用, 无Controller)

  

 

 

项目细述

  • POM文件分析

  由此看出, 我们在新建项目第二步选择的MVC, 其实就是这里的spring-boot-start-web的依赖.

  POM.xml文件包含了两个默认的模块

    spring-boot-starter:核心模块, 包括自动配置支持、日志、YAML等

    spring-boot-starter-test:测试模块, 包括JUnit、Hamcrest、Mockito等

      PS:(由于本例中导入了MVC模块, 其包含了spring-boot-starter 模块, 故没有在POM文件中体现出来)

  • 编写Controller
@RestController
public class DemoController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello World";
    }
}

  @RestController和@Controller类似, 我们观察一下其源码, 不难发现其默认返回JSON格式的数据

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     * @since 4.0.1
     */
    @AliasFor(annotation = Controller.class)
    String value() default "";

}

 

启动主程序, 打开浏览器访问 http://localhost:8080/hello, 就可以看到浏览器的返回数据, Spring Boot搭建WEB项目, 就是如此简单 !!!

  •  编写单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class DemoControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void hello() throws Exception {
        System.out.println(
                mvc.perform(MockMvcRequestBuilders.get("/hello"))
                        .andExpect(MockMvcResultMatchers.status().isOk())
                        .andReturn()
                        .getResponse()
                        .getContentAsString()
        );
    }
}

 

 总结

  Spring Boot可以简单、搞笑的快速搭建项目, 使得不太需要关心框架之间的兼容性, 版本冲突等问题, 任何东西, 仅仅需要添加一个配置、一个注解就可以. 

  相信在微服务架构大行其道的今天, 加持着Spring生态圈的Spring Boot、Spring Cloud等, 一定会成为之后的业内主流. 

 

  

 

posted on 2018-03-20 13:04  隔壁公司的程序员  阅读(251)  评论(0)    收藏  举报

导航