01. SpringBoot入门

01. SpringBoot入门

学习目标

  • Spring Boot 介绍
  • 构建 Spring Boot 项目
  • Spring Boot 入门 HelloWorld
  • Spring Boot的全局配置文件
  • SpringBoot项目的两种发布方式

1.SpringBoot介绍

1.1.什么是SpringBoot

Spring Boot是由Pivotal团队提供的全新框架,其中“Boot”的意思就是“引导”,意在简化开发模式,使开发者能够快速的开发出基于Spring 的应用。

Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。

1.2.Spring Boot 特点

• 嵌入的 Tomcat,无需部署 WAR 文件

• 简化Maven配置

• 自动配置Spring

2.构建 Spring Boot 项目

2.1.下载SpringBoot项目

1、访问http://start.spring.io/

2、选择构建工具Maven Project、Spring Boot版本以及一些工程基本信息,点击“GENERETE”下载项目压缩包
下载SpringBoot项目

2.2.maven构建SpringBoot项目

1、创建maven工程,不要使用骨架创建maven工程

2、填写项目坐标信息

填写项目坐标信息

3、设置项目保存位置

设置项目保存位置

2.3.pom.xml

参考下载的springboot项目修改pom.xml

2.3.1.继承springboot父工程

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

2.3.2.设置jdk版本

    <properties>
        <java.version>1.8</java.version>
    </properties>

2.3.3.添加启动器

    <dependencies>
        <!--  springboot的web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.3.4.创建启动类

package com.usian;

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

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

2.4.SpringBoot 启动器

所谓的springBoot 启动器其实就是一些 jar包的集合,SprigBoot一共提供 44 个启动器。例如:

​ Ø spring-boot-starter-web:支持全栈式的 web 开发,包括了 tomcat 和 springMVC 等 jar

​ Ø spring-boot-starter-jdbc:支持 spring 以 jdbc 方式操作数据库的 jar 包的集合

​ Ø spring-boot-starter-redis:支持 redis 键值存储的数据库操作

3.Spring Boot入门HelloWorld

3.1.controller

package com.usian.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloWorld {
	@RequestMapping("/hello")
	@ResponseBody
	public Map<String, Object> showHelloWorld() {
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "HelloWorld");
		return map;
	}
}

3.2.启动类存放位置

启动类存放的位置:

  • controller 同一个包下:ok

  • controller 的上一级包中:ok

3.3.自定义banner

1、banner生成网站:http://www.bootschool.net/ascii

2、将生成的banner.txt复制到resources目录中

3、测试

在这里插入图片描述

4.Spring Boot的全局配置文件

SpringBoot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下

4.1.properties配置文件

4.1.1.修改tomcat的端口为8088

server.port=8088

4.1.2.修改访问项目时的名字

server.servlet.context-path=/springboot

4.2.yml配置文件

yml是 Spring Boot 中新增的一种配置文件格式。特点:具备天然的树状结构

1.2.1.yml与 properties的区别

  • 配置文件的扩展名有变化

  • 配置文件中的语法有变化:

​ 1、在 yml 中使用“ : ”进行分割

​ 3、在 yml中缩进时不允许使用tab键,缩进的空格数不重要,只要是左对齐的一列数据,都是同一个层级

​ 3、每个K的冒号后面一定要加一个空格

1.2.2.yml方式配置springboot

server:
  port: 8090
  servlet:
    context-path: /springboot

5.Spring Boot项目的两种发布方式

5.1.方式1:通过jar包发布

步骤1:在pom中添加一个SpringBoot的构建的插件

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

步骤2:在maven视图中,选择“package”,在target中会产生xxx.jar包

在maven视图中,选择“package”,在target中会产生xxx.jar包
在这里插入图片描述

步骤3:然后在cmd终端发布项目

java -jar xxx.jar

在cmd终端发布项目在这里插入图片描述

在这里插入图片描述

5.2.方式2:通过war包发布

步骤1:在pom.xml文件中将jar修改为war

在这里插入图片描述

步骤2:设置tomcat启动器依赖范围

maven依赖范围参考:资料/maven依赖作用范围.png

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--tomcat启动器依赖范围-->
            <scope>provided</scope>
        </dependency>

步骤3:设置war包的名字

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>hello</warName>
                </configuration>
            </plugin>

步骤4:修改启动类

package com.usian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

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

6.小结

-----------------------01、springboot入门–小结------------------

一、springboot介绍

Spring Boot(spring+springmvc):不是对spring功能的增强,而是提供了一种快速开发spring应用的方式	
特点:
	简化xml
	简化maven
	内嵌tomcat

二、springboot项目构建

1、创建maven工程,继承springboot父工程(管理依赖版本号)
2、设置jdk的版本
3、添加springboot启动器
4、创建启动类,并添加 @SpringBootApplication(注意:启动类必须放到controller、service和mapper的上级目录)

三、springboot全局配置文件

1、application.properties
server.port=8090
server.servlet.context-path=/项目名

2、application.yml
server:
port: 8090
servlet:
context-path: /项目名

   语法:
		  1、树状结构,空格缩进
		  2、“.”----->“:”
		  3、“=”---->“:空格”

四、自定义banner
创建banner.txt,放到resources目录

五、两种发布方式
1、jar方式:
a、添加spring-boot-maven-plugin插件
b、cmd里输入 java -jar xxx.jar
2、war方式 :
a、设置打包方式为:war
b、设置tomcat启动器启动器的依赖范围:provided
c、设置war包的名字
d、告诉tomcat启动类的位置:

		           public class Application extends SpringBootServletInitializer {
				   @Override
				    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
					return builder.sources(Application.class);
				    }
			  }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王聊JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值