本文最后更新于 2025-02-03,文章内容可能已经过时。

1、新建正常SpringBoot项目并引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nn3n.develop</groupId>
    <artifactId>nn3n-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- 使属性配置出现自动提示 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

2、编写starter 的属性配置类

package com.nn3n.develop.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties("log")
public class LogProperties {
    private Boolean enable= false; //是否启用该starter
    private Boolean classFullName= true; //日志展示全类名
    private Boolean showUseTime= true; //日志展示方法执行时间
}
log:
  class-full-name: true
  show-use-time: true
  enable: true

3、编写starter业务实现

比如

package com.nn3n.develop.annotation;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import java.lang.annotation.ElementType;

@Target(ElementType.METHOD) //该注解只能用于方法上
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Log {


}
package com.nn3n.develop.aspect;

import com.nn3n.develop.config.LogProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

@Slf4j
@Aspect
@ConditionalOnProperty(name = "log.enable", havingValue = "true", matchIfMissing = false)
public class LogAspect {

    @Resource
    private LogProperties logProperties;

    @Around("@annotation(com.nn3n.develop.annotation.Log)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long beginTime = System.currentTimeMillis();
        Class targetClass = joinPoint.getSignature().getDeclaringType();
        String functionName = joinPoint.getSignature().getName();
        String name = (logProperties.getClassFullName() ? targetClass.getName() : targetClass.getSimpleName()) + "#" + functionName;
        try {
            log.info("{}开始", name);
            Object res = joinPoint.proceed(joinPoint.getArgs());
            log.info("{}结束", name);
            return res;
        } catch (Throwable t) {
            log.info("{}异常," + t.getMessage(), name);
            throw t;
        } finally {
            if (logProperties.getShowUseTime()) {
                log.info("{}执行时间:{}ms", name, System.currentTimeMillis() - beginTime);
            }
        }
    }
}

4、编写starter自动配置类

package com.nn3n.develop.autoconfiguration;

import com.nn3n.develop.aspect.LogAspect;
import com.nn3n.develop.config.LogProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
//引入Properties类
@EnableConfigurationProperties(LogProperties.class)
//引入LogAspect
@Import(LogAspect.class)
public class LogAutoConfiguration {
    
}

5、配置starter自动创建对象并注入ioc容器中

编写META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件,引入自动配置类
com.nn3n.develop.autoconfiguration.LogAutoConfiguration

6、打包发布至仓库、并在欲使用的项目中引入

<dependency>
    <groupId>com.nn3n.develop</groupId>
    <artifactId>nn3n-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

并在项目中根据starter的属性来按需配置

log:
  class-full-name: true
  show-use-time: true
  enable: true