springboot知识点整合相关技术一

这篇博客详细介绍了Springboot的使用,包括快速入门、静态资源处理、模板引擎(Freemarker和JSP)、全局异常处理、AOP日志记录、异步调用、自定义参数、环境配置、Mybatis集成、PageHelper分页、热部署原理及实践、性能优化、监控中心(Actuator)的搭建和AdminUI的使用,以及多数据源分布式事务的配置。
摘要由CSDN通过智能技术生成

1、快速入门

​ 1.1、新建一个maven项目sirius-springboot-quickstart(springboot入门),加入依赖

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sirius</groupId>
    <artifactId>sirius-springboot-quickstart</artifactId>
    <version>1.0-SNAPSHOT</version>
​
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath />
    </parent>
​
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

​ 1.2、启动类SiriusSpringBootQuickStartApplication

springboot启动原理: 采用springmvc注解方式启动,内置http服务器(默认tomcat)

package com.sirius.springboot;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
​
/**
 *  程序入口
 * @author EDZ
 */
//@EnableAutoConfiguration //@EnableAutoConfiguration注解方式启动 ,扫包范围默认当前类里面
//@ComponentScan(basePackages = {"com.sirius.springboot.controller"}) //@ComponentScan注解方式启动 ,缺点:如果扫包比较多写起来比较麻烦
//使用@SpringBootApplication方式启动
@SpringBootApplication
public class SiriusSpringBootQuickStartApplication {
​
    public static void main(String[] args) {
        //整个程序入口,启动springboot项目创建内置tomcat服务器,使用tomcat加载springmvc注解启动类
        SpringApplication.run(SiriusSpringBootQuickStartApplication.class, args);
    }
}

第一个controller例子

package com.sirius.springboot.controller;
​
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController //这个注解表示该类中的所有方法返回json格式(@Controller + @ResponseBody组合)
public class OrderController {
​
    @RequestMapping("/orderIndex")
    public String orderIndex() {
        return "springboot2第一个例子";
    }
}
​
访问: http://localhost:8080/orderIndex
备注:默认端口是8080

2、springboot静态资源访问

https://www.hangge.com/blog/cache/detail_2461.html

​ 在大型互联公司静态资源(js、css、图片)都是动静分离的(CDN缓存)。高并发优化方案,减少web项目流量开支,节省费用。

​ springboot要求静态资源存放在resources目录下,目录名符合如下规则:

/static 
/public 
/resources
/META-INF/resources
​
举例:在src/main/resources下创建static,在目录下方一个文件1.jpg。
启动程序后访问 http://localhost:8080/1.jpg。如能显示成功,配置成功。
不是访问 http://localhost:8080/static/1.jpg

3、使用Freemarker模板引擎渲染web视图

​ 使用Freemarker模板引擎渲染web视图,相当于动态页面转成静态html,目的是提高搜索引擎收入(排名在前)。

​ 3.1、pom文件引入:

<!-- 引入freemarker的依赖包 -->
<dependency>
      <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

​ 3.2、后台代码

​ 在src/main/resources下创建一个templates文件夹,新建后缀为*.ftl文件

FreemarkerController:

@Controller
public class FreemarkerController {
​
    @RequestMapping("/ftlIndex")
    public String ftlIndex(Map<String, Object> map) {
        map.put("name", "Freemarker用例");
        return "index";
    }
}

index.ftl页面:

这是我的ftl用例。
获取后端传入的值:${name}。
更多Freemarker表达式,自己百度......

4、使用JSP渲染Web视图

​ 使用springboot整合jsp并不是很好,因为springboot默认没有对jsp有很大的支持。

​ 依赖springboot外部tomcat支持,创建项目使用war不要jar要不可能会找不到页面。

​ 4.1、pom文件引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!-- Springboot 外部tomcat支持  -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

​ 4.2、在application.properties添加配置

# 不要把JSP页面存放在resources(资源文件)下,否则可能不能被访问到。
# 在src/main/webapp下建/WEB-INF/jsp/
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

​ 4.3、后台代码

@Controller
public class JspController {
​
    @RequestMapping("/jspIndex")
    public String orderIndex() {
        return "jspIndex";
    }
}

​ 4.4、JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
这是一个jsp demo
</body>
</html>

5、Springboot整合全局捕获异常

​ 全局捕获异常: 整个web请求项目全局捕获异常。

​ 5.1、后台代码

/**
 *  全局捕获异常案例
 * @author EDZ
 *
 */
@RestController
public class ErrorController {
​
    @RequestMapping("/getUser")
    public String error(int i) {
        int j = 1/i;
        return "success" + j;
    }
    
