手把手教你springboot自定义starter 实现AOP技术实现日志切面
)
# 前言
SpringBoot中的starter是一种非常重要的机制(自动化配置),能够抛弃以前繁杂的配置,将其统一集成进starter,
应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。
starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot会自动通过classpath路径下的类发现需要的Bean,
并注册进IOC容器。SpringBoot提供了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。
所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念
提示:以下是本篇文章正文内容,下面案例可供参考
一、为什么要自定义starter?
在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,
然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。
如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,
SpringBoot为我们完成自动装配,简直不要太爽
二、自定义starter开发流程
1.我们先创建一个springboot项目
1.1.命名规范
SpringBoot官方命名方式
格式:spring-boot-starter-{模块名}
举例:spring-boot-starter-web
自定义命名方式
格式:{模块名}-spring-boot-starter
举例:mystarter-spring-boot-starter
如图所示(示例):
这里我们从springboot依赖库中勾选出我们所需要的依赖
记住:spring-boot-configuration-processor 是必须要引入的依赖 不能为false 为false则表示传递依赖 会引起依赖重复 所以为true
2.编写我们的属性类
代码如下(示例):
package com.wang.demospringbootstarter.property;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author wang
* @date 2022/2/20 15:55
* @project demo-spring-boot-starter
* @company 小刘
*/
@ConfigurationProperties("demo.weblog")
public class WebLogProperty {
private Boolean enabled;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
}
2.1编写我们的属性类
代码如下(示例):
package com.wang.demospringbootstarter.config;
import com.wang.demospringbootstarter.property.WebLogProperty;
import com.wang.demospringbootstarter.weblog.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;
/**
* @author wang
* @date 2022/2/20 15:50
* @project demo-spring-boot-starter
* @company 小刘
*/
@Configuration //表示这个类是配置类
@EnableConfigurationProperties({WebLogProperty.class})
@ConditionalOnProperty(prefix = "demo.weblog",value = "enabled", matchIfMissing = true)
public class WebLogConfig {
@ConditionalOnMissingBean
@Bean
public WebLogAspect webLogAspect(){
return new WebLogAspect();
}
这里要注意:matchIfMissing属性:默认情况下matchIfMissing为false,也就是说如果未进行属性配置,则自动配置不生效。如果matchIfMissing为true,则表示如果没有对应的属性配置,则自动配置默认生效
2.2编写业务逻辑
package com.wang.demospringbootstarter.weblog;
import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Aspect
@Component
@Slf4j
public class WebLogAspect {
//@Pointcut("execution(public * com.zking..controller.*.*(..))")
@Pointcut("execution(* *..*Controller.*(..))")
public void webLog(){}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
log.info("开始服务:{}", request.getRequestURL().toString());
log.info("客户端IP :{}" , request.getRemoteAddr());
log.info("参数值 :{}", Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
log.info("返回值 : {}" , ret);
}
}
3.打包
找到以上命令进行打包
到这里就可以去自己本地的maven仓库里面找到自己所打好的依赖。
在需要的项目中引入依赖
4.测试
这样我们的aop实现日志切面就完成了
总结
以上就是我对starter的基本理解
有误的地方还望大神指点