SpringBoot配置模块化服务-一处配置随处使用

配置服务模块,有点像配置中心,不过功能暂时还没那么强大。后期完善后会更新源码地址。

目录

项目功能简介

项目核心配置代码

环境配置

MVC静态资源隐射配置


项目功能简介

系统环境监测:

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);
    }
}

待续......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值