配置服务模块,有点像配置中心,不过功能暂时还没那么强大。后期完善后会更新源码地址。
目录
项目功能简介
系统环境监测:
MVC资源访问映射:
Swagger接口文档配置:
Redis 操作RedisTemplate配置:
项目核心配置代码
环境配置
通过判定系统做对应的初始化和依赖检查工作。
package com.patrol.config;
import com.patrol.beans.OSInfo;
import com.patrol.config.condition.LinuxCondition;
import com.patrol.config.condition.WindowsCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import java.io.File;
/**
* @Copyright: 2019-2021
* @FileName: EnvironmentConfig.java
* @Author: PJL
* @Date: 2020/4/29 15:16
* @Description: 环境配置
*/
@Slf4j
@Configuration
public class EnvironmentConfig {
/**
* windows文件上传路径并设置缺省值
*/
@Value("${system.windows-upload-path:D:/upload/}")
String windowsUploadPath;
/**
* windows文件下载路径并设置缺省值
*/
@Value("${system.windows-download-path:D:/download/}")
String windowsDownloadPath;
/**
* linux文件上传路径并设置缺省值
*/
@Value("${system.linux-upload-path:/usr/local/upload/}")
String linuxUploadPath;
/**
* linux文件下载路径并设置缺省值
*/
@Value("${system.linux-download-path:/usr/local/download/}")
String linuxDownloadPath;
/**
* 设置是否启用Google webp图像压缩算法并设置缺省值
*/
@Value("${system.enabled-google-webp:false}")
boolean enableWebp;
/**
* 上传文件地址-外部静态常量参数
*/
public static String UPLOAD_PATH = "";
/**
* 下载文件地址-外部静态常量参数
*/
public static String DOWNLOAD_PATH = "";
/**
* Linux配置
*
* @return
*/
@Bean
@Conditional(LinuxCondition.class)
OSInfo linuxInfo() {
log.info(">>>>>>Linux os set upload and download dirs.");
EnvironmentConfig.UPLOAD_PATH = linuxUploadPath;
EnvironmentConfig.DOWNLOAD_PATH = linuxDownloadPath;
OSInfo osInfo = new OSInfo();
osInfo.setOs("Linux");
osInfo.setUploadPath(EnvironmentConfig.UPLOAD_PATH);
osInfo.setDownloadPath(EnvironmentConfig.DOWNLOAD_PATH);
this.createUploadDownloadPath();
if (enableWebp) {
this.googleWebpLibraryCheck(false);
}
return osInfo;
}
/**
* Windows配置
*
* @return
*/
@Bean
@Conditional(WindowsCondition.class)
OSInfo windowsInfo() {
log.info(">>>>>>Windows os set upload and download dirs.");
EnvironmentConfig.UPLOAD_PATH = windowsDownloadPath;
EnvironmentConfig.DOWNLOAD_PATH = windowsDownloadPath;
OSInfo osInfo = new OSInfo();
osInfo.setOs("Windows");
osInfo.setUploadPath(EnvironmentConfig.UPLOAD_PATH);
osInfo.setDownloadPath(EnvironmentConfig.DOWNLOAD_PATH);
this.createUploadDownloadPath();
if (enableWebp) {
this.googleWebpLibraryCheck(true);
}
return osInfo;
}
/**
* 创建上传下载目录
*/
private void createUploadDownloadPath() {
log.info(">>>>>>createUploadDownloadPath dirs.");
File upload = new File(EnvironmentConfig.UPLOAD_PATH);
File download = new File(EnvironmentConfig.DOWNLOAD_PATH);
if (!upload.exists()) {
upload.mkdir();
}
if (!download.exists()) {
download.mkdir();
}
}
/**
* 检查系统是否设置webp依赖环境
*/
private void googleWebpLibraryCheck(boolean isWindows) {
String path = System.getProperty("java.library.path");
log.info(">>>>>>Google webp library load environment ");
log.info(">>>>>>Google webp available paths: {}", path);
log.info(">>>>>>Google webp library load environment to check ......");
// 注意windows路径分隔符是";"(分号) linux是":"(冒号)
String[] dirs = path.split(isWindows ? ";" : ":");
String linuxFile = "libwebp-imageio.so";
String windowsFile = "webp-imageio.dll";
File file;
boolean isExists = false;
for (String filePath : dirs) {
filePath = isWindows ? new StringBuffer(filePath).append("\\").append(windowsFile).toString() : new StringBuffer(filePath).append("/").append(linuxFile).toString();
file = new File(filePath);
if (file.exists()) {
isExists = true;
break;
}
}
if (!isExists) {
String library = isWindows ? windowsFile : linuxFile;
log.info(">>>>>>Google webp library load environment to check ......{} is not found.", library);
log.info(">>>>>>Please put {} in any paths of Java environment. ", library);
System.exit(0);
} else {
log.info(">>>>>>Google webp library load environment to check ......webp is found success!");
}
}
}
MVC静态资源隐射配置
这部分是静态资源如图片、js等等的隐射,以及图片上传的映射。
package com.patrol.config.resource;
import com.patrol.config.EnvironmentConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @Copyright: 2019-2021
* @FileName: WebMvcConfig.java
* @Author: PJL
* @Date: 2020/5/27 12:49
* @Description: MVC静态资源访问配置
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//和页面有关的静态目录都放在项目的static目录下
registry.addResourceHandler("/static/**", "/**").addResourceLocations("classpath:/static/");
//registry.addResourceHandler("/static/webService/businessIco/**","/webService/businessIco/**").addResourceLocations("classpath:/static/webService/businessIco/");
//上传访问路径如:http://host:port/upload/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
registry.addResourceHandler("/upload/**").addResourceLocations("file:" + EnvironmentConfig.UPLOAD_PATH);
//下载访问路径如:http://host:post/download/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
registry.addResourceHandler("/download/**").addResourceLocations("file:" + EnvironmentConfig.DOWNLOAD_PATH);
}
}
待续......