I want use aop in jiraSDK

lei
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 14, 2025

 

 

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

 

 

1 answer

0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 14, 2025

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.

https://community.atlassian.com/t5/Jira-articles/Spring-Java-based-configuration-and-AOP-in-Jira-plugins/ba-p/762265

It's a quite old article but it may shed a light, hopefully.

Suggest an answer

Log in or Sign up to answer