gpt4 book ai didi

java - Spring AOP 可以实现通用返回类型

转载 作者:行者123 更新时间:2023-12-02 12:46:05 25 4
gpt4 key购买 nike

我尝试使用通用注释创建方面,例如

@Aspect
@Component
public class CommonAspect<T extends CommonEntity>{


@AfterReturning(value = "@annotation(audit)",returning="retVal")
public void save(JoinPoint jp,T retVal, Audit audit) {

Audit audit = new Audit();
audit.setMessage(retVal.getAuditMessage());
//other code to store audit

}


}

这可能吗?在我的例子中它失败了。我想将此 @Audit 注释用于人员、用户等不同类型的实体。所以返回值可以是通用的。

最佳答案

您似乎正在尝试为返回CommonEntity的方法定义一个方面。在这种情况下,您不需要使用泛型,您只需删除泛型声明并稍微调整方面声明即可:

@Aspect
@Component
public class CommonAspect {


@AfterReturning(value = "@annotation(audit) && execution(CommonEntity *(..))",returning="retVal")
public void save(JoinPoint jp, CommonEntity retVal, Audit audit) {

Audit auditInfo = new Audit();
auditInfo.setMessage(retVal.getAuditMessage());
//other code to store audit

}


}

我所做的是替换参数列表中的 T 并将 execution(CommonEntity *(..)) 添加到切入点表达式以将匹配限制为返回 CommonEntity 的切入点。

关于java - Spring AOP 可以实现通用返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com