使用 Spring 或 AspectJ 将基于代码的样式转换为基于注释的样式 AOP
Converting Code based style to Annotation Based style AOP using Spring or AspectJ
我有以下基于代码的样式方面,它在代码中查找字段级注释并调用以该字段作为参数的方法。这是它的样子..
public aspect EncryptFieldAspect
{
pointcut encryptStringMethod(Object o, String inString):
call(@Encrypt * *(String))
&& target(o)
&& args(inString)
&& !within(EncryptFieldAspect);
void around(Object o, String inString) : encryptStringMethod(o, inString) {
proceed(o, FakeEncrypt.Encrypt(inString));
return;
}
}
上述方法工作正常,但我想将其转换为基于 Spring 或 AspectJ 的注释,类似于此。发现 AspectJ 文档有点混乱任何提示都会有所帮助..
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MyAspect {
@Around("execution(public * *(..))")
public Object allMethods(final ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println("Before...");
try{
return thisJoinPoint.proceed();
}finally{
System.out.println("After...");
}
}
}
不确定您正在阅读哪些文档 - https://eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html 中的页面向您展示了如何将代码转换为注释样式。我承认它们并不像它们可能的那样全面。
基本上:
- 从 aspect 关键字切换到 @Aspect
- 将你的切入点移动到方法上指定的字符串 @Pointcut 注释中
- 将您的建议从未命名块转换为方法。 (由于要进行的参数,这对于 around 建议可能会变得棘手)
你原来的变成这样:
@Aspect
public class EncryptFieldAspect
{
@Pointcut("call(@need.to.fully.qualify.Encrypt * *(java.lang.String)) && target(o) && args(inString) && !within(need.to.fully.qualify.EncryptFieldAspect)");
void encryptStringMethod(Object o, String inString) {}
@Around("encryptStringMethod(o,inString)")
void myAdvice(Object o, String inString, ProceedingJoinPoint thisJoinPoint) {
thisJoinPoint.proceed(new Object[]{o, FakeEncrypt.Encrypt(inString)});
}
}
我有以下基于代码的样式方面,它在代码中查找字段级注释并调用以该字段作为参数的方法。这是它的样子..
public aspect EncryptFieldAspect
{
pointcut encryptStringMethod(Object o, String inString):
call(@Encrypt * *(String))
&& target(o)
&& args(inString)
&& !within(EncryptFieldAspect);
void around(Object o, String inString) : encryptStringMethod(o, inString) {
proceed(o, FakeEncrypt.Encrypt(inString));
return;
}
}
上述方法工作正常,但我想将其转换为基于 Spring 或 AspectJ 的注释,类似于此。发现 AspectJ 文档有点混乱任何提示都会有所帮助..
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MyAspect {
@Around("execution(public * *(..))")
public Object allMethods(final ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println("Before...");
try{
return thisJoinPoint.proceed();
}finally{
System.out.println("After...");
}
}
}
不确定您正在阅读哪些文档 - https://eclipse.org/aspectj/doc/next/adk15notebook/ataspectj-pcadvice.html 中的页面向您展示了如何将代码转换为注释样式。我承认它们并不像它们可能的那样全面。
基本上:
- 从 aspect 关键字切换到 @Aspect
- 将你的切入点移动到方法上指定的字符串 @Pointcut 注释中
- 将您的建议从未命名块转换为方法。 (由于要进行的参数,这对于 around 建议可能会变得棘手)
你原来的变成这样:
@Aspect
public class EncryptFieldAspect
{
@Pointcut("call(@need.to.fully.qualify.Encrypt * *(java.lang.String)) && target(o) && args(inString) && !within(need.to.fully.qualify.EncryptFieldAspect)");
void encryptStringMethod(Object o, String inString) {}
@Around("encryptStringMethod(o,inString)")
void myAdvice(Object o, String inString, ProceedingJoinPoint thisJoinPoint) {
thisJoinPoint.proceed(new Object[]{o, FakeEncrypt.Encrypt(inString)});
}
}