refactor: 使用常量优化部分配置属性名

This commit is contained in:
2024-01-02 22:14:00 +08:00
parent 1f3687df6f
commit 20250681da
15 changed files with 109 additions and 15 deletions

View File

@@ -36,6 +36,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import java.util.HashMap;
import java.util.Map;
@@ -50,7 +51,7 @@ import java.util.Properties;
@Slf4j
@AutoConfiguration
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "continew-starter.captcha.behavior", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_BEHAVIOR, name = PropertiesConstants.ENABLED, havingValue = "true")
@EnableConfigurationProperties(BehaviorCaptchaProperties.class)
public class BehaviorCaptchaAutoConfiguration {

View File

@@ -33,6 +33,7 @@ import org.springframework.context.annotation.Bean;
import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration;
import top.charles7c.continew.starter.captcha.behavior.enums.StorageType;
import top.charles7c.continew.starter.captcha.behavior.impl.BehaviorCaptchaCacheServiceImpl;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
/**
* 行为验证码缓存配置
@@ -49,7 +50,7 @@ abstract class BehaviorCaptchaCacheConfiguration {
*/
@ConditionalOnClass(RedisClient.class)
@AutoConfigureBefore(RedissonAutoConfiguration.class)
@ConditionalOnProperty(name = "continew-starter.captcha.behavior.cache-type", havingValue = "redis")
@ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "redis")
static class Redis {
static {
CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name().toLowerCase(), new BehaviorCaptchaCacheServiceImpl());
@@ -60,7 +61,7 @@ abstract class BehaviorCaptchaCacheConfiguration {
/**
* 自定义缓存实现类-自定义
*/
@ConditionalOnProperty(name = "continew-starter.captcha.behavior.cache-type", havingValue = "custom")
@ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "custom")
static class Custom {
@Bean

View File

@@ -21,6 +21,7 @@ import com.anji.captcha.service.CaptchaCacheService;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.charles7c.continew.starter.captcha.behavior.enums.StorageType;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import java.awt.*;
@@ -31,7 +32,7 @@ import java.awt.*;
* @since 1.1.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.captcha.behavior")
@ConfigurationProperties(PropertiesConstants.CAPTCHA_BEHAVIOR)
public class BehaviorCaptchaProperties {
/**

View File

@@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
/**
* 图形验证码自动配置
@@ -31,7 +32,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(GraphicCaptchaProperties.class)
@ConditionalOnProperty(prefix = "continew-starter.captcha.graphic", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.CAPTCHA_GRAPHIC, name = PropertiesConstants.ENABLED, havingValue = "true")
public class GraphicCaptchaAutoConfiguration {
@PostConstruct

View File

@@ -22,6 +22,7 @@ import com.wf.captcha.base.Captcha;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.charles7c.continew.starter.captcha.graphic.enums.GraphicCaptchaType;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import java.awt.*;
@@ -32,7 +33,7 @@ import java.awt.*;
* @since 1.0.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.captcha.graphic")
@ConfigurationProperties(PropertiesConstants.CAPTCHA_GRAPHIC)
public class GraphicCaptchaProperties {
/**

View File

@@ -27,6 +27,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.constant.StringConstants;
/**
@@ -39,7 +40,7 @@ import top.charles7c.continew.starter.core.constant.StringConstants;
@Lazy
@AutoConfiguration
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = "continew-starter.cors", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.CORS, name = PropertiesConstants.ENABLED, havingValue = "true")
@EnableConfigurationProperties(CorsProperties.class)
public class CorsAutoConfiguration {

View File

@@ -18,6 +18,7 @@ package top.charles7c.continew.starter.core.autoconfigure.cors;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.constant.StringConstants;
import java.util.ArrayList;
@@ -31,7 +32,7 @@ import java.util.List;
* @since 1.0.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.cors")
@ConfigurationProperties(PropertiesConstants.CORS)
public class CorsProperties {
/**

View File

@@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.exception.BaseException;
import java.util.Arrays;
@@ -42,7 +43,7 @@ import java.util.concurrent.ScheduledExecutorService;
@Lazy
@AutoConfiguration
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "continew-starter.thread-pool", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.THREAD_POOL, name = PropertiesConstants.ENABLED, havingValue = "true")
@EnableAsync(proxyTargetClass = true)
public class AsyncAutoConfiguration implements AsyncConfigurer {

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import java.util.concurrent.ScheduledExecutorService;
@@ -42,7 +43,7 @@ import java.util.concurrent.ThreadPoolExecutor;
@Slf4j
@Lazy
@AutoConfiguration
@ConditionalOnProperty(prefix = "continew-starter.thread-pool", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.THREAD_POOL, name = PropertiesConstants.ENABLED, havingValue = "true")
@EnableConfigurationProperties(ThreadPoolProperties.class)
public class ThreadPoolAutoConfiguration {

View File

@@ -18,6 +18,7 @@ package top.charles7c.continew.starter.core.autoconfigure.threadpool;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
/**
* 线程池配置属性
@@ -27,7 +28,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @since 1.0.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.thread-pool")
@ConfigurationProperties(PropertiesConstants.THREAD_POOL)
public class ThreadPoolProperties {
/**

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.core.constant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* 配置属性相关常量
*
* @author Charles7c
* @since 1.1.1
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PropertiesConstants {
/**
* ContiNew Starter
*/
public static final String CONTINEW_STARTER = "continew-starter";
/**
* 启用配置
*/
public static final String ENABLED = "enabled";
/**
* 跨域配置
*/
public static final String CORS = CONTINEW_STARTER + ".cors";
/**
* 线程池配置
*/
public static final String THREAD_POOL = CONTINEW_STARTER + ".thread-pool";
/**
* 日志配置
*/
public static final String LOG = CONTINEW_STARTER + ".log";
/**
* 存储配置
*/
public static final String STORAGE = CONTINEW_STARTER + ".storage";
/**
* 本地存储配置
*/
public static final String STORAGE_LOCAL = STORAGE + ".local";
/**
* 验证码配置
*/
public static final String CAPTCHA = CONTINEW_STARTER + ".captcha";
/**
* 图形验证码配置
*/
public static final String CAPTCHA_GRAPHIC = CAPTCHA + ".graphic";
/**
* 行为验证码配置
*/
public static final String CAPTCHA_BEHAVIOR = CAPTCHA + ".behavior";
}

View File

@@ -17,6 +17,7 @@
package top.charles7c.continew.starter.log.httptracepro.autoconfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import java.lang.annotation.*;
@@ -29,5 +30,5 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@ConditionalOnProperty(prefix = "continew-starter.log", name = "enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.LOG, name = PropertiesConstants.ENABLED, havingValue = "true")
public @interface ConditionalOnEnabledLog {}

View File

@@ -18,6 +18,7 @@ package top.charles7c.continew.starter.log.httptracepro.autoconfigure;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.log.common.enums.Include;
import java.util.HashSet;
@@ -30,7 +31,7 @@ import java.util.Set;
* @since 1.1.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.log")
@ConfigurationProperties(PropertiesConstants.LOG)
public class LogProperties {
/**

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.constant.StringConstants;
import java.util.Map;
@@ -42,7 +43,7 @@ import java.util.Map;
@AutoConfiguration
@RequiredArgsConstructor
@EnableConfigurationProperties(LocalStorageProperties.class)
@ConditionalOnProperty(name = "continew-starter.storage.local.enabled", havingValue = "true")
@ConditionalOnProperty(prefix = PropertiesConstants.STORAGE_LOCAL, name = PropertiesConstants.ENABLED, havingValue = "true")
public class LocalStorageAutoConfiguration implements WebMvcConfigurer {
private final LocalStorageProperties properties;

View File

@@ -19,6 +19,7 @@ package top.charles7c.continew.starter.storage.local.autoconfigure;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.unit.DataSize;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import java.util.HashMap;
import java.util.Map;
@@ -30,7 +31,7 @@ import java.util.Map;
* @since 1.1.0
*/
@Data
@ConfigurationProperties(prefix = "continew-starter.storage.local")
@ConfigurationProperties(PropertiesConstants.STORAGE_LOCAL)
public class LocalStorageProperties {
/**