   //如果每个方法都可能会发生异常,每个方法都加上try不好,因此使用全局捕获异常处理
    //全局捕获异常使用aop技术,采用异常通知
    @RequestMapping("/getUser1")
    public String error1(int i) {
        int j = 0;
        try {
            j = 1 / i;
        } catch (Exception e) {
            return "系统错误";
        }
        return "success" + j;
    }
}
访问: http://localhost:8080/getUser?i=0
​
​

​ 5.2、全局异常捕获类

/**
 * 全局捕获异常案例
 * 1、捕获返回json格式
 * 2、捕获返回页面
 * @author EDZ
 */
@ControllerAdvice(basePackages = "com.sirius.springboot.controller") //切入点
public class GlobalExceptionHandler {
​
    //ModelAndView 返回页面
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody  //返回json到客户端
    public Map<String,Object> errorJson(){
        //实际开发中,会将错误记录在日志中,每天检测有哪些错误报告,通过邮件发送给你。写在MongoDB中
        Map<String,Object> errorResultMap = new HashMap<String, Object>();
        errorResultMap.put("code", "500");
        errorResultMap.put("msg", "AOP全局捕获异常-系统错误");
        return errorResultMap;
    }
}

6、使用AOP统一处理Web请求日志

​ 6.1、pom依赖

    <!-- springboot log4j -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <!-- springboot aop技术 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <a
学习尚硅谷视频整理的文档 Spring Boot 1 1 Spring Boot入门 4 1.1 简介 4 1.2 微服务(martin fowler发表了一篇文章) 5 1.3 环境约束 7 1.4 第一个Spring Boot项目(jar):HelloWorld 8 1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@Bean 27 2.7 配置文件占位符 30 2.8 Profile多环境支持 31 2.9 配置文件的加载位置 33 2.10 外部配置加载顺序 36 2.11 自动配置原理 37 2.12 @Conditional派生注解 41 3 Spring Boot与日志 42 3.1 日志框架分类和选择 42 3.2 SLF4j使用 43 3.3 其他日志框架统一转换成slf4j+logback 44 3.4 Spring Boot日志使用 45 3.5 Spring Boot默认配置 47 3.6 指定日志文件和日志Profile功能 52 3.7 切换日志框架(不使用SLF4j+LogBack) 54 4 Spring Boot与Web开发 55 4.1 Web开发简介 55 4.2 静态资源映射规则 56 4.3 引入Thymeleaf 60 4.4 Thymeleaf语法 61 4.5 SpringMVC自动配置原理 67 4.6 SpringBoot扩展与全面接管 70 4.7 如何修改SpringBoot的默认配置 72 4.8 【实验】CRUD操作 73 4.8.1 默认访问首页 73 4.8.2 登录页面国际化 74 4.8.3 登录 80 4.8.4 拦截器进行登录检查 81 4.8.5 实验要求(没按要求做,不想改了!) 82 4.8.6 CRUD-员工列表 83 4.8.7 CRUD-员工修改 86 4.8.8 CRUD-员工添加 87 4.8.9 CRUD-员工删除 88 4.9 错误处理原理&错误页面定制 90 4.10 配置嵌入式Servlet容器(springboot 1.50版本) 97 4.10.1 如何定制和修改Servelt容器的相关配置 97 4.10.2 注册servlet三大组件【servlet,filter,listener】 98 4.10.3 替换为其他嵌入式容器 102 4.10.4 嵌入式servlet容器自动配置原理 103 4.10.5 嵌入式servlet容器启动原理 103 4.11 使用外置的Servlet容器 104 4.11.1 步骤 104 4.11.2 原理 107 5 Spring Boot与Docker(虚拟化容器技术) 110 5.1 简介 110 5.2 核心概念 111 5.3 安装Docker 112 5.4 Docker常用命令&操作 113 5.5 安装MySQL示例 114 6 Spring Boot与数据访问 115 6.1 JDBC 115 6.1.1 实现 115 6.1.2 自动配置原理 116 6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程(Springboot 1.50版本) 128 7.1.1 创建SpringApplication对象 129 7.1.2 运行run方法 130 7.1.3 编写事件监听机制 132 8 Spring Boot自定义starters 136 8.1 概述 136 8.2 步骤 137 9 更多Springboot整合示例 144 10 Spring Boot与缓存 145 10.1 JSR107缓存规范 145 10.2 Spring的缓存抽象 146 10.2.1 基本概念 146 10.2.2 整合项目 146 10.2.3 CacheEnable注解 148 10.2.4 Cache注解 150 10.3 整合redis 154 10.3.1 在Docker上安装redis 154 10.3.2 Redis的Template 154 10.3.3 整合(百度) 155
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值