乙巳🐍年

acc8226 的博客

For full documentation visit mkdocs.org.

Commands

  • mkdocs new [dir-name] - Create a new project.
  • mkdocs serve - Start the live-reloading docs server.
  • mkdocs build - Build the documentation site.
  • mkdocs -h - Print help message and exit.

Project layout

mkdocs.yml    # The configuration file.
docs/
    index.md  # The documentation homepage.
    ...       # Other markdown pages, images and other files.
阅读全文 »

坚果云

【全平台】坚果云 主要用于存少量文档

功能完备, 多端同步, 为数不多支持 WebDAV 协议。 (在手机上使用 ES 浏览器可不装其他客户端都能访问坚果云内容)

非会员每个周期内流量有限, 适合保存文档
每月 1G 上传流量,下载 3G 流量。我一般将其作为同步文档用
非会员也有稀缺的同步盘。

坚果云 如何启用 WebDAV

小飞机

小飞机 用于网页分享不限速分享文件

阿里云盘

【win mac】阿里云盘 主打大容量 + 同步功能,高峰时段限速但一般没感觉。然后再搭配猫头鹰文件。可以读取阿里网盘的视频文件

建议安装客户端全量体验。而非网页版。

阿里云盘获取 refresh_token 的方法

首先要有一个属于自己的阿里云盘账号(一般为手机号);进入阿里云盘的官网地址,输入阿里云盘账号和密码登陆;在浏览器上按一次 F12 键,进入开发者工具模式,在顶上菜单栏点 Application ,然后在左边菜单找到 Local storage 下面的 https://www.aliyundrive.com 这个域名,点到这个域名会看到有一个 token 选项,再点 token ,就找到 refresh_token 了。

阅读全文 »

设置激活 Spring Profiles

SPRING Environment 为此提供了一个 API,但是您通常会设置一个 System 属性(spring.profiles.active) 或者一个 OS 环境变量(SPRING_PROFILES_ACTIVE)。此外,您可以使用 -D 参数启动应用程序(记住将其放在 main 类或 jar 归档之前) ,如下所示:

1
java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

或者

1
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=production

在 Spring Boot 中,您还可以在 application.properties 中设置 active profile ,如下面的示例所示:

可以是一个

1
spring.profiles.active=production

也可以是多个

1
spring.profiles.active=dev,hsqldb

或者使用 application.yml 形式

1
2
3
spring:
profiles:
active: dev, hsqldb
阅读全文 »

Spring Boot 介绍

官网地址
https://spring.io/projects/spring-boot

Spring Boot 2.3.12.RELEASE API 参考
https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/api/

Spring Boot 参考文档
https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/html/

优点

  • 快速创建独立运行 spring 项目和主流框架集成
  • 嵌入式 servlet 容器, 应用无需打成 war 包
  • starters 自动依赖与版本控制
  • 大量的自动配置, 简化开发, 也可修改默认值
  • 无需配置 xml, 无代码生成, 开箱即用
  • 准生产环境的运行时应用监控
  • 与云计算的天然集成

Spring 主要目标

  • 为所有 Spring 开发提供从根本上更快且可广泛访问的入门体验。
  • 开箱即用,但随着需求开始偏离默认值而迅速摆脱困境。
  • 提供大型项目(例如嵌入式服务器,安全性,度量标准,运行状况检查和外部化配置)通用的一系列非功能性功能。
  • 没有代码生成,也不需要 XML 配置。

环境要求

生成项目

Spring Initializr 在线生成

https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.13&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo project for Spring Boot&packageName=com.example.demo

https://start.aliyun.com/bootstrap.html/#!type=maven-project&language=java&architecture=none&platformVersion=2.4.1&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo project for Spring Boot&packageName=com.example.demo

pom 文件分析

方式一:通过指定 parent 父项目进行创建。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<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>
</dependencies>

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

</project>

方式二:通过 dependencyManagement 进行指定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.4.1</spring-boot.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>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<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>
<version>2.4.1</version>
<configuration>
<mainClass>com.example.demo.DemoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
  1. 启动引导 Spring

ReadingListApplication 在 Spring Boot 应用程序里有两个作用:配置和启动引导。首先,这是主要的 Spring 配置类。虽然Spring Boot的自动配置免除了很多Spring配置,但你还需要进行少量配置来启用自动配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package readinglist;

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

// 开启组件扫描和自动配置
@SpringBootApplication
public class ReadingListApplication {

public static void main(String[] args) {
// 负责启动引导应用程序
SpringApplication.run(ReadingListApplication.class, args);
}
}

@SpringBootApplication 开启了 Spring 的组件扫描和 Spring Boot 的自动配置功能。实际上,@SpringBootApplication 将三个有用的注解组合在了一起。

Spring的@Configuration:标明该类使用 Spring 基于 Java 的配置。虽然本书不会写太多配置,但我们会更倾向于使用基于 Java 而不是XML 的配置。

