my code
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NzLog {
String value() default "";
}
package com.rednuo.udm.ann;
import com.atlassian.plugin.spring.scanner.annotation.component.JiraComponent;
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.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
/**
* desc:
*
* @author
* @date 2025/2/14
* project: nz_jira_udm
*/
@Aspect
@JiraComponent
public class NzLogAspect {
public NzLogAspect() {
System.out.println("xxxx");
}
@Pointcut("@annotation(com.rednuo.udm.ann.NzLog)")
public void pointCut() {}
@Before("@annotation(com.rednuo.udm.ann.NzLog)")
public void logBefore(JoinPoint joinPoint) throws Exception {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
NzLog logAnnotation = method.getAnnotation(NzLog.class);
if(logAnnotation != null){
String logMessage = logAnnotation.value();
System.out.println("Logging before method execution: " + logMessage);
// 在此处添加代码,将日志信息保存到数据库
// saveToDatabase(logMessage, joinPoint.getArgs());
}
}
// 如果需要在方法成功返回后也记录一些信息,可以使用@AfterReturning
@AfterReturning(value = "pointCut()", returning = "returnObj")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("Method executed successfully. Result: " + result);
}
}
The code below is installed on the system normally, but it does not take effect and does not report any errors
Hi @lei
It has been so long but I remember I managed to use AOP in a Jira plugin by the help of this article.
It's a quite old article but it may shed a light, hopefully.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.