|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-04-14 关键字: annotation继承的问题
想在接口类中方法使用自己的注解,比如@Log,但是根据文档说明说接口的注解不能被实现类所继承。
作了个测试,确实在实现类中该方法反射中找不到自定义的注解,如上面说的@Log。 比如:TestInterface.java
public interface TestInterface
{
@Log(hello="testInterfaceCreate")
public void create();
}
其实现类TestImpl.java如下:
public class TestImpl implements TestInterface
{
@Override
public void create()
{
}
}
最终,写了个测试Test.java:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<TestImpl> c = TestImpl.class;
Method method = c.getMethod("create");
if(c.isAnnotationPresent(Log.class))
{
Log it = method.getAnnotation(Log.class);
System.out.println("实现类继承了注解Log");
}
else
{
System.out.println("实现类没有继承注解Log");
}
}
}
打印结果“实现类没有继承注解Log”。如果是extends的情况,是可以成功的。 肤浅了解,知道在Spring AOP里面是支持JDK动态代理的,@Transactional和@Secured这些注解都能做到这一点的。它们的实现类在spring容器环境下就可以根据这些注解被拦截。 假设Log定义如下:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Log
{
String hello();
}
总结的问题:不知道有没有办法利用Spring AOP拦截到我在接口类方法上写的自定义注解。实现类上直接加注解,利用Aspect可以拦截到。
@Component
@Aspect
public class LogAspect
{
@After("@annotation(com.XXXXX.annotationtype.Log)")
public void recordOperatinLog()
{
System.out.println("###进入操作日志拦截事务");
}
}
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-04-14
加@Override在eclipse里面通不过编译
Inherited只能在类上面继承 Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation. |
|
| 返回顶楼 | |
|
时间:2008-04-14
Override必须在JDK1.5以上版本上才能通过哇。编译检查而已。不加也可以。
|
|
| 返回顶楼 | |
|
时间:2008-04-14
嗯。确实,实现类没办法获取接口类定义的注解。现在我希望的是:有办法利用Spring AOP拦截到我在接口类方法上写的自定义注解。
|
|
| 返回顶楼 | |
|
时间:2008-04-14
fortaotao 写道 Override必须在JDK1.5以上版本上才能通过哇。编译检查而已。不加也可以。
我能用annotation当然是1.5以上了,Override是覆盖方法,不是实现方法,可能是编译器的解释不同. |
|
| 返回顶楼 | |
|
时间:2008-04-14
fortaotao 写道 嗯。确实,实现类没办法获取接口类定义的注解。现在我希望的是:有办法利用Spring AOP拦截到我在接口类方法上写的自定义注解。
有办法的,获取这个类的超类或者接口,一直向上,所有的都读取,没记错的话xwork就是这么做的 |
|
| 返回顶楼 | |
|
时间:2008-04-14
哦,对的,这样也可以。可能我的主要问题没说明白。我主要是希望像我下面的代码这样,拦截到接口类中的注解定义呢。我不想在实现类写这些注解定义。
@Component
@Aspect
public class LogAspect
{
@After("@annotation(com.XXXXX.annotationtype.Log)")
public void recordOperatinLog()
{
System.out.println("###进入操作日志拦截事务");
}
}
|
|
| 返回顶楼 | |
|
时间:2008-04-14
fortaotao 写道 哦,对的,这样也可以。可能我的主要问题没说明白。我主要是希望像我下面的代码这样,拦截到接口类中的注解定义呢。我不想在实现类写这些注解定义。
@Component
@Aspect
public class LogAspect
{
@After("@annotation(com.XXXXX.annotationtype.Log)")
public void recordOperatinLog()
{
System.out.println("###进入操作日志拦截事务");
}
}
aspectj的pointcut应该已经满足了你的需要,你测试过了没用? |
|
| 返回顶楼 | |
|
时间:2008-04-14
测试过了。我有2个类:CoreService.java和CoreServiceImpl.java。
CoreServiceImpl.java是CoreService.java的实现类。 定义的方法为create(Object obj)。 如果将@Log写在CoreService.java的create()方法上,拦截失败。 写在CoreServiceImpl.java的create()方法上,就成功了。奇怪的很。我再检查下。按照理解应该是可行的。我的事务@Transactional一直是在CoreService.java上标注的,都没问题。 |
|
| 返回顶楼 | |
|
时间:2008-04-14
如果我的pointcut如下面写的话也可以拦截到。
@After("execution(* com.XXXX.framework.service.CoreService.create(..))")
"XXXX"是我们公司的英文名哈,不好显示出来,见谅,呵呵。谢谢~ |
|
| 返回顶楼 | |



