在 Axios 的 REST 封装 介绍了怎么封装 Axios,在这一篇文章中,使用 Fluent 风格的链式调用对 Axios 再做了一次封装,两种方式都差不多,只是风格不同,可根据自己的喜好选择一种使用。
大圣,此去欲何?踏南天,碎凌霄。若一去不回……?便一去不回!
在 Axios 的 REST 封装 介绍了怎么封装 Axios,在这一篇文章中,使用 Fluent 风格的链式调用对 Axios 再做了一次封装,两种方式都差不多,只是风格不同,可根据自己的喜好选择一种使用。
在浏览器中点击 PDF 文件的链接:
被访问都是 PDF 文件,为啥在网站 A 和在网站 B 访问时,浏览器的行为不一样,是什么东西影响它在浏览器中的行为呢?答案就是浏览器会根据响应的 Content-Type 来决定下载还是打开它们 (当然 Content-Type 的值只是一个 hit,具体的操作还是要看浏览器的实现)。
文件的类型非常多,怎么获取文件的 Content-Type 是什么呢?Java 1.7 提供 java.nio.file.Files.probeContentType(Path path)
用于尝试获取文件的 Content-Type,但发现支持的文件类型不够全面,查看方法 probeContentType 的帮助文档:
This method uses the installed FileTypeDetector implementations to probe the given file to determine its content type. Each file type detector’s probeContentType is invoked, in turn, to probe the file type. If the file is recognized then the content type is returned. If the file is not recognized by any of the installed file type detectors then a system-default file type detector is invoked to guess the content type.
A given invocation of the Java virtual machine maintains a system-wide list of file type detectors. Installed file type detectors are loaded using the service-provider loading facility defined by the ServiceLoader class. Installed file type detectors are loaded using the system class loader. If the system class loader cannot be found then the extension class loader is used; If the extension class loader cannot be found then the bootstrap class loader is used. File type detectors are typically installed by placing them in a JAR file on the application class path or in the extension directory, the JAR file contains a provider-configuration file named java.nio.file.spi.FileTypeDetector in the resource directory META-INF/services, and the file lists one or more fully-qualified names of concrete subclass of FileTypeDetector that have a zero argument constructor. If the process of locating or instantiating the installed file type detectors fails then an unspecified error is thrown. The ordering that installed providers are located is implementation specific.
以在 Test.java 中加载 app-1.jar 和 app-2.jar 中的类 com.xtuer.Aloha 为例演示 classpath 中 jar 被加载的顺序。
创建项目,里面只有 1 个类 com.xtuer.Aloha,分别打包成 2 个版本的 jar 包:
1 | package com.xtuer; |
项目的目录结构 (此 gradle 管理的 Java 项目结构仅作为参考):
1 | app |
可以使用 classpath 指定类加载的路径,但 classpath 的生效是有条件的:
命令 | classpath 生效 | 说明 |
---|---|---|
java -cp .;lib/x.jar Test | ✔ | 运行 class |
java -cp lib/x.jar -jar app.jar | ✖ | 运行 jar |
Spring Boot 程序大多是打成 jar 包,使用 java -jar boot.jar
的方式启动 (此时 -cp 无效),可以使用 loader.path 指定类加载路径加载其他 jar,但 loader.path 生效是有条件的:
命令 | MANIFEST.MF 的 Main-Class | loader.path 生效 | 打包配置 |
---|---|---|---|
java -Dloader.path=./lib -jar app.jar | JarLauncher | ✖ | 默认配置 |
java -Dloader.path=./lib -jar app.jar | PropertiesLauncher | ✔ | 额外配置 |
loader.path 实现了 classpath 的功能。
为了使用 loader.path,需要把 jar 包的 Main-Class 配置为 PropertiesLauncher,在 build.gradle 中如下配置,可参考 Using the PropertiesLauncher:
1 | bootJar { |
无论启动类是 JarLauncher 或者 PropertiesLauncher,loader.path 引入的 jar 和 Spring Boot 中 lib/*.jar 都是使用类加载器 org.springframework.boot.loader.LaunchedURLClassLoader 进行加载,也即是说他们使用的是同一个类加载器。
注意到同一个程序,打包成不同类型时,PropertiesLauncher (20s) 比 JarLauncher (8s) 启动慢很多。
使用 Jasypt 加密 Spring Boot 配置中的项,例如数据库密码。详细介绍可阅读 Springboot 配置文件、隐私数据脱敏的最佳实践 和 Spring Boot 配置文件密码加密两种方案。
本文主要介绍具体使用部分,注意 jasypt-spring-boot-starter 2.0 和 3.0 的区别。3.0 前后虽然都是使用 jasypt-1.9.3.jar,但是生成密文命令的参数有点区别:
版本 | algorithm 默认值 | iv-generator 默认值 | 加密命令 |
---|---|---|---|
2.1.2 | PBEWithMD5AndDES | org.jasypt.iv.NoIvGenerator | java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=root password=xtuer algorithm=PBEWithMD5AndDES |
3.0.3 | PBEWITHHMACSHA512ANDAES_256 | org.jasypt.iv.RandomIvGenerator | java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=root password=xtuer algorithm=PBEWITHHMACSHA512ANDAES_256 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator |
提示:
- input 为要加密的内容,password 为加密的密钥
- jasypt-1.9.3.jar 可以从 maven 或者 gradle 本地仓库中找到
使用 kubernetes-sigs/yaml 来把对象序列化为 yaml 字符串,把 yaml 字符串反序列化为 go 对象。
1 | package main |
下载 yaml:
1 | go mod init |
编译执行:
1 | go run test.go |
运行输出:
1 | id: 1 |
在虚拟机中安装 CentOS,主要有以下步骤:
访问 https://www.virtualbox.org/ 下载安装即可。
Axure 创建弹窗,主要涉及以下几个方面:
下面我们实现一个如图的弹窗功能: