EmmmuaCode EmmmuaCode
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)

wufan

海内存知己,天涯若比邻。
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)
  • Spring5-基础

    • Spring5 入门案例
    • Spring核心之控制反转IOC
    • Spring核心之面向切面编程AOP
    • Jdbc Template
    • Spring5 事务
    • Spring5 新功能
    • Spring5 Webflux
  • SpringMVC-基础

    • SpringMVC 入门案例
    • RequestMapping注解
    • SpringMVC 获取请求参数
    • 域对象共享数据
    • SpringMVC的视图
    • RESTFul
    • RESTful案例
    • HttpMessageConverter
    • 文件上传和下载
    • 拦截器
    • 异常处理器
    • 注解配置SpringMVC
    • SpringMVC执行流程
  • SpringBoot2-基础

    • Spring与SpringBoot
    • SpringBoot2 快速入门
      • 系统要求
        • maven设置
      • HelloWorld
        • 创建maven工程
        • 引入依赖
        • 创建主程序
        • 编写业务
        • 测试
        • 一些思考
        • 为什么我们添加一个starter-web模块便可以了呢?
        • 我们如何更改更多Server的配置呢?比如Tomcat Server
        • 简化配置
        • 简化部署
        • SpringBoot还提供了哪些starter模块呢?
    • SpringBoot2 常用注解
    • 了解自动装配原理
  • SpringBoot2-核心功能

    • 配置文件
    • Web开发
    • 数据访问
    • 单元测试
  • SpringCloud-微服务架构

    • 微服务架构理论入门
    • SpringCloud 初步构建
    • Eureka 服务注册与发现
    • ZooKeeper 服务注册与发现
    • Consul 服务注册与发现
    • Ribbon 负载均衡服务调用
    • OpenFeign 简化服务调用
    • Hystrix 服务降级|熔断
    • GateWay 服务网关
    • Config 服务配置中心 与 BUS 消息总线
    • Stream 消息驱动
    • Sleuth 分布式请求链路追踪
  • SpringCloud Alibaba

    • SpringCloud Alibaba 简介
    • Nacos 服务发现、配置管理和服务管理平台
    • Sentinel 实现熔断与限流
    • Seata 分布式事务
  • Spring Security

    • SpringSecurity 概述
    • SpringSecurity 入门案例
    • SpringSecurity 登录认证详解
    • SpringSecurity 授权
    • SpringSecurity 跨域
    • SpringSecurity 遗留小问题
  • studynotes
  • Spring
  • SpringBoot2
wufan
2022-03-11
目录

SpringBoot2 快速入门

# SpringBoot2 快速入门

  • 官方文档 (opens new window)

# 系统要求

  • Java8&兼容Java14
  • Maven3.3及以上
  • idea 2019.1.2

# maven设置

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# HelloWorld

需求:浏览发送/hello请求,响应 Hello,Spring Boot 2

# 创建maven工程

# 引入依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>

    <dependencies>
        <!--web场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13

# 创建主程序

/**
 * @author frx
 * @version 1.0
 * @date 2022/3/14  18:14
 * 主程序类 这是一个SpringBoot应用
 */
//告诉SpringBoot,这是一个SpringBoot应用
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 编写业务

controller层

/**
 * @author frx
 * @version 1.0
 * @date 2022/3/14  18:57
 */
//@ResponseBody //返回一段话,而不是跳转到某个页面
//@Controller
@RestController  //@ResponseBody与@Controller的合体
public class HelloController {


