【发布时间】:2012-12-06 13:30:49
【问题描述】:
我用原始的 Pointcut 和 Advise 方法编写了简单的 Aspect:
@Aspect
public class MyAspect {
@Pointcut("execution(static * com.mtag.util.SomeUtil.someMethod(..))")
public void someMethodInvoke() { }
@AfterReturning(value = "someMethodInvoke())", returning = "comparisonResult")
public void decrementProductCount(List<String> comparisonResult) {
//some actions
}
}
我有以下基于 Spring 注释的应用程序配置:
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
//...
}
以及 com.mtag.util 包中的实用程序类:
public class SomeUtil {
static List<String> someMethod(List<String> oldList, List<String> newList) {
//...
}
}
但是当我打电话时
SomeUtil.someMethod(arg1, arg2);
在单元测试中,我可以看到方法调用没有被拦截并且我的@AfterReturning 建议不起作用。
但如果我将 someMethod() 类型更改为实例(非静态)方法,则切入点
@Pointcut("execution(* com.mtag.util.SomeUtil.someMethod(..))")
并通过添加@Component注解并像这样调用目标方法来管理SomeUtil bean:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class}, loader = AnnotationConfigContextLoader.class)
public class SomeUtilTest {
@Autowired
private SomeUtil someUtil;
@Test
public void categoriesDiffCalc() {
List<String> result = someUtil.someMethod(...);
}
}
一切正常。
我可以通过什么方式为静态方法设置 Advice?
【问题讨论】:
-
@AfterReturning(value = "someMethodInvoke())"你多了一个括号 -
@izhamoidsin,是接受(由我)提供的答案。还是这个不正确?
标签: java aspectj spring-aop