1.首先明确Starter功能和目标(明确Starter要提供什么功能)
2.创建Maven或Gradle项目(这个项目会是一个库,不是可执行的应用项目)
Maven示例(pom.xml)在父类中引入需要的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smart</groupId>
<artifactId>smart_root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>pom</packaging>
<!-- 引入项目的子类 -->
<modules>
<module>pagex_spring_boot_starter</module>
</modules>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- 添加需要的依赖,如Spring Boot的autoconfigure和configuration-processor等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- 其他业务依赖 -->
</dependencies>
<!-- 打包配置(如果需要) -->
<build>
<!-- 插件配置等 -->
</build>
</project>
3.编写自动类:
创建一个或多个自动配置类,使用@configuration注解和Spring Boot的条件注解
注:如果Starter需要支持通过application.properties或application.yml文件配置,则需要编写配置属性类,并使用@ConfigurationProperties注解
import com.smart.aop.PageXAop;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author lenovo
* @version 0.1
* @className PageXAutoConfiguration
* @date 2024/7/1 21:51
* @since jdk11
*/
@Configuration
@Import(LogTimeXAop.class)
// 配置属性类
@ConditionalOnProperty(prefix = "com.smart.LogTimeX", name = "enable", havingValue = "true", matchIfMissing = true)
public class LogTimeXConfiguration {
}
4、在spring.factories中注册自动配置类:
在src/main/resources/META-INF目录下创建spring.factories文件,并添加自动配置类的全路径名
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.smart.autoconfiguration.PageXAutoConfiguration
5、 编写文档或示例,以便其他开发者通过注解使用
例如:模糊查询的时间戳
package com.smart.aop;
import cn.hutool.core.date.StopWatch;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* @author lenovo
* @version 0.1
* @className LogTimeXAop
* @date 2024/7/2 15:52
* @since jdk11
*/
@Component
@Aspect
@Slf4j
public class LogTimeXAop {
//定义切入点
@Pointcut("@annotation(com.smart.annotation.LogTimeX)")
public void point(){}
@Around("point()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object result = pjp.proceed();
stopWatch.stop();
log.info("查询耗时:{}",stopWatch.getTotalTimeMillis());
return result;
}
}
6.打包发布,推送到Maven中央仓库或私有仓库
7.其他项目使用Starter
在 pom.xml 引入相应的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.smart</groupId>
<artifactId>smart_root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.by</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.smart</groupId>
<artifactId>timex_spring_boot_starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建controller包
package com.by.controller;
import com.by.entity.CategoryQuery;
import com.by.service.CategoryService;
import com.smart.annotation.LogTimeX;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author lenovo
* @version 0.1
* @className ProductController
* @date 2024/7/1 17:14
* @since jdk11
*/
@RestController
@RequestMapping("/api/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 模糊查询
*/
@GetMapping
@LogTimeX //在dao层加入想要实现方法的注解
//万能接口
public List<CategoryQuery> select(CategoryQuery query){
// StopWatch stopWatch = new StopWatch();
// stopWatch.start();
List<CategoryQuery> list = categoryService.select(query);
// stopWatch.stop();
// log.debug("查询耗时:{}",stopWatch.getTotalTimeMillis());
return list;
}
}