springboot⾃定义注解作⽤在类上demo
最近⼀直在研究使⽤过滤器、、aop
然后写了⼀个aop,⾃定义注解,注解作⽤在⽅法上可以实现逻辑,结果放在类上就不可以,都没有进⼊切⾯了,最后在仔细学习发现问题,话不多说上代码
pom//这⾥包有点多,其实好多都可以不要,偷懒了
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3.1</version>
</dependency>
<!-- mvnrepository/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.5.1</version>
</dependency>
<!-- mvnrepository/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
</dependency>
<!-- mvnrepository/artifact/com.alibaba/fastjson --> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
⾸先⾃定义注解
;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/
/METHOD--的就是这个注解可以作⽤在⽅法上
//TYPE--的就是这个注解可以作⽤在⽅法上
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Zidingyizhujie {
String lanjie()default"";//这个根据你⾃⼰的逻辑写
}
上切⾯类,这⾥我写的是⽰例是后置通知,就是接⼝执⾏完
;
import com.alibaba.fastjson.JSON;
ample.guolvqiaop.utils.IpUtils;
log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
springboot aopimport org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.flect.MethodSignature;
import org.springframework.stereotype.Component;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import flect.Method;
/
**
* @Auther: 叶川江
* @Date: 2021/9/22 18:56
* @Description:
*/
@Aspect//表⽰切⾯类
@Component
@Log4j2
public class SystemLogAspect {
/***
* 注解作⽤在类上要⽤ @within
* 注解作⽤在⽅法上要⽤ @annotation
* @param joinPoint
* @param o
*/
@AfterReturning(returning ="o",pointcut ="@Zidingyizhujie)")
public void prinl(JoinPoint joinPoint,Object o){
//获取ip,IpUtils这是获取ip的⼯具类,⾃⼰在⽹上百度,或者看我以前作品都有
ServletRequestAttributes attributes =(ServletRequestAttributes) RequestAttributes();        HttpServletRequest request = Request();
System.out.println("ip:  "+IP(request));
//获取具体的⽅法名
MethodSignature signature =(MethodSignature) Signature();
String name1 = Method().getName();
System.out.println("⽅法名--"+name1);
// 获取url
String requestURI = RequestURI();
System.out.println("url---"+requestURI);
// 这也是获取返回值的⼀种⽅式
System.out.println("----接⼝的返回值----"+o);
// 因为作⽤在类上,这个获取的类
String name = Target().getClass().getName();
System.out.println(" 类的相对路径:      "+name);
/
/ 这也是获取值返回值的⼀种⽅式
Object[] args = Args();
if(args.length !=0){
String s = JSONString(args[0]);
System.out.println("-----------接⼝的返回值---------------"+s);
}
}
}
最后最好在配置⽂件yaml加上配置,使⽤类带理
spring:
aop:
auto: true
问题的答案在切⾯的代码⾥⾯
希望看到这⾥的各位同仁
都可以在这条道路越⾛越好,有时间多多学习,吃不了学习的苦就得吃⽣活的苦。
==============================================
红⾐少年 欲承先志 涉嫌通关 长梦未央!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。