自学内容网 自学内容网

sunrays-framework 微调

1.common-log4j2-starter 动态获取并打印日志存储的根目录的绝对路径以及应用的访问地址

1.目录

CleanShot 2024-11-16 at 16.30.44@2x

2.log4j2.xml 配置LOG_HOME
        <!-- 日志存储的根目录 -->
        <Property name="LOG_HOME">./logs</Property>
3.LogHomePrinter.java 配置监听器
package com.sunxiansheng.log4j2.listener;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

import java.io.File;
import java.net.InetAddress;

/**
 * 动态获取并打印日志存储的根目录的绝对路径以及应用的访问地址
 *
 * @author sunxiansheng
 */
public class LogHomePrinter implements ApplicationListener<ApplicationReadyEvent> {

    private static final Logger logger = LoggerFactory.getLogger(LogHomePrinter.class);

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        try {
            // 获取 LoggerContext
            LoggerContext context = (LoggerContext) LogManager.getContext(false);
            // 获取 Log4j2 的当前配置
            Configuration config = context.getConfiguration();

            // 获取 LOG_HOME 属性值
            String logHome = config.getStrSubstitutor().replace("${LOG_HOME}");
            if (logHome == null || logHome.isEmpty()) {
                logger.warn("未配置 LOG_HOME 属性,使用默认值或未定义!");
            } else {
                // 获取 LOG_HOME 的绝对路径
                File logHomeDir = new File(logHome).getCanonicalFile();
                logger.info("日志存储的根目录为: {}", logHomeDir.getAbsolutePath());
            }

            // 获取 Spring 环境
            Environment environment = event.getApplicationContext().getEnvironment();

            // 获取访问地址
            String ip = InetAddress.getLocalHost().getHostAddress();
            String port = environment.getProperty("server.port", "8080");
            String contextPath = environment.getProperty("server.servlet.context-path", "");
            if (!contextPath.startsWith("/")) {
                contextPath = "/" + contextPath;
            }

            // 打印访问路径
            logger.info("应用访问地址: http://{}:{}{}", ip, port, contextPath);

        } catch (Exception e) {
            logger.error("获取日志存储根目录或应用访问地址时发生错误", e);
        }
    }
}
4.spring.factories 注册监听器
org.springframework.context.ApplicationListener=\
com.sunxiansheng.log4j2.listener.LogHomePrinter
5.测试
1.common-log4j2-starter-demo 配置
log:
  aspect:
    enable: true # 开启自定义的日志切面
  trace:
    enable: true # 开启自定义的日志追踪
server:
  port: 8082
2.启动测试

CleanShot 2024-11-16 at 16.33.29@2x

CleanShot 2024-11-16 at 16.33.50@2x

2.common-minio-starter 删除桶名的配置参数

1.MinioProperties.java

CleanShot 2024-11-16 at 15.17.13@2x

2.删除

CleanShot 2024-11-16 at 15.19.22@2x

3.common-mybatis-plus-starter 修改代码生成器

1.修改ServiceImpl的Java模板,调用框架提供的方法使用super而不是this
## 引入宏定义
$!{define.vm}

## 设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "ServiceImpl.java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

## 拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

## 包声明
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.service.impl;
#end

## 导入相关类
import com.sunxiansheng.mybatis.plus.base.service.impl.SunRaysBaseServiceImpl;
import $!{tableInfo.savePackageName}.convert.${tableInfo.name}Converter;
import $!{tableInfo.savePackageName}.entity.po.${tableInfo.name};
import $!{tableInfo.savePackageName}.entity.req.${tableInfo.name}Req;
import $!{tableInfo.savePackageName}.entity.vo.${tableInfo.name}Vo;
import $!{tableInfo.savePackageName}.mapper.${tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.mapper.plus.${tableInfo.name}PlusMapper;
import com.sunxiansheng.mybatis.plus.page.PageResult;
import com.sunxiansheng.mybatis.plus.page.SunPageHelper;
import $!{tableInfo.savePackageName}.service.${tableInfo.name}Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})Service实现类:实现业务逻辑
 * 注意:使用this来调用框架自动生成的方法,使用注入的xxxMapper来调用手写的方法
 *
 * @author $!{author}
 * @since $!time.currTime()
 */
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableInfo.name}ServiceImpl
        extends SunRaysBaseServiceImpl<$!{tableInfo.name}PlusMapper, $!{tableInfo.name}, $pk.type>
        implements $!{tableInfo.name}Service {

    // ============================== 注入Mapper ==============================
    @Resource
    private $!{tableInfo.name}Mapper ${tool.firstLowerCase($!{tableInfo.name})}Mapper;

    // ============================== 框架生成(auto-generation) ==============================
    /**
     * 构造器注入父类的MyBatis-Plus Mapper
     *
     * @param mybatisPlusMapper 父类需要的Plus Mapper
     */
    public $!{tableInfo.name}ServiceImpl($!{tableInfo.name}PlusMapper mybatisPlusMapper) {
        super.setMybatisPlusMapper(mybatisPlusMapper);
    }
    // ============================== 框架生成(auto-generation) ==============================

    // ============================== 自定义Service实现 ==============================
    @Override
    @Transactional(rollbackFor = Exception.class) // 开启事务
    public PageResult<$!{tableInfo.name}Vo> listPage($!{tableInfo.name}Req $!tool.firstLowerCase($!{tableInfo.name})Req) {
        // req转换为po
        $!{tableInfo.name} po = $!{tableInfo.name}Converter.INSTANCE.convertReq2Po($!tool.firstLowerCase($!{tableInfo.name})Req);

        // 分页查询
        PageResult<$!{tableInfo.name}Vo> paginate = SunPageHelper.paginate(
            $!tool.firstLowerCase($!{tableInfo.name})Req.getPageNo(),
            $!tool.firstLowerCase($!{tableInfo.name})Req.getPageSize(),
            // 调用框架自动生成的方法使用super.xxx !!!
            () -> super.count(po),
            (offset, limit) -> {
                // 调用自定义的方法使用注入的xxxMapper.xxx !!!
                // 查询数据,并转换为vo
                List<$!{tableInfo.name}> ${tool.firstLowerCase($!{tableInfo.name})}List =
                    ${tool.firstLowerCase($!{tableInfo.name})}Mapper.queryPage(po, offset, limit);
                return $!{tableInfo.name}Converter.INSTANCE.convertPoList2VoList(${tool.firstLowerCase($!{tableInfo.name})}List);
            }
        );
        return paginate;
    }
}
2.导出模板

CleanShot 2024-11-16 at 16.53.58@2x

4.common-test-starter 排除logging

CleanShot 2024-11-16 at 16.56.57@2x


原文地址:https://blog.csdn.net/m0_64637029/article/details/145216912

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!