我最近已經開始尋找到Java Web服務,發現以下令人費解:Java的繼承註解在接口
如果非要在其中有一個@Consumes註釋,然後我實現接口接口中定義的方法中,服務正常工作,就好像@Consumes是繼承的。
但是,從閱讀各種文章和here,它似乎註釋不被繼承。
我敲了下面的測試來檢查了這一點:
interface ITestAnnotationInheritance {
@Consumes
void test();
}
class TestAnnotationInheritanceImpl implements ITestAnnotationInheritance {
@Override
//@Consumes // This doesn't appear to be inherited from interface
public void test() {}
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
System.out.println(TestAnnotationInheritanceImpl.class.getMethod("test").getAnnotation(Consumes.class));
}
}
,其結果是:
null
如果我取消了@Consumes在TestAnnotationInheritanceImpl類是輸出爲:
@javax.ws.rs.Consumes(value=[*/*])
這證明註釋不是被繼承的,但是Web服務如何實現w orks罰款?
非常感謝
'@ Inherited'僅適用於類級別的註釋,而不適用於方法級別的註解。 –