Spring boot configures modular Services - one configuration is used everywhere

Keywords: Windows Linux Google Java

Configuring the service module is a bit like the configuration center, but the function is not so powerful for the time being. Later, the source address will be updated after improvement.

catalog

Project function introduction

Project core configuration code

Environment configuration

Hidden configuration of MVC static resources

Project function introduction

System environmental monitoring:

MVC resource access mapping:

Swagger interface document configuration:

Redis operation RedisTemplate configuration:

Project core configuration code

Environment configuration

The corresponding initialization and dependency check are done by the decision system.

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: Environment configuration
 */
@Slf4j
@Configuration
public class EnvironmentConfig {

    /**
     * windows File upload path and set default value
     */
    @Value("${system.windows-upload-path:D:/upload/}")
    String windowsUploadPath;

    /**
     * windows File download path and set default
     */
    @Value("${system.windows-download-path:D:/download/}")
    String windowsDownloadPath;

    /**
     * linux File upload path and set default value
     */
    @Value("${system.linux-upload-path:/usr/local/upload/}")
    String linuxUploadPath;

    /**
     * linux File download path and set default
     */
    @Value("${system.linux-download-path:/usr/local/download/}")
    String linuxDownloadPath;

    /**
     * Set whether to enable Google webp image compression algorithm and set the default value
     */
    @Value("${system.enabled-google-webp:false}")
    boolean enableWebp;

    /**
     * Upload file address - external static constant parameter
     */
    public static String UPLOAD_PATH = "";

    /**
     * Download file address - external static constant parameter
     */
    public static String DOWNLOAD_PATH = "";

    /**
     * Linux to configure
     *
     * @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 to configure
     *
     * @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;
    }

    /**
     * Create upload and download directory
     */
    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();
        }
    }

    /**
     * Check whether the system sets webp dependent environment
     */
    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 ......");
        // Note that the windows path separator is ";" (semicolon) linux is ":" (colon)
        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!");
        }
    }

}

Hidden configuration of MVC static resources

This part is the insinuation of static resources such as pictures, js and so on, as well as the mapping of picture upload.

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 Static resource access configuration
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //The static directories related to the page are placed in the static directory of the project
        registry.addResourceHandler("/static/**", "/**").addResourceLocations("classpath:/static/");
        //registry.addResourceHandler("/static/webService/businessIco/**","/webService/businessIco/**").addResourceLocations("classpath:/static/webService/businessIco/");
        //The upload access path is as follows: http://host:port/upload/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + EnvironmentConfig.UPLOAD_PATH);
        //The download access path is as follows: http://host:post/download/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
        registry.addResourceHandler("/download/**").addResourceLocations("file:" + EnvironmentConfig.DOWNLOAD_PATH);
    }
}

To be continued

Posted by lmg on Fri, 29 May 2020 07:21:25 -0700