未涉及java AspectJ注释切入点(对于任何注释类型)
我已经经历了很多stackoverflow问题和AspectJ docs,我的实现中肯定有一些小问题,因为我没有发现任何红旗出现在示例和文档中
目标
定义一个自定义注释,作为方法调用的切入点(如果这里的语义不正确,我很抱歉,我希望示例能有所帮助)
代码
请注意,我已经更改了一些类名,使它们听起来更通用
package com.example.so;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import java.util.HashSet;
@Aspect
public class CustomAspect {
private final HashSet<String> instanceProperty;
public CustomAspect(final HashSet<String> constructorArgument) {
this.instanceProperty = constructorArgument;
}
/* @Around("execution(* com.example.so.IService..*(..))")
public void beforeTransactionalMethod(ProceedingJoinPoint pjp) throws Throwable {
System.log.println("execution point-cut");
pjp.proceed();
}*/
// @Before("@annotation(java.lang.Override)")
@Before("@annotation(com.example.so.Custom)")
public void around() throws Throwable {
System.out.println("annotation");
}
}
方面界面:
package com.example.so;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Custom{
}
弹簧配置
<aop:aspectj-autoproxy />
<bean id="customAspect" class="com.example.so.CustomAspect">
<constructor-arg ref="arg"/>
</bean>
使用方面:
(...)
@Override
@Custom
public void saveData(final Object data) {
// this is being called
}
(...)
POM以防万一(均为1.6.11版):
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
行为
如果我删除以下评论:
/* @Around("execution(* com.example.so.IService..*(..))")
public void beforeTransactionalMethod(ProceedingJoinPoint pjp) throws Throwable {
System.log.println("execution point-cut");
pjp.proceed();
}*/
这会被调用,但无论我做什么,我都无法调用注释点切割,无论是使用我的自定义注释还是其他注释(这可能很愚蠢,但我还是尝试了):
//@Before("@annotation(java.lang.Override)")
@Before("@annotation(com.example.so.Custom)")
public void around() throws Throwable {
System.out.println("annotation");
}
有人有什么想法吗?我相信所有的配置都在那里,因为CustomAspect
在上下文中并且是活动的。我以前添加过组件标签,但我删除了它们,因为我认为它们在这种情况下没有意义
提前谢谢你
共 (0) 个答案