Spring Boot快速入门 -> 构建一个微服务(一)

前言:不忘初心,寻找最初的编程快感!

1.SpringBoot简介

在这里插入图片描述
SpringBoot是一个便捷搭建基于spring工程的脚手架;作用是帮助开发人员快速的搭建大型的spring项目,简化工程的配置,依赖管理;实现开发人员把时间都集中在业务开发上。

通过Spring Boot,可以轻松地创建独立的,基于生产级别的基于Spring的应用程序,您可以“运行”它们。

我们对Spring平台和第三方库持固执己见的观点,因此您可以以最小的麻烦开始使用。大多数Spring Boot应用程序需要最少的Spring配置。

特征

  • 创建独立的Spring应用程序
  • 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
  • 提供自以为是的“入门”依赖项,以简化构建配置
  • 尽可能自动配置Spring和3rd Party库
  • 提供可用于生产的功能,例如指标,运行状况检查和外部化配置
  • 完全没有代码生成,也不需要XML配置

2.SpringBoot快速入门

实现步骤如下:

  1. 创建工程
  2. 添加依赖(spring-boot-starter-web)
  3. 创建启动类Application
  4. 创建处理器Controller
  5. 测试

快速搭建

集成环境:IDEA丶MAVEN丶JDK1.8

  1. 创建myspringboot项目
    在这里插入图片描述
  2. 在pom.xml中添加相关依赖
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <!-- springboot 父级依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <!-- springboot web启动器 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 创建Application启动类
@SpringBootApplication
public class MySpringBootApplication {
    public static void main(String[] args) {

        SpringApplication application = new SpringApplication(MySpringBootApplication.class);

        application.run();
    }
}
  1. 创建测试Controller
@RestController
public class TestController {

    @RequestMapping("hello")
    public String sayHello(){
        return "welcome, this is springboot !";
    }
}

  1. 测试
  • 启动项目:MySpringBootApplication启动入口
    在这里插入图片描述
  • 打开浏览器,输入http://localhost:8080/hello
    在这里插入图片描述

总结:

  • @SpringBootApplication注解中包含@SpringBootConfiguration、@ EnableAutoConfiguration、@ ComponentScan,后续文章中会更新相关源码解析。。。
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication{}
  1. @Configuration 和 @bean 注入定义了一个实体类;
  2. @EnableAutoConfiguration 启用了上下文自动配置;
  3. @ComponentScan 扫描指定的包,若未指定值,默认扫包范围:被注释的 class所在的包;
  • @RestController注解中包含@Controller和@ResponseBody注解,与springmvc中的注解对应,表明该类可以处理HTTP请求并返回Json类型的响应;
@Controller
@ResponseBody
public @interface RestController{}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值