浅学SpringBoot

一、Spring Boot 的简单介绍

1、springboot的概念

用来简化Spring应用的初始搭建以及开发过程。 -使用springboot以后,搭建一个spring应用和开发变得很简单。
Springboot就是一些写好了maven的模块,我们在使用SPring就不需以传统的方式来用,只需要以maven导入对应的springboot模块,就能完成一大堆操作。简单的说,它使用maven的方式对Spring应用开发进行进一步封装和简化。

2、使用springboot的原因

简化spring应用搭建,开发,部署,监控的开发工具。使编码更简单,使配置更简单,使部署更简单,使监控更简单。

3、springboot的功能

无需手动管理依赖jar包的版本.升级spring boot时,这些依赖的版本也会随之升级。个人无需指定版本号。

Spring Boot在 org.springframework.boot 组下提供的一些Starters:
在这里插入图片描述
spring-boot-starter-web:web支持,其实就是springmvc简化使用。
Spring-boot-starter-jdbc:springboot对jdbc支持
Spring-boot-starter-data jpa:springboot对data jpa支持
Spring-boot-starter-mybatis:springboot对mybatis支持
Spring-boot-starter-test:springboot对test支持

Spring-boot-starter-redis
Spring-boot-starter-es
Spring-boot-starter-sorl

二、Spring Boot 入门

1、创建maven项目

在这里插入图片描述

2、在pom.xml中导入springboot的依赖

  • parent父节点中
<!--
spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
 -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.5.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<!--springboot版本管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
  • 在子模块module中
<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

3、代码测试

在子模块中

  • 写个controller类
@RestController//@RestController的效果 相当于在方法上面@ResponseBody  返回json数据
public class HiController {

    @RequestMapping("/index")
    public String index(){
        return "idnex";
    }
}

  • 新建启动类完成启动
@SpringBootApplication
public class App {
    //在main方法上启动应用程序
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

4、启动后流程

  • 启动类启动时,控制台打印出端口信息
    在这里插入图片描述
  • 然后在流程器中输入地址:http://localhost:8080/index
  • 最后浏览器页面显示json数据:index - - - >完成步骤
    在这里插入图片描述

5、热部署

使用spring-boot-devtools来实现热部署,在发现代码有更改之后,重新启动应用。
深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类(自己写的),称为 restart ClassLoader。

  • 在模块的pom.xml中导入包和插件
<!-- 热部署-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <optional>true</optional>
     <scope>true</scope>
 </dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--fork :  如果没有该项配置,可能devtools不会起作用,即应用不会restart -->
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

注意:热部署完后,改动代码后需要Ctrl+F9手动编译,然后就会自动启动应用app

三、Spring Boot Web

需要的是跳转到一个页面或者获取一个Json数据。

1、跳转JSP页面

(1)、创建一个maven web project。项目名为springboot-jsp

在这里插入图片描述

(2)、在pom.xml中导入maven依赖包

由于在父节点pring boot paren中导入了一个maven包,那么下面和spring boot相关的就不需要引入版本了。详细往上看一下springboot-parent的pom.xml中的maven包,有备注。

		<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- servlet 依赖. -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!-- tomcat 的支持. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
(3)、配置application.properties对jsp的支持。(也可以用YMEL配置

类似springmvc的视图解析器。

添加src/main/resources/application.properties:

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application

下面是项目中的配置信息:
在这里插入图片描述

(4)、编写jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    成功进入jsp页面
</body>
</html>
(5)、编写测试的controller
@Controller
public class ControllerTest {

    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
(6)、编写测试的启动类
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}
(7)、模块开发中,启动tomcat 访问jsp 需要配置

在这里插入图片描述
在这里插入图片描述

(8)、最后在浏览器中输入地址:http://localhost:8080/index

在这里插入图片描述

2、支持Freemaker模板技术

将动态的jsp页面生成想要的静态HTML页面。页面静态化

(1)创建一个maven项目。名为springboot-freeMark

在这里插入图片描述

(2)、导入maven依赖
<!--springmvc支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 对freemark的支持-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
(3)、配置application.properties(之后会用YMEL配置)
# FreeeMarker 模板引擎配置
#       设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
#        关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
(4)、在资源文件resources中准备一个模板templates。

注意:用上面的配置,文件名只能是templates,否则会找不到
在这里插入图片描述
index.ftl模板代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Hello World!</title>
</head>
<body>
<#--${msg}:从controller中存入在model中取值-->
<h1>你好 , ${msg}</h1>
</body>
</html>
(5)编写测试的controller
@Controller
public class FreeMarkController {
    @RequestMapping("/index")
    public String index(Model model){
        //往model中存值,在前台页面通过$(msg)取值
        model.addAttribute("msg", "张三");
        return "index";
    }
}
(6)、编写测试的启动类
@SpringBootApplication
public class FreeMarkApp {
    public static void main(String[] args) {
        SpringApplication.run(FreeMarkApp.class);
    }
}
(7)、最后在浏览器中输入地址:http://localhost:8080/index

在这里插入图片描述

3、获取JSON数据

在上面的springboot-freemark项目中做此测试

(1)、准备个User对象类,方便测试
public class User {

    private Long id;
    private String name;

    private Date birthday;

    public User(Long id, String name,Date birthday) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }
    //注意springmvc的时间格式。
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public User(){}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}

(2)、返回字符串类型的JSON数据

在类上注解@RestController,那么在方法上就不用配置@ResponseBody,同样也返回json格式数据
如果在类上注解原来的@Contrller,要想返回json数据,就必须在方法上注解@ResponseBody

