mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor: 封装公共配置,降低使用复杂度
This commit is contained in:
@@ -26,7 +26,10 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import top.charles7c.continew.starter.core.autoconfigure.ProjectProperties;
|
||||
import top.charles7c.continew.starter.core.handler.GeneralPropertySourceFactory;
|
||||
|
||||
|
||||
/**
|
||||
* API 文档自动配置
|
||||
@@ -37,6 +40,7 @@ import top.charles7c.continew.starter.core.autoconfigure.ProjectProperties;
|
||||
@Slf4j
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true")
|
||||
@PropertySource(value = "classpath:default-api-doc.yml", factory = GeneralPropertySourceFactory.class)
|
||||
public class SpringDocAutoConfiguration {
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,16 @@
|
||||
--- ### 接口文档配置
|
||||
springdoc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
tags-sorter: alpha
|
||||
operations-sorter: alpha
|
||||
show-extensions: true
|
||||
api-docs:
|
||||
enabled: ${springdoc.swagger-ui.enabled}
|
||||
path: /v3/api-docs
|
||||
## 接口文档增强配置
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
language: zh_cn
|
||||
swagger-model-name: 实体类列表
|
@@ -31,8 +31,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import top.charles7c.continew.starter.core.handler.GeneralPropertySourceFactory;
|
||||
|
||||
/**
|
||||
* Sa-Token 自动配置
|
||||
@@ -45,6 +47,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@RequiredArgsConstructor
|
||||
@EnableConfigurationProperties(SaTokenExtensionProperties.class)
|
||||
@ConditionalOnProperty(prefix = "sa-token.extension", name = "enabled", havingValue = "true")
|
||||
@PropertySource(value = "classpath:default-auth-satoken.yml", factory = GeneralPropertySourceFactory.class)
|
||||
public class SaTokenAutoConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private final SaTokenExtensionProperties properties;
|
||||
|
@@ -0,0 +1,10 @@
|
||||
--- ### Sa-Token 配置(https://sa-token.cc/doc.html#/use/config)
|
||||
sa-token:
|
||||
# token 前缀(例如填写 Bearer,实际传参 token 键: Bearer xxxx-xxxx-xxxx-xxxx)
|
||||
token-prefix: Bearer
|
||||
# 是否尝试从 请求体 里读取 Token
|
||||
is-read-body: true
|
||||
# 是否尝试从 header 里读取 Token
|
||||
is-read-header: true
|
||||
# 是否尝试从 cookie 里读取 Token(此值为 false 后,StpUtil.login(id) 登录时也不会再往前端注入 Cookie,适合前后端分离模式)
|
||||
is-read-cookie: false
|
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.handler;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.boot.env.YamlPropertySourceLoader;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.DefaultPropertySourceFactory;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 通用配置文件读取工厂(DefaultPropertySourceFactory 仅支持 properties 配置文件读取,详见:<a href="https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings">YAML Shortcomings</a>)
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class GeneralPropertySourceFactory extends DefaultPropertySourceFactory {
|
||||
|
||||
@Override
|
||||
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException {
|
||||
Resource resource = encodedResource.getResource();
|
||||
String resourceName = resource.getFilename();
|
||||
if (StrUtil.isNotBlank(resourceName) && StrUtil.endWithAny(resourceName, ".yml", ".yaml")) {
|
||||
return new YamlPropertySourceLoader().load(resourceName, resource).get(0);
|
||||
}
|
||||
return super.createPropertySource(name, encodedResource);
|
||||
}
|
||||
}
|
@@ -33,7 +33,9 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import top.charles7c.continew.starter.core.handler.GeneralPropertySourceFactory;
|
||||
|
||||
/**
|
||||
* MyBatis Plus 自动配置
|
||||
@@ -47,6 +49,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
@EnableTransactionManagement(proxyTargetClass = true)
|
||||
@EnableConfigurationProperties(MyBatisPlusExtensionProperties.class)
|
||||
@ConditionalOnProperty(prefix = "mybatis-plus.extension", name = "enabled", havingValue = "true")
|
||||
@PropertySource(value = "classpath:default-data-mybatis-plus.yml", factory = GeneralPropertySourceFactory.class)
|
||||
public class MybatisPlusAutoConfiguration {
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,17 @@
|
||||
--- ### MyBatis Plus 配置(https://baomidou.com/pages/56bac0/#%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE)
|
||||
mybatis-plus:
|
||||
# 启动时是否检查 MyBatis XML 文件的存在(默认:false 不检查)
|
||||
check-config-location: true
|
||||
## MyBatis 原生支持配置
|
||||
configuration:
|
||||
# 是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名)到经典 Java 属性名 aColumn(驼峰命名)的类似映射
|
||||
# 此属性在 MyBatis 中原默认值为 false,在 MyBatis-Plus 中,此属性也将用于生成最终的 SQL 的 select body,如果您的数据库命名符合规则无需使用 @TableField 注解指定数据库字段名
|
||||
map-underscore-to-camel-case: true
|
||||
# MyBatis 自动映射时未知列或未知属性处理策略,通过该配置可指定 MyBatis 在自动映射过程中遇到未知列或者未知属性时如何处理
|
||||
# NONE:不做任何处理 (默认值);WARNING:以日志的形式打印相关警告信息;FAILING:当作映射失败处理,并抛出异常和详细信息
|
||||
auto-mapping-unknown-column-behavior: NONE
|
||||
# 日志配置
|
||||
# 默认:org.apache.ibatis.logging.slf4j.Slf4jImpl
|
||||
# 更详细(会有性能损耗):org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
# 关闭(可单纯使用 p6spy 分析):org.apache.ibatis.logging.nologging.NoLoggingImpl
|
||||
log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
|
@@ -28,6 +28,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import top.charles7c.continew.starter.core.handler.GeneralPropertySourceFactory;
|
||||
import top.charles7c.continew.starter.json.jackson.serializer.BigNumberSerializer;
|
||||
|
||||
import java.math.BigInteger;
|
||||
@@ -45,6 +47,7 @@ import java.util.TimeZone;
|
||||
*/
|
||||
@Slf4j
|
||||
@AutoConfiguration
|
||||
@PropertySource(value = "classpath:default-json-jackson.yml", factory = GeneralPropertySourceFactory.class)
|
||||
public class JacksonAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@@ -0,0 +1,21 @@
|
||||
--- ### Spring 配置
|
||||
spring:
|
||||
## MVC 配置
|
||||
mvc:
|
||||
format:
|
||||
# 日期格式化(针对 java.util.Date)
|
||||
date-time: yyyy-MM-dd HH:mm:ss
|
||||
## Jackson 配置
|
||||
jackson:
|
||||
# 时区配置
|
||||
time-zone: GMT+8
|
||||
# 日期格式化(针对 java.util.Date)
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
# 序列化配置(Bean -> JSON)
|
||||
serialization:
|
||||
# 允许序列化无属性的 Bean
|
||||
FAIL_ON_EMPTY_BEANS: false
|
||||
# 反序列化配置(JSON -> Bean)
|
||||
deserialization:
|
||||
# 允许反序列化不存在的属性
|
||||
FAIL_ON_UNKNOWN_PROPERTIES: false
|
Reference in New Issue
Block a user