sunrays-framework配置重构

ops/2025/1/31 20:58:41/

文章目录

    • 1.common-log4j2-starter
        • 1.目录结构
        • 2.Log4j2Properties.java 新增两个属性
        • 3.Log4j2AutoConfiguration.java 条件注入LogAspect
        • 4.ApplicationEnvironmentPreparedListener.java 从Log4j2Properties.java中定义的配置读取信息
    • 2.common-minio-starter
        • 1.MinioProperties.java
    • 3.common-mybatis-plus-starter
        • 1.目录结构
        • 2.BaseEntity.java
        • 3.SunRaysBaseServiceImpl.java 删除掉事务注解
        • 4.MyBatisPlusProperties.java 统一配置
        • 5.MybatisPLusAutoConfiguration.java
        • 6.application.yml 提供通用配置
        • 7.pom.xml 排除logging
    • 4.common-openai-starter-demo
        • 1.目录结构
        • 2.OpenAiProperties.java

1.common-log4j2-starter

1.目录结构

CleanShot 2025-01-03 at 19.32.33@2x

java__8">2.Log4j2Properties.java 新增两个属性
java">package com.sunxiansheng.log4j2.config.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** Description: 日志切面配置** @Author sun* @Create 2024/10/24 23:16* @Version 1.0*/
@ConfigurationProperties(prefix = "sun-rays.log4j2")
@Data
public class Log4j2Properties {/*** 是否开启日志切面*/private boolean logAspectEnable = true;/*** 日志存储根目录*/private String home = "./logs";/*** 日志模块(从仓库中最顶级的模块开始) 例如:sunrays-framework/sunrays-demo/common-log4j2-starter-demo*/private String module = "defaultModule";
}
java_LogAspect_44">3.Log4j2AutoConfiguration.java 条件注入LogAspect
java">package com.sunxiansheng.log4j2.config;import com.sunxiansheng.log4j2.aspectj.LogAspect;
import com.sunxiansheng.log4j2.config.properties.Log4j2Properties;
import com.sunxiansheng.log4j2.trace.TraceIdFilter;
import lombok.extern.slf4j.Slf4j;
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.Configuration;import javax.annotation.PostConstruct;/*** Description: Log4j2自动配置类** @Author sun* @Create 2024/10/24 10:36* @Version 1.0*/
@Configuration
@EnableConfigurationProperties({Log4j2Properties.class}) // 启用配置类
@Slf4j
public class Log4j2AutoConfiguration {/*** 自动配置成功日志*/@PostConstructpublic void logConfigSuccess() {log.info("Log4j2AutoConfiguration has been loaded successfully!");}/*** 条件注入LogAspect** @return*/@Bean@ConditionalOnMissingBean@ConditionalOnProperty(name = "sun-rays.log4j2.log-aspect-enable", havingValue = "true", matchIfMissing = true)LogAspect logAspect() {log.info("LogAspect 成功注入!");return new LogAspect();}/*** 条件注入链路追踪过滤器** @return*/@Bean@ConditionalOnMissingBeanTraceIdFilter traceIdFilter() {return new TraceIdFilter();}
}
java_Log4j2Propertiesjava_107">4.ApplicationEnvironmentPreparedListener.java 从Log4j2Properties.java中定义的配置读取信息
java">package com.sunxiansheng.log4j2.listener;import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;/*** Description: 配置文件已加载,Environment 对象已初始化* 使用 ApplicationEnvironmentPreparedEvent 读取 application.yml 文件中的 sun-rays-log4j2.home 和 sun-rays-log4j2.module 配置** @Author sun* @Create 2024/12/13 23:53* @Version 1.0*/
public class ApplicationEnvironmentPreparedListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {@Overridepublic void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {ConfigurableEnvironment environment = event.getEnvironment();// 获取 log.home 属性并提供默认值String logHome = environment.getProperty("sun-rays.log4j2.home", "./logs");if ("./logs".equals(logHome)) {System.err.println("WARNING: sun-rays.log4j2.home 属性未设置,使用默认值: " + logHome);}// 获取 log.module 属性并提供默认值String logModule = environment.getProperty("sun-rays.log4j2.module", "defaultModule");if ("defaultModule".equals(logModule)) {System.err.println("WARNING: sun-rays.log4j2.module 属性未设置,使用默认值: " + logModule);}// 将属性设置为系统属性System.setProperty("log.home", logHome);System.setProperty("log.module", logModule);}/*** 当前监听器的启动顺序需要在日志配置监听器的前面,保证在日志文件初始化之前读取 application.yml 的配置。** @return*/@Overridepublic int getOrder() {return LoggingApplicationListener.DEFAULT_ORDER - 1;}
}

2.common-minio-starter

java_163">1.MinioProperties.java
java">package com.sunxiansheng.minio.config.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** Description: Minio配置** @Author sun* @Create 2024/11/12 23:15* @Version 1.0*/
@ConfigurationProperties(prefix = "sun-rays.minio")
@Data
public class MinioProperties {/*** minio服务地址*/private String endpoint;/*** minio服务用户名*/private String accessKey;/*** minio服务密码*/private String secretKey;
}

3.common-mybatis-plus-starter

1.目录结构

CleanShot 2025-01-04 at 17.28.52@2x

java_203">2.BaseEntity.java
java">package com.sunxiansheng.mybatis.plus.base.entity;import com.baomidou.mybatisplus.annotation.TableField;
import lombok.*;import java.io.Serializable;
import java.util.Date;/*** Description: Entity基类** @Author sun* @Create 2024/10/26 10:35* @Version 1.0*/
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class BaseEntity implements Serializable {private static final long serialVersionUID = 1L;/*** 创建者*/@TableField("create_by")  // 数据库中的字段名private String createBy;/*** 创建时间*/@TableField("create_time")  // 数据库中的字段名private Date createTime;/*** 更新者*/@TableField("update_by")  // 数据库中的字段名private String updateBy;/*** 更新时间*/@TableField("update_time")  // 数据库中的字段名private Date updateTime;
}
java__256">3.SunRaysBaseServiceImpl.java 删除掉事务注解
java">package com.sunxiansheng.mybatis.plus.base.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunxiansheng.mybatis.plus.base.service.SunRaysBaseService;import java.io.Serializable;
import java.util.List;/*** Description: SunRays-Framework 基础Service实现类** @Author sun* @Create 2024/10/25 17:38* @Version 1.0*/
public class SunRaysBaseServiceImpl<M extends BaseMapper<T>, T, ID extends Serializable>implements SunRaysBaseService<T, ID> {// Mapper字段,私有化处理,不允许子类直接访问,只能通过Setter注入private M mybatisPlusMapper;// Setter方法用于注入MyBatis-Plus Mapperpublic void setMybatisPlusMapper(M mybatisPlusMapper) {this.mybatisPlusMapper = mybatisPlusMapper;}// ============================== 基于事务的CRUD方法 ==============================@Overridepublic boolean existsById(ID id) {T t = mybatisPlusMapper.selectById(id);return t != null;}@Overridepublic boolean exists(T po) {QueryWrapper<T> queryWrapper = new QueryWrapper<>(po);return mybatisPlusMapper.exists(queryWrapper);}@Overridepublic Long count(T po) {QueryWrapper<T> queryWrapper = new QueryWrapper<>(po);return mybatisPlusMapper.selectCount(queryWrapper);}@Overridepublic T listById(ID id) {return mybatisPlusMapper.selectById(id);}@Overridepublic T listOne(T po) {return mybatisPlusMapper.selectOne(new QueryWrapper<>(po));}@Overridepublic List<T> listAll(T po) {QueryWrapper<T> queryWrapper = new QueryWrapper<>(po);return mybatisPlusMapper.selectList(queryWrapper);}@Overridepublic int insertOne(T po) {return mybatisPlusMapper.insert(po);}@Overridepublic int updateById(T po) {return mybatisPlusMapper.updateById(po);}@Overridepublic int update(T po, T condition) {QueryWrapper<T> queryWrapper = new QueryWrapper<>(condition);return mybatisPlusMapper.update(po, queryWrapper);}@Overridepublic int deleteById(ID id) {return mybatisPlusMapper.deleteById(id);}@Overridepublic int deleteBatchByIds(List<ID> ids) {return mybatisPlusMapper.deleteBatchIds(ids);}@Overridepublic int delete(T po) {QueryWrapper<T> queryWrapper = new QueryWrapper<>(po);return mybatisPlusMapper.delete(queryWrapper);}
}
java__356">4.MyBatisPlusProperties.java 统一配置
java">package com.sunxiansheng.mybatis.plus.config.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** Description: MyBatisPlus配置** @Author sun* @Create 2025/1/3 21:36* @Version 1.0*/
@ConfigurationProperties(prefix = "sun-rays.mybatis-plus")
@Data
public class MyBatisPlusProperties {/*** 是否启用sql美化*/private boolean sqlBeautyEnabled = true;
}
java_382">5.MybatisPLusAutoConfiguration.java
java">package com.sunxiansheng.mybatis.plus.config;import com.sunxiansheng.mybatis.plus.config.properties.MyBatisPlusProperties;
import com.sunxiansheng.mybatis.plus.interceptor.SqlBeautyInterceptor;
import lombok.extern.slf4j.Slf4j;
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.Configuration;import javax.annotation.PostConstruct;/*** Description: MybatisPLus自动配置类** @Author sun* @Create 2024/10/23 23:00* @Version 1.0*/
@Configuration
@EnableConfigurationProperties({MyBatisPlusProperties.class}) // 启用配置类
@Slf4j
public class MybatisPLusAutoConfiguration {/*** 自动配置成功日志*/@PostConstructpublic void logConfigSuccess() {log.info("MybatisPLusAutoConfiguration has been loaded successfully!");}/*** SQL美化拦截器*/@Bean@ConditionalOnMissingBean@ConditionalOnProperty(prefix = "sun-rays.mybatis-plus", value = "sql-beauty-enabled", havingValue = "true", matchIfMissing = true)public SqlBeautyInterceptor sqlBeautyInterceptor() {log.info("SqlBeautyInterceptor 成功注入!");return new SqlBeautyInterceptor();}
}
6.application.yml 提供通用配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource # druid连接池druid:initial-size: 10          # 初始化连接数(适当减少以节省资源)min-idle: 10              # 最小空闲连接数max-active: 50            # 最大连接数(根据业务需求调整)max-wait: 30000           # 获取连接的最大等待时间(30秒)timeBetweenEvictionRunsMillis: 60000  # 每60秒检查一次空闲连接minEvictableIdleTimeMillis: 300000    # 最小空闲时间5分钟后回收testWhileIdle: true       # 检查空闲连接是否可用testOnBorrow: true        # 检查从连接池获取的连接是否可用removeAbandoned: true     # 启用清理超时连接removeAbandonedTimeout: 180  # 连接超时时间180秒logAbandoned: true        # 记录被清理的连接日志
7.pom.xml 排除logging
<!-- jdbc -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><exclusions><exclusion><artifactId>spring-boot-starter-logging</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions>
</dependency>

4.common-openai-starter-demo

1.目录结构

CleanShot 2025-01-04 at 17.45.18@2x

java_474">2.OpenAiProperties.java
java">package com.sunxiansheng.openai.config.properties;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** Description: OpenAI配置属性类** @Author sun* @Create 2024/12/14 11:44* @Version 1.0*/
@Data
@ConfigurationProperties(prefix = "sun-rays.openai")
public class OpenAiProperties {/*** OpenAI API Key*/private String apiKey;/*** OpenAI API URL 有默认值*/private String apiUrl = "https://api.openai.com/v1/chat/completions";
}

http://www.ppmy.cn/ops/154573.html

相关文章

7层还是4层?网络模型又为什么要分层?

~犬&#x1f4f0;余~ “我欲贱而贵&#xff0c;愚而智&#xff0c;贫而富&#xff0c;可乎&#xff1f; 曰&#xff1a;其唯学乎” 一、为什么要分层 \quad 网络通信的复杂性促使我们需要一种分层的方法来理解和管理网络。就像建筑一样&#xff0c;我们不会把所有功能都混在一起…

基础项目实战——3D赛车(c++)

目录 前言一、渲染引擎二、关闭事件三、梯形绘制四、轨道绘制五、边缘绘制六、草坪绘制七、前后移动八、左右移动​九、曲线轨道​十、课山坡轨道​十一、循环轨道​十二、背景展示​十三、引入速度​十四、物品绘制​十五、课数字路障​十六、分数展示​十七、重新生成​十八、…

C++17 搜索器教程:解锁高效搜索新姿势

C17搜索器教程&#xff1a;解锁高效搜索新姿势 C17搜索器简介 在C的发展历程中&#xff0c;C17是一个重要的里程碑&#xff0c;它引入了诸多实用的新特性&#xff0c;搜索器功能便是其中之一。此功能着重对std::search算法进行了强化&#xff0c;使其支持多种搜索策略&#x…

关于opencv环境搭建问题:由于找不到opencv_worldXXX.dll,无法执行代码,重新安装程序可能会解决此问题

方法一&#xff1a;利用复制黏贴方法 打开opencv文件夹目录找到\opencv\build\x64\vc15\bin 复制该目录下所有文件&#xff0c;找到C:\Windows\System32文件夹&#xff08;注意一定是C盘&#xff09;黏贴至该文件夹重新打开VS。 方法二&#xff1a;直接配置环境 打开opencv文…

machine learning自定义数据集使用框架的线性回归方法对其进行拟合

使用框架&#xff08;如Scikit-learn&#xff09;对自定义数据集进行线性回归拟合是一个常见的任务。以下是一个详细的步骤指南&#xff0c;展示如何使用Scikit-learn库在Python中完成这一任务 import numpy as np from sklearn.model_selection import train_test_split fro…

搭建Spring Boot开发环境

JDK&#xff08;1.8及以上版本&#xff09; Apache Maven 3.6.0 修改settings.xml 设置本地仓库位置 <localRepository>D:/repository</localRepository> 设置远程仓库镜像 <mirror><id>alimaven</id><name>aliyun maven</name&…

JavaEE:多线程进阶

JavaEE&#xff1a;多线程进阶 一、对比不同锁策略之间的应用场景及其区别1. 悲观锁 和 乐观锁1.1 定义和原理1.2 应用场景1.3 示例代码 2. 重量级锁 和 轻量级锁2.1 定义和原理2.2 应用场景2.3 示例代码 3. 挂起等待锁 和 自旋锁3.1 定义和原理3.2 应用场景3.3 示例代码 4. 几…

c语言网 1127 尼科彻斯定理

原题 题目描述 验证尼科彻斯定理&#xff0c;即&#xff1a;任何一个整数m的立方都可以写成m个连续奇数之和。 输入格式 任一正整数 输出格式 该数的立方分解为一串连续奇数的和 样例输入 13 样例输出 13*13*132197157159161163165167169171173175177179181 ​ #include<ios…