Spring 的@ComponentScan:启用组件扫描,这样你写的 Web 控制器类和其他组件才能被自动发现并注册为Spring应用程序上下文里的Bean。本章稍后会写一个简单的 Spring MVC 控制器,使用 @Controller 进行注解,这样组件扫描才能找到它。

Spring Boot的@EnableAutoConfiguration:这个不起眼的小注解也可以称为 @Abracadabra2,就是这一行配置开启了 Spring Boot 自动配置的魔力,让你不用再写成篇的配置了。

2. 测试 Spring Boot 应用程序
Initializr 还提供了一个测试类的骨架,可以基于它为你的应用程序编写测试。但 ReadingListApplicationTests 不止是个用于测试的占位符,它还是一个例子,告诉你如何为 Spring Boot 应用程序编写测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package readinglist;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import readinglist.ReadingListApplication;

@RunWith(SpringJUnit4ClassRunner.class)
// 通过Spring Boot加载上下文
@SpringApplicationConfiguration(
classes = ReadingListApplication.class)
@WebAppConfiguration

public class ReadingListApplicationTests {

@Test
public void contextLoads() { // 测试加载的上下文
}

}

3. 配置应用程序属性
你完全不用告诉 Spring Boot 为你加载 application.properties,只要它存在就会被加载,Spring 和应用程序代码都能获取其中的属性。

额外知识

Spring MVC 中将 @PathVariable 的使用

1
2
3
4
5
6
7
@RequestMapping(value="/{reader}", method=RequestMethod.GET)
public String readersBooks(
@PathVariable("reader") String reader,
Model model) {
...
}
}

自动配置微调

$ java -jar readinglist-0.0.1-SNAPSHOT.jar --server.port=8000

默认情况下,Spring Boot 会用 Logback(http://logback.qos.ch)来记录日志,并启用INFO级别输出到控制台。

Spring Boot应用程序有多种设置途径

Spring Boot 能从多种属性源获得属性,包括如下几处。

(1) 命令行参数
(2) java:comp/env 里的JNDI属性
(3) JVM 系统属性
(4) 操作系统环境变量
(5) 随机生成的带 random.* 前缀的属性(在设置其他属性时,可以引用它们,比如${random.long})
(6) 应用程序以外的 application.properties 或者 appliaction.yml 文件
(7) 打包在应用程序内的 application.properties 或者 appliaction.yml 文件
(8) 通过 @PropertySource 标注的属性源
(9) 默认属性

这个列表按照优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性。例如,命令行参数会覆盖其他属性源里的属性。

application.properties 和 application.yml 文件能放在以下四个位置。

(1) 外置,在相对于应用程序运行目录的/config子目录里。
(2) 外置,在应用程序运行的目录里。
(3) 内置,在config 包内。
(4) 内置,在Classpath 根目录。

同样,这个列表按照优先级排序。也就是说,/config 子目录里的application.properties 会覆盖应用程序Classpath里的application.properties 中的相同属性。

此外,如果你在同一优先级位置同时有 application.properties 和 application.yml,那么 application.yml 里的属性会覆盖 application.properties 里的属性。

Spring Boot 会为错误视图提供如下错误属性

timestamp:错误发生的时间。
status:HTTP状态码。
error:错误原因。
exception:异常的类名。
message:异常消息(如果这个错误是由异常引起的)。
errors:BindingResult异常里的各种错误(如果这个错误是由异常引起的)。
trace:异常跟踪信息(如果这个错误是由异常引起的)。
path:错误发生时请求的URL路径。

构建成品后运行项目

一键启用

1
2
3
4
5
6
mvn package
# 可以直接前台运行
java -jar target/springbootTest-0.0.1-SNAPSHOT.jar

# 或者后端启动
nohup java -jar 自己的springboot项目.jar >mylog.log 2>&1 &

搭配参数改变端口

1
java -jar demo-0.0.1-SNAPSHOT.jar --server.port=8012

参考

README - 《Spring Boot 中文文档》 - 书栈网 · BookStack
https://www.bookstack.cn/read/springboot/README.md

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded 的三种解决办法_Hello_World_QWP的博客-CSDN 博客
https://blog.csdn.net/Hello_World_QWP/article/details/83271592

有的时候创建命令行应用程序可能很有用,例如与项目的 REST API 交互,或者使用本地文件内容。

Spring Shell,可以和 SpringBoot 一起制作一款命令行工具。可轻松构建一个功能完整的 Shell (又名命令行)应用程序。

特性

Spring Shell 的特性包括

  • 一个简单的、注释驱动的编程模型,用于提供自定义命令
  • 使用 Spring Boot 自动配置功能作为命令插件策略的基础
  • 选项卡完成、彩色化和脚本执行
  • 自定义命令提示符、 shell 历史文件名、结果处理和错误
  • 基于领域特定条件的命令动态启用
  • 与 bean 验证 API 的集成
  • 已经内置的命令,如清晰的屏幕,华丽的帮助,退出
  • ASCII 艺术表格,具有格式化、对齐、花哨的边框等。
阅读全文 »
0%