  • 编写测试的controller
@RestController
public class JsonController {
    @RequestMapping("/json")
    //不用@ResponseBody 类上有注解@RestController,也能返回json数据了
    public String json(){
        return "zhangsan";
    }
}
  • 编写测试的启动类。
@SpringBootApplication
public class FreeMarkApp {
    public static void main(String[] args) {
        SpringApplication.run(FreeMarkApp.class);
    }
}
(3)、返回对象类型的JSON数据,还带有时间格式
  • 编写测试的controller
@RequestMapping("/json1")
//不用@ResponseBody 类上有注解@RestController,也能返回json数据了
public User json1(){
    return new User(1L,"张三",new Date());
}
  • 编写测试的启动类。
@SpringBootApplication
public class FreeMarkApp {
    public static void main(String[] args) {
        SpringApplication.run(FreeMarkApp.class);
    }
}
(4)、返回List类型的JSON数据
  • 编写测试的controller
//3、List集合类型的JSON数据,还带有时间格式
    @RequestMapping("/json2")
    //不用@ResponseBody 类上有注解@RestController,也能返回json数据了
    public List<User> json2(){
        //返回list集合数据
        return Arrays.asList(new User(1L,"张三",new Date()),
                             new User(2L,"李四",new Date()),
                             new User(3L,"王五",new Date()));
    }
  • 编写测试的启动类。
@SpringBootApplication
public class FreeMarkApp {
    public static void main(String[] args) {
        SpringApplication.run(FreeMarkApp.class);
    }
}

四、springboot的配置

1、YAML

文件名:application.yml

(1)、YAML概念

Springboot除了支持properties的配置,还支持YAML,而且用得较多。
也可以称为“雅梅尔”。
用途广泛,用于配置文件,日志文件,跨语言数据共享,对象持久化,复杂的数据结构。

(2)YAML的原则与语法
  • 原则:
    1、大小写敏感
    2、使用缩进表示层级关系
    3、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
    4、使用#表示注释
    5、字符串可以不用引号标注
  • 语法:
    1、
    在这里插入图片描述
    3、相互嵌套
    在这里插入图片描述
(3)、操作YAML配置更改端口

就在子项目springboot-freemark中测试

  • 在资源文件resources中配置YAML。将之前的application.properties配置文件处理一下,不能同时有两者配置文件
    在这里插入图片描述
  • 启动启动类后,控制台打印出tomcat的端口:已由8080改为9000
    在这里插入图片描述
  • 最后在浏览器中输入地址:http://localhost:9000/json
    在这里插入图片描述

2、Springboot 打包运行方式

项目要上线,就直接把项目打包成jar包,就直接运行,只依赖JDK。
基于上面子模块springboot-freemark中进行操作

  • 先将pom.xml中war改为jar,这样就打包成jar包,否则是war包
    在这里插入图片描述
(1)、在pom.xml中导入打包运行的插件
<!--springboot打包运行配置-->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--主类 包含main:启动类位置-->
                <mainClass>cn.lyq.springboot.freemark.FreeMarkApp</mainClass>
                <layout>JAR</layout>
            </configuration>
        </plugin>
    </plugins>
</build>

在这里插入图片描述

(2)、用cmd进入项目 运行 mvn clean package spring-boot:repackage

找到springboot-freemark项目文件,用cmd命令窗口进行maven打包
在这里插入图片描述

在命令窗口中运行mvn clean package spring-boot:repackage命令
在这里插入图片描述

在项目的编译target中,生成一个jar包
在这里插入图片描述

直接在文件中用cmd命令运行jar包
在这里插入图片描述

在命令窗口输入命令:java -jar springboot-freemark-1.0-SNAPSHOT.jar
先将idea中的运行关闭,否则命令窗口无法启动
在这里插入图片描述

直接就在命令窗口运行springboot了
在这里插入图片描述

在浏览器中输入地址:http://localhost:9000/json1 有json数据,表示打包成功
在这里插入图片描述

3、Spring Profiles多环境支持

还是基于子模块springboot-freemark来进行测试操作

(1)Spring Profiles概念

Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只在特定的环
境下生效。以后项目中,测试一套环境、开发一套、产品一套等等,可以切换使用。

(2)多环境配置的实现

默认使用application.properties/yml的配置

  • 定义一个主文件环境配置 application.yml 。
    这里的配置:指定激活application-dev.yml的端口
    在这里插入图片描述
1)第一种实现方式:常用
  • 定义一个开发的文件环境配置
    在这里插入图片描述
  • 定义一个测试文件环境配置
    在这里插入图片描述
  • 在主文件配置中,已经指定激活开发环境的端口,所有,启动springboot时,tomcat端口号就为9001
    在这里插入图片描述
2)第二种实现方式:激活指定profile

项目打包成jar包。然后用cmd的方式在命令窗口指定激活。还是和打包运行方式一样,先关闭idea的springboot的启动,要不然大不了jar包。

  • 先将项目打包成jar包。命令窗口输入:mvn clean package spring-boot:repackage指令
    在这里插入图片描述
  • 然后在运行的时候指定激活环境
    命令行:java -jar -Dspring.profiles.active=test jar包的名字:表示指定激活测试环境
    在这里插入图片描述
  • 命令窗口运行指令,tomcat端口为指定激活环境的端口号。测试环境:text 的端口为上面定义的9002
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值