2011-02-07 86 views
12

我使用AspectJ拦截被注解为@Profile(description="something")与注释AspectJ切入点参数

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface Profile { 
    public String description() default ""; 
} 

@Around("com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods(profile)") 
public Object profile(ProceedingJoinPoint pjp, Profile profile) throws Throwable { 
    .... 
} 

@Pointcut("@annotation(com.merc.annotations.Profile)") 
protected void logAnnotatedMethods(Profile profile) { 
} 

方法,但同时compileing使用AJC

formal unbound in pointcut 
+0

嗨,我的要求和你一样。我有一个疑问是什么是'com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods'。我注意到,你创建的logAnnotatedMethods,但我没有得到什么是com.merc.aop.ctw.aspect.PointcutDefinitions?请指导我。 – James 2015-07-07 18:19:13

回答

14
@Pointcut("@annotation(com.merc.annotations.Profile)") 
protected void logAnnotatedMethods(Profile profile) { 
} 

这是不正确的,@annotation()想要一个参数名称,而不是参数类型。

如果您的类是使用调试代码编译的,则pointcut参数必须与方法参数具有相同的名称,如果不是,则需要依赖参数类型是唯一的,或者使用argNames明确写出参数名称参数:

@Pointcut(value="@annotation(profile)",argNames="profile") 
protected void logAnnotatedMethods(Profile arg) { } 

参考:

+0

我不同意。只是使用了注解类型@annotation作为参数,并且它像Spring 3.1中的魅力一样,正如文档所说的那样。 http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html – 2013-03-12 13:45:24

5

我打得周围,我得到以下错误味精发现以下工作

@Pointcut("@annotation(profile)") 
protected void logAnnotatedMethods(Profile profile) { 
}