    @RequestMapping("/hello") //映射请求
    public String handle01(){
        return "Hello,SpringBoot2"; //向浏览器返回
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 测试

运行main方法,结果如下:

  • 控制台

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)

2022-03-14 20:15:14.512  INFO 300 --- [           main] com.frx01.boot.MainApplication           : Starting MainApplication on FRXcomputer with PID 300 (F:\SpringBoot-project\SpringBoot-demo1\target\classes started by DELL in F:\SpringBoot-project\SpringBoot-demo1)
2022-03-14 20:15:14.520  INFO 300 --- [           main] com.frx01.boot.MainApplication           : No active profile set, falling back to default profiles: default
2022-03-14 20:15:15.312  INFO 300 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-03-14 20:15:15.318  INFO 300 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-03-14 20:15:15.318  INFO 300 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2022-03-14 20:15:15.368  INFO 300 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-03-14 20:15:15.368  INFO 300 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 772 ms
2022-03-14 20:15:15.481  INFO 300 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-03-14 20:15:15.713  INFO 300 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-14 20:15:15.779  INFO 300 --- [           main] com.frx01.boot.MainApplication           : Started MainApplication in 1.563 seconds (JVM running for 2.484)
2022-03-14 20:18:01.580  INFO 300 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-14 20:18:01.580  INFO 300 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-14 20:18:01.591  INFO 300 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 11 ms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  • 访问URL

01

非常非常的好用,非常非常的奈斯,非常非常的妙

# 一些思考

到此,你会发现一个简单的web程序居然完成了。这里你需要一些思考:

# 为什么我们添加一个starter-web模块便可以了呢?

我们安装Maven Helper的插件,用来查看spring-boot-starter-web模块的依赖

04

我们看下这个模块的依赖,你便能初步窥探出模块支撑

05

# 我们如何更改更多Server的配置呢?比如Tomcat Server

为什么Tomcat默认端口是8080? 如前文所述,SpringBoot最强大的地方在于约定大于配置,只要你引入某个模块的xx-start包,它将自动注入配置,提供了这个模块的功能;比如这里我们在POM中添加了如下的包

<dependencies>
        <!--web场景启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
1
2
3
4
5
6
7

它内嵌了Tomcat并且提供了默认的配置,比如默认端口是8080.

我们可以在application.properties或者application.yml中配置

# 简化配置

application.properties

server.port=8888
1

# 简化部署

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
1
2
3
4
5
6
7
8

把项目打成jar包,直接在目标服务器执行即可。

02

  • 访问URL

03

提示

取消掉cmd的快速编辑模式

# SpringBoot还提供了哪些starter模块呢?

Spring Boot 推荐的基础 POM 文件

名称 说明
spring-boot-starter 核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。
spring-boot-starter-amqp 通过 spring-rabbit 支持 AMQP。
spring-boot-starter-aop 包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。
spring-boot-starter-batch 支持 Spring Batch,包含 HSQLDB。
spring-boot-starter-data-jpa 包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。
spring-boot-starter-data-mongodb 包含 spring-data-mongodb 来支持 MongoDB。
spring-boot-starter-data-rest 通过 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 仓库。
spring-boot-starter-jdbc 支持使用 JDBC 访问数据库。
spring-boot-starter-security 包含 spring-security。
spring-boot-starter-test 包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。
spring-boot-starter-velocity 支持使用 Velocity 作为模板引擎。
spring-boot-starter-web 支持 Web 应用开发,包含 Tomcat 和 spring-mvc。
spring-boot-starter-websocket 支持使用 Tomcat 开发 WebSocket 应用。
spring-boot-starter-ws 支持 Spring Web Services。
spring-boot-starter-actuator 添加适用于生产环境的功能,如性能指标和监测等功能。
spring-boot-starter-remote-shell 添加远程 SSH 支持。
spring-boot-starter-jetty 使用 Jetty 而不是默认的 Tomcat 作为应用服务器。
spring-boot-starter-log4j 添加 Log4j 的支持。
spring-boot-starter-logging 使用 Spring Boot 默认的日志框架 Logback。
spring-boot-starter-tomcat 使用 Spring Boot 默认的 Tomcat 作为应用服务器。
#SpringBoot2
上次更新: 2024/04/21, 09:42:22
Spring与SpringBoot
SpringBoot2 常用注解

← Spring与SpringBoot SpringBoot2 常用注解→

最近更新
01
微信支付功能的实现与流程
11-21
02
购物车与结算区域的深入优化与功能完善
11-21
03
购物车与结算区域的功能实现与优化
11-21
更多文章>
Theme by Vdoing | Copyright © 2023-2024 EmmmuaCode | 黔ICP备2022009864号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式