|
@@ -17,7 +17,427 @@ src/main/resources/application-test.properties
|
|
|
src/main/resources/application.properties
|
|
|
|
|
|
|
|
|
+技术栈
|
|
|
+
|
|
|
+* springboot3.2
|
|
|
+* mysql5.7
|
|
|
+* Redis
|
|
|
+* MongoDB
|
|
|
+* maven 3.5.0
|
|
|
+* jdk 11
|
|
|
+* tomcat
|
|
|
+
|
|
|
## Reference
|
|
|
|
|
|
|
|
|
|
|
|
+## springboot 版本对比
|
|
|
+
|
|
|
+| Spring Boot 2.x | Spring Boot 3.x | |
|
|
|
+| :-------------- | :--------------- | ------------------ |
|
|
|
+| Spring版本 | Spring 5.x | Spring 6.x |
|
|
|
+| JDK版本 | >= 1.8 | >= 17 |
|
|
|
+| Tomcat版本 | 9.x | 10.x |
|
|
|
+| Annotation包 | javax.annotation | jakarta.annotation |
|
|
|
+| Servlet包 | javax.servlet | jakarta.servlet |
|
|
|
+| JMS包 | javax.jms | jakarta.jms |
|
|
|
+| JavaMail包 | javax.mail | jakarta.mail |
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# 静态资源
|
|
|
+
|
|
|
+css 样式;
|
|
|
+js 脚本;
|
|
|
+favicon.ico 图标等;
|
|
|
+
|
|
|
+Spring Boot 访问静态资源,默认有两个默认目录:
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+- `classpath/static` 目录:`src/main/resource`
|
|
|
+- `ServletContext` 根目录下: `src/main/webapp`
|
|
|
+
|
|
|
+这里打包app.jar包后,WEB-INF 下面的 classes 目录为 classpath
|
|
|
+
|
|
|
+
|
|
|
+配置文件可以更改目录路径:
|
|
|
+
|
|
|
+在 properties 文件里面设置 spring.resources.static-locations 就ok了。
|
|
|
+
|
|
|
+
|
|
|
+```
|
|
|
+vim application.properties
|
|
|
+
|
|
|
+spring.resources.static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
|
|
|
+
|
|
|
+
|
|
|
+spring.mvc.static-path-pattern=/static/**
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+## 前后端分离
|
|
|
+
|
|
|
+
|
|
|
+## 配置日期格式化
|
|
|
+
|
|
|
+要让 Spring Boot 能够按照指定的格式进行日期类型转换,需要做以下步骤:
|
|
|
+
|
|
|
+- 定义一个 `MvcConfig` 类,让其实现 `WebMvcConfigurer` 接口;
|
|
|
+- 重写 `addFormatters` 方法;
|
|
|
+- 添加一个 `DateFormatter`;
|
|
|
+
|
|
|
+```java
|
|
|
+package site.exception.springbootdateformat.config;
|
|
|
+
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.format.FormatterRegistry;
|
|
|
+import org.springframework.format.datetime.DateFormatter;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class MvcConfig implements WebMvcConfigurer {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置日期格式化
|
|
|
+ * @param registry
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void addFormatters(FormatterRegistry registry) {
|
|
|
+ registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+## 配置跨域
|
|
|
+
|
|
|
+实现 `addCorsMappings` 接口来添加规则来允许跨域访问:
|
|
|
+
|
|
|
+```java
|
|
|
+package site.exception.config;
|
|
|
+
|
|
|
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
|
+
|
|
|
+public class CORSConfig extends WebMvcConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addCorsMappings(CorsRegistry registry) {
|
|
|
+ // 允许所有跨域访问
|
|
|
+ registry.addMapping("/**");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+`/**` 允许所有域名都能够跨域访问,下面看看更为精细的控制:
|
|
|
+
|
|
|
+```JAVA
|
|
|
+package site.exception.config;
|
|
|
+
|
|
|
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author www.exception.site (exception 教程网)
|
|
|
+ * @date 2019/2/13
|
|
|
+ * @time 下午8:36
|
|
|
+ * @discription
|
|
|
+ **/
|
|
|
+public class CORSConfig extends WebMvcConfigurerAdapter {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addCorsMappings(CorsRegistry registry) {
|
|
|
+ registry.addMapping("/api/**")
|
|
|
+ .allowedOrigins("http://www.exception.site")
|
|
|
+ .allowedMethods("POST", "GET");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
+通过上面的配置,只允许来自 [www.exception.site](https://www.exception.site/) 的跨域访问,并且限定只能对 `/api` 下的所有接口进行跨域访问,同时只能访问 `POST` 和 `GET` 方法。
|
|
|
+
|
|
|
+## Jackson 配置
|
|
|
+
|
|
|
+Jackson 是 Spring Boot 内置的 Json 解析框架,用来完成出入参的序列化和反序列化。通常,我们会在 Controller 类中方法上,加上 @RequestBody 或者 @ResponseBody 注解,Spring Boot 会自动对出入参做 Json 解析与转换工作。
|
|
|
+
|
|
|
+## 配置 Jetty
|
|
|
+
|
|
|
+默认自带的 tomcat,可以改为jetty 轻量,添加依赖
|
|
|
+
|
|
|
+```xml
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-web</artifactId>
|
|
|
+ <!-- 移除掉默认支持的 Tomcat -->
|
|
|
+ <exclusions>
|
|
|
+ <exclusion>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-tomcat</artifactId>
|
|
|
+ </exclusion>
|
|
|
+ </exclusions>
|
|
|
+</dependency>
|
|
|
+
|
|
|
+<!-- 添加 jetty 依赖 -->
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-jetty</artifactId>
|
|
|
+</dependency>
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+## hello world
|
|
|
+
|
|
|
+pom.xml 配置
|
|
|
+
|
|
|
+
|
|
|
+```
|
|
|
+ <parent>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-parent</artifactId>
|
|
|
+ <version>2.1.4.RELEASE</version>
|
|
|
+ <relativePath/> <!-- lookup parent from repository -->
|
|
|
+ </parent>
|
|
|
+
|
|
|
+
|
|
|
+ <properties>
|
|
|
+ <java.version>1.8</java.version>
|
|
|
+ </properties>
|
|
|
+
|
|
|
+ <dependencies>
|
|
|
+ <dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter</artifactId>
|
|
|
+ </dependency>
|
|
|
+
|
|
|
+ <dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-test</artifactId>
|
|
|
+ <scope>test</scope>
|
|
|
+ </dependency>
|
|
|
+
|
|
|
+ <dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-web</artifactId>
|
|
|
+ </dependency>
|
|
|
+
|
|
|
+ <dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-autoconfigure</artifactId>
|
|
|
+ </dependency>
|
|
|
+ </dependencies>
|
|
|
+
|
|
|
+ <build>
|
|
|
+ <plugins>
|
|
|
+ <plugin>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-maven-plugin</artifactId>
|
|
|
+ </plugin>
|
|
|
+ </plugins>
|
|
|
+ </build>
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# spring boot 介绍
|
|
|
+
|
|
|
+## 微服务
|
|
|
+
|
|
|
+将一个大系统分割为多个独立的应用,拥有自己的进程。易部署,易扩展。
|
|
|
+
|
|
|
+## Spring boot
|
|
|
+
|
|
|
+## 它是如何工作的?
|
|
|
+
|
|
|
+Spring Boot 使用 **@EnableAutoConfiguration** 注解根据您添加到项目的依赖项自动配置您的应用程序。 例如,如果 MySQL 数据库在你的类路径上,但你没有配置任何数据库连接,那么 Spring Boot 会自动配置一个内存数据库。
|
|
|
+
|
|
|
+Spring Boot 应用程序的入口点是包含 **@SpringBootApplication** 注解的类和 main 方法。
|
|
|
+
|
|
|
+Spring Boot 通过使用 **@ComponentScan** 注解自动扫描项目中包含的所有组件。
|
|
|
+
|
|
|
+------
|
|
|
+
|
|
|
+## Spring Boot 启动器
|
|
|
+
|
|
|
+处理依赖管理对于大型项目来说是一项艰巨的任务。 Spring Boot 通过提供一组依赖项方便开发人员解决了这个问题。
|
|
|
+
|
|
|
+例如,如果您想使用 Spring 和 JPA 进行数据库访问,则在项目中包含 **spring-boot-starter-data-jpa** 依赖项就足够了。
|
|
|
+
|
|
|
+请注意,所有 Spring Boot 启动器都遵循相同的命名模式 **spring-boot-starter-** *,其中 * 表示它是应用程序的一种类型。
|
|
|
+
|
|
|
+### 示例
|
|
|
+
|
|
|
+请查看下面解释的以下 Spring Boot 启动器以更好地理解 −
|
|
|
+
|
|
|
+**Spring Boot Starter Actuator 依赖项** 用于监视和管理您的应用程序。 其代码如下所示 −
|
|
|
+
|
|
|
+```
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-actuator</artifactId>
|
|
|
+</dependency>
|
|
|
+```
|
|
|
+
|
|
|
+**Spring Boot Starter Security 依赖项** 用于 Spring Security。 其代码如下所示 −
|
|
|
+
|
|
|
+```
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-security</artifactId>
|
|
|
+</dependency>
|
|
|
+```
|
|
|
+
|
|
|
+**Spring Boot Starter web 依赖** 用于编写一个 Rest Endpoints。 其代码如下所示 −
|
|
|
+
|
|
|
+```
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-web</artifactId>
|
|
|
+</dependency>
|
|
|
+```
|
|
|
+
|
|
|
+**Spring Boot Starter Thyme Leaf 依赖项** 用于创建 Web 应用程序。 其代码如下所示 −
|
|
|
+
|
|
|
+```
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
|
|
|
+</dependency>
|
|
|
+```
|
|
|
+
|
|
|
+**Spring Boot Starter Test 依赖项**用于编写测试用例。 其代码如下所示 −
|
|
|
+
|
|
|
+```
|
|
|
+<dependency>
|
|
|
+ <groupId>org.springframework.boot</groupId>
|
|
|
+ <artifactId>spring-boot-starter-test</artifactId>
|
|
|
+</dependency>
|
|
|
+```
|
|
|
+
|
|
|
+------
|
|
|
+
|
|
|
+## 自动配置
|
|
|
+
|
|
|
+Spring Boot Auto Configuration 根据您在项目中添加的 JAR 依赖项自动配置您的 Spring 应用程序。 例如,如果 MySQL 数据库在你的类路径上,但你没有配置任何数据库连接,那么 Spring Boot 会自动配置一个内存数据库。
|
|
|
+
|
|
|
+为此,您需要在 main 主类文件中添加 **@EnableAutoConfiguration** 注解或 **@SpringBootApplication** 注解。 然后,您的 Spring Boot 应用程序将被自动配置。
|
|
|
+
|
|
|
+观察以下代码以更好地理解 −
|
|
|
+
|
|
|
+```
|
|
|
+import org.springframework.boot.SpringApplication;
|
|
|
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
+
|
|
|
+@EnableAutoConfiguration
|
|
|
+public class DemoApplication {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ SpringApplication.run(DemoApplication.class, args);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+------
|
|
|
+
|
|
|
+## Spring Boot 应用程序
|
|
|
+
|
|
|
+Spring Boot Application 的入口点是包含 **@SpringBootApplication** 注解的类。 这个类应该有运行 Spring Boot 应用程序的 main 方法。 **@SpringBootApplication** 注解包括自动配置、组件扫描和 Spring Boot 配置。
|
|
|
+
|
|
|
+如果在类中添加了**@SpringBootApplication**注解,则不需要添加**@EnableAutoConfiguration、@ComponentScan**和**@SpringBootConfiguration**注解。 **@SpringBootApplication** 注解包括所有其他注解。
|
|
|
+
|
|
|
+观察以下代码以更好地理解 −
|
|
|
+
|
|
|
+```
|
|
|
+import org.springframework.boot.SpringApplication;
|
|
|
+import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
+
|
|
|
+@SpringBootApplication
|
|
|
+public class DemoApplication {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ SpringApplication.run(DemoApplication.class, args);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+```
|
|
|
+
|
|
|
+------
|
|
|
+
|
|
|
+## 组件扫描
|
|
|
+
|
|
|
+Spring Boot 应用程序在应用程序初始化时扫描所有 bean 和包声明。 您需要为您的类文件添加 **@ComponentScan** 注解以扫描您在项目中添加的组件。
|
|
|
+
|
|
|
+观察以下代码以更好地理解 −
|
|
|
+
|
|
|
+```
|
|
|
+import org.springframework.boot.SpringApplication;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+
|
|
|
+@ComponentScan
|
|
|
+public class DemoApplication {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ SpringApplication.run(DemoApplication.class, args);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+```
|
|
|
+
|