现在很多开源的组件都会提供对应的
springboot-starter
包给我们去用,要做一个starter
包并不难。参照Spring内置的实现就好了:
1、在工程里引入 starter 打包相关的依赖。
2、在我们工程内建 spring.factories 文件,编写我们配置类的全限类名。
使用AOP实现拦截方法执行和打印日志的功能
名称 zhinian-log-spring-boot-starter
, 版本号是 1.0-SNAPSHOT
。且同时引入下方依赖。
<?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.zn.opit.base</groupId>
<artifactId>zhinian-log-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.7.13</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.7.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.zn.opit.base.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "zn.log")
public class ZhiNianLogProperties {
private boolean enabled;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
如果我们需要从 application.yaml 或 application.properties 中拿到一些使用者配置的数据,那么我们就需要定义一个properties类。这个properties类主要作用是将 application.yaml 或 application.properties 中的配置信息映射成实体类,比如我们这里指定 prefix = “zn.log” 这样,我们就能将以zn.log为前缀的配置项拿到了。
AOP切面逻辑
package com.zn.opit.base.component;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class WebLogAspect {
@Pointcut("execution(* *..*Controller.*(..))")
public void webLog(){}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
//方法开始前,打印开始日志
System.out.println("zn-starter --------doBefore");
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,打印结束日志
System.out.println("zn-starter --------doAfterReturning");
}
}
package com.zn.opit.base.config;
import com.zn.opit.base.component.WebLogAspect;
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;
@Configuration // 表示该类是一个配置类
@EnableConfigurationProperties({ZhiNianLogProperties.class}) // 该注解的作用是为 xxxProperties 开启属性配置功能,并将这个类以组件的形式注入到容器中
@ConditionalOnProperty(prefix = "zn.log", value = "enabled") // 当指定的配置项等于你想要的时候,配置类生效
public class ZnLogAutoConfig {
@Bean // @Bean:该注解用于将方法的返回值以 Bean 对象的形式添加到容器中
@ConditionalOnMissingBean // @ConditionalOnMissingBean(xxx.class):该注解表示当容器中没有 xxx 类时,该方法才生效
public WebLogAspect webLogAspect() {
return new WebLogAspect();
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zn.opit.base.config.ZnLogAutoConfig
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
在项目根目录下执行mvn clean install,打包并放到maven仓库中(maven仓库地址和配置有关)
显示build success,表示starter制品包打包完成
<?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.zn.opit.base</groupId>
<artifactId>zhinian-log-starter-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.13</version>
</dependency>
<dependency>
<groupId>com.zn.opit.base</groupId>
<artifactId>zhinian-log-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
(要符合starter里的aop拦截表达式规则,否则拦截不到)
package com.zn.opit.base.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/hello")
@RestController
public class HelloController {
@GetMapping("/test")
public String test() {
return "SUCCESS";
}
}
zn.log.enabled=true
server.port=8077
启动业务项目,访问接口,验证
至此,一个简单的starter示例就写完了。