|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-04-14
fortaotao 写道 如果我的pointcut如下面写的话也可以拦截到。
@After("execution(* com.XXXX.framework.service.CoreService.create(..))")
"XXXX"是我们公司的英文名哈,不好显示出来,见谅,呵呵。谢谢~ 那可能就是aspectj不支持你那种方式,但是要读到interface或者父类的annotation是可以的,你要自己写的话还不如直接用上面的方式,不要为了annotation而annotation. 我目前是在参数上面加annotation,比如create(User user),对User.class加annotation,不知道这样能不能达到你的需求 |
|
| 返回顶楼 | |
|
时间:2008-04-14
参数上使用注解也是一种思路,应该是我钻牛角尖了。我开始想的是方法上用注解适合以后项目的复用。准备单独写些Aspect类,比如日志LogAspect、工作流WorkflowAspect,然后谁的方法上做了这些注解就自动调用定义的事务。万分感谢quaff的帮助哈。^_^
|
|
| 返回顶楼 | |
|
时间:2008-04-14
quaff 写道 fortaotao 写道 Override必须在JDK1.5以上版本上才能通过哇。编译检查而已。不加也可以。
我能用annotation当然是1.5以上了,Override是覆盖方法,不是实现方法,可能是编译器的解释不同. 在jdk1.6上支持实现接口的方法可以用@Override标注,1.5就编译出错 |
|
| 返回顶楼 | |
|
时间:2008-04-15
public static boolean isAnnotationPresent(Method method,Class<? extends Annotation> clasz)
{ if(method.isAnnotationPresent(clasz)) return true; Class[] faces = method.getDeclaringClass().getInterfaces(); for(Class c : faces) { try{ Method m = c.getDeclaredMethod(method.getName(), method.getParameterTypes()); return m.isAnnotationPresent(clasz); }catch(Exception e){} } return false; } public static <T extends Annotation> T getAnnotation(Method method,Class<T> clasz) { T anno = method.getAnnotation(clasz); if(anno != null) return anno; Class[] faces = method.getDeclaringClass().getInterfaces(); for(Class c : faces) { try{ Method m = c.getDeclaredMethod(method.getName(), method.getParameterTypes()); anno = m.getAnnotation(clasz); if(anno != null) return anno; }catch(Exception e){} } return null; } |
|
| 返回顶楼 | |
|
时间:2008-04-15
pppppp 写道 public static boolean isAnnotationPresent(Method method,Class<? extends Annotation> clasz)
{ if(method.isAnnotationPresent(clasz)) return true; Class[] faces = method.getDeclaringClass().getInterfaces(); for(Class c : faces) { try{ Method m = c.getDeclaredMethod(method.getName(), method.getParameterTypes()); return m.isAnnotationPresent(clasz); }catch(Exception e){} } return false; } public static <T extends Annotation> T getAnnotation(Method method,Class<T> clasz) { T anno = method.getAnnotation(clasz); if(anno != null) return anno; Class[] faces = method.getDeclaringClass().getInterfaces(); for(Class c : faces) { try{ Method m = c.getDeclaredMethod(method.getName(), method.getParameterTypes()); anno = m.getAnnotation(clasz); if(anno != null) return anno; }catch(Exception e){} } return null; } 请问一下,这是你自己封装的还是来源于什么地方的? 自己写判断是没问题。但是解决不了Spring里用AspectJ的pointcut拦截接口注解的问题。呵呵。 |
|
| 返回顶楼 | |
|
时间:2008-04-16
这是AbstractFallbackTransactionAttributeSource中的实现, private TransactionAttribute computeTransactionAttribute(Method method, Class targetClass) {
// Don't allow no-public methods as required.
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
return null;
}
// The method may be on an interface, but we need attributes from the target class.
// If the target class is null, the method will be unchanged.
Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
// If we are dealing with method with generic parameters, find the original method.
if (JdkVersion.isAtLeastJava15()) {
specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
}
// First try is the method in the target class.
TransactionAttribute txAtt = findTransactionAttribute(specificMethod);
if (txAtt != null) {
return txAtt;
}
// Second try is the transaction attribute on the target class.
txAtt = findTransactionAttribute(specificMethod.getDeclaringClass());
if (txAtt != null) {
return txAtt;
}
if (specificMethod != method) {
// Fallback is to look at the original method.
txAtt = findTransactionAttribute(method);
if (txAtt != null) {
return txAtt;
}
// Last fallback is the class of the original method.
return findTransactionAttribute(method.getDeclaringClass());
}
return null;
}
从代码中可以看到,先到实现类或其中方法上去找有没有指定的annotation,然后到接口上类或或其方法上指,如果都找不到,就返回空。这是@Transactional。@Secured的实现也是类似。 所以如果你要实现从接口上指定@Log,可以参考AbstractFallbackMethodDefinitionSource的实现。不过Spring文档中建议在实现类中指定。
|
|
| 返回顶楼 | |
|
时间:2008-04-17
原来是这么回事,这几天我也碰到这种问题!
但是如果是要这么做的话怎么样才能拦截自己在接口处声明的注解类型。也是通过自己定义的拦截器么? |
|
| 返回顶楼 | |





