1
试图找出如何以注释方式代理我的bean与AOP建议。spring 3 AOP异常建议
我有一个简单的类
@Service
public class RestSampleDao {
@MonitorTimer
public Collection<User> getUsers(){
....
return users;
}
}
我已创建自定义注解监督执行时间
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface MonitorTimer {
}
,并建议做一些假的监控
public class MonitorTimerAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable{
try {
long start = System.currentTimeMillis();
Object retVal = invocation.proceed();
long end = System.currentTimeMillis();
long differenceMs = end - start;
System.out.println("\ncall took " + differenceMs + " ms ");
return retVal;
} catch(Throwable t){
System.out.println("\nerror occured");
throw t;
}
}
}
现在我可以使用它如果我手动代理这样的道的实例
AnnotationMatchingPointcut pc = new AnnotationMatchingPointcut(null, MonitorTimer.class);
Advisor advisor = new DefaultPointcutAdvisor(pc, new MonitorTimerAdvice());
ProxyFactory pf = new ProxyFactory();
pf.setTarget(sampleDao);
pf.addAdvisor(advisor);
RestSampleDao proxy = (RestSampleDao) pf.getProxy();
mv.addObject(proxy.getUsers());
但我该如何在Spring中设置它,以便我的自定义注释方法能够被这个拦截器自动代理?我想注入代理samepleDao而不是真正的一个。这可以做到没有XML配置?
我认为应该有可能仅仅注释我想拦截的方法,并且弹簧DI会代理什么是必要的。
或者我必须使用aspectj吗?将提供最简单的解决方案: - )
非常感谢您的帮助!
感谢的是解决了这个问题: - ) – Art79 2010-01-18 18:27:34