springboot知识点整合相关技术一

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值