一、SpringBoot入门

目录

1 SpringBoot HelloWorld

1.1 创建一个maven工程

1.2 下载官方参考文档

1.3 使用官方参考文档

1.3.1 Creating the POM (创建POM)

1.3.2 Adding Classpath Dependencies(添加依赖)

1.3.3 Writing the Code(编写代码)

1.3.4 Running the Example(运行例子)

1.3.5 Creating an Executable Jar (创建一个可执行的Jar)

2 HelloWorld探究

2.1 使用IntelliJ IDEA快速创建SpringBoot项目

2.2 SpringBoot项目结构分析

2.2.1 pom.xml文件

2.2.2 resources文件目录结构


1 SpringBoot HelloWorld

功能:浏览器发送sayHello请求,服务器接受请求并处理,响应Hello

1.1 创建一个maven工程

<groupId>com.seagetech</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>1.0.0</version>

1.2 下载官方参考文档

在官网找到相应的下载地址(官网地址:https://spring.io/projects)或者直接使用如下地址下载:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/pdf/spring-boot-reference.pdf。文档名称《spring-boot-reference.pdf》,版本2.10.RELEASE

1.3 使用官方参考文档

Part II. Getting Started(第二部分)--->11. Developing Your First Spring Boot Application(开发你的第一个SpringBoot应用)

1.3.1 Creating the POM (创建POM)

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

 

1.3.2 Adding Classpath Dependencies(添加依赖)

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>  

 

1.3.3 Writing the Code(编写代码)

创建一个主程序类,类名随便取,因为我们是做一个HelloWorld的项目,所有我们创建一个HelloWorldApplication类:

/**
 * @auther wangzb
 * @date 2018/11/9 21:32
 */
@Controller
@EnableAutoConfiguration
public class HelloWorldApplication {


    @RequestMapping(value = "/sayHello")
    @ResponseBody
    public String sayHello(String name){
        return "Hello," + name;
    }


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

 

1.3.4 Running the Example(运行例子)

启动main方法后,查看SpringBoot启动日志

可以看到Tomcat的启动日志:

Tomcat started on port(s): 8080 (http) with context path ''

即,Tomcat默认启动端口为8080,项目根路径为"",所以在浏览器或者Postman中输入地址:http://localhost:8080/sayHello?name=wangzb,浏览器或Postman响应:

Hello,wangzb

 

1.3.5 Creating an Executable Jar (创建一个可执行的Jar)

要创建一个可执行jar,我们需要将spring-boot-maven-plugin添加到我们的pom.xml中。来

这样做,在dependencies部分下面插入以下几行:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

 接下来就可以打包,在idea中点击package,执行打包命令,之后我们在项目的target包下就可以看到刚打包好的springboot-helloworld-1.0.0.jar包。在包的路径下,打开CMD命令行,执行如下命令:

 

2 HelloWorld探究

 

2.1 使用IntelliJ IDEA快速创建SpringBoot项目

(1) New Project(新建项目)

 

 

(2) 左边菜单选择Spring Initializr,右边菜单选择jdk版本,然后点击Next

 

 

(3) 填写Maven坐标GroupId/ArtifactId、打包形式(本例使用Jar的方式,war包形式请看后续文章)、项目名称/版本等基本信息,然后点击Next。

 

 

(4) 选择SpringBoot的版本,以及需要依赖的包,这里我们选择最新版2.1.0,导入Web模块依赖包,点击Next。

 

 

(5) 修改项目名称以及项目存放路径,默认即可,点击Finish。

 

 

 

2.2 SpringBoot项目结构分析

通过2.1的讲解,我们熟悉了如果快速创建SpringBoot项目,接下来就是分析下SpringBoot的项目结构,上节中创建好的SpringBoot项目结构如下:

 

 

 

2.2.1 pom.xml文件

2.2.1.1 父项目

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.0.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>
按住Ctrl,点击spring-boot-starter-parent进入spring-boot-starter-parent项目,他的父项目是:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

 如上,看到spring-boot-dependencies,它是真正管理SpringBoot项目里的所有依赖版本,是Spring Boot的版本仲裁中心,以后我们导入依赖默认是不需要写版本(没有在dependencies里面管理的依赖自然需要声明版本号)。

2.2.1.2 启动器(Starter)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

(1) 以下列举SpringBoot官方提供的starter pom。更多请参考《spring-boot-reference.pdf》2.1.0.RELEASE版,第13.5节Starters

(2)除了官方的starter pom外,还有第三方为SpringBoot所写的starter pom,如下表所示。

 

2.2.2 resources文件目录结构

(1)static:保存所有的静态资源,如js、css、images等。

(2)templates:保存所有的模板文件,SpringBoot默认jar使用嵌入式的Tomcat,默认不支持JSP页面,可以使用模板引擎(thymeleaf、freemarker )。

(3)application.properties:SpringBoot应用配置文件,可以修改一些默认的配置,如修改Tomcat的端口,项目根路径等。

2.2.3 主程序类

打开2.1节创建的springboot-demo项目,找到SpringBootDemoApplication类:

package com.seagetech.springbootdemo;

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

@SpringBootApplication
public class SpringbootDemoApplication {

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

       基本和1.3.3节创建的主程序类一样,main方法加注解的形式,不同的是,Idea创建的主程序类使用的是@SpringBootApplication注解,它是SpringBoot的核心注解,也是一个组合注解,打开这个注解,源码如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

      @SpringBootApplication注解的核心功能其实是:@SpringBootConfiguration、@EnableAutoConfigration、@ComponentScan三个注解提供。有关这几个注解的详解,请阅读《二、SpringBoot自动配置原理》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值