SpringBoot系列:1、HelloWorld

  • 环境:
  • maven:3.6.2
  • jdk:1.8
  • SpringBoot:2.1.9.RELEASE
  • 开发工具:IDEA

1、SpringBoot简介

  • 简化Spring应用的初始化搭建以及开发过程
  • 整个Spring技术栈的一个大整合
  • J2EE开发的一站式解决方案
  • 约定大于配置

2、微服务

英文版:https://martinfowler.com/articles/microservices.html微服务:

  • 架构风格
  • 一个应用应该是一组小型服务,可以通过HTTP的方式进行互通
  • 每一个功能元素最终都是一个可独立替换和独立升级的软件单元

2、优点

  • 快速创建独立运行的Spring项目以及与主流框架集成
  • 使用嵌入式的Servlet容器,应用无需达成war包
  • starters自动依赖与版本控制
  • 大量的自动配置,简化开发,也可修改默认值
  • 无需配置XML,无代码生成,开箱即用
  • 准生产环境的运行时应用监控
  • 与云计算的天然集成

3、构建项目

3.1 新建项目

选择Spring Initializer,然后选择默认的url,点击【Next】file填写项目的基本信息,点击【Next】file勾选上web模块,点击【Next】file配置项目存储位置,点击【Next】完成项目创建。file

3.2 目录结构
  • /src/main/java:目录下放置所有的Java文件(源代码)
  • /src/main/resources:用于存放所有的资源文件,包括静态资源文件、配置文件、页面文件等
  • /src/main/resources/static:用于存放各类静态资源
  • /src/main/resources/application.yml:配置文件,支持俩种文件类型:.properties和.yml
  • /src/main/resources/templates:用于存放模版文件,如Thymeleaf
  • /src/test/java:放置单元测试类Java代码
  • /target:放置编译后的.class文件、配置文件等
3.3 新建HelloController类
package com.xiaoh.springboot.helloworld.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiaoH
 * @date 2019/10/15
 * @description
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        
        return "Hello, Spring Boot!";
    }
    
}
  • @RestController:相当于@Controller @ResponseBody两个注解的结合
3.4 运行SpringbootHelloworldApplication

创建项目时,已自动生成。

package com.xiaoh.springboot.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootHelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootHelloworldApplication.class, args);
    }

}
  • @SpringBootApplication:是一个组合注解,包含@EnableAutoConfiguration、@ComponmentScan和@SpringBootConfiguration三个注解,是项目启动注解
  • 将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器种

如下图所示,运行成功,默认端口为8080。file

3.4 浏览器访问

file

4、pom文件

4.1 父项目
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.9.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
    它的父项目是
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.9.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
    这个是真正管理SpringBoot应用里面所有依赖的版本。
4.2 导入的依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

spring-boot-starter-web:spring-boot-starter:springboot场景启动器,帮我们导入了web模块正常运行所依赖的组件SpringBoot将所有功能场景都抽取出来,做成一个个启动器(starters),只需要在项目中引入这些starter相关场景的所有依赖都会导入进去。

5 源码

GitHub:https://github.com/chenjiecg/SpringBoot.git

本文由博客一文多发平台 OpenWrite 发布!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值