{"id":113,"date":"2023-02-18T10:04:55","date_gmt":"2023-02-18T02:04:55","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=113"},"modified":"2023-02-18T10:06:55","modified_gmt":"2023-02-18T02:06:55","slug":"java-annotations-on-parent-class-be-inherited-by-children-classes","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/java-annotations-on-parent-class-be-inherited-by-children-classes\/","title":{"rendered":"Java\u7236\u7c7b\u4e0a\u7684\u6ce8\u89e3\u80fd\u88ab\u5b50\u7c7b\u7ee7\u627f\u5417"},"content":{"rendered":"<p>\u5728\u7f16\u5199\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a<code>@Inherited<\/code>\u6ce8\u89e3\uff0c\u6307\u660e\u81ea\u5b9a\u4e49\u6ce8\u89e3\u662f\u5426\u53ef\u4ee5\u88ab\u7ee7\u627f\u3002\u4f46\u5b9e\u73b0\u60c5\u51b5\u53c8\u53ef\u7ec6\u5206\u4e3a\u591a\u79cd\u3002<\/p>\n<p>\u6d4b\u8bd5\u73af\u5883\u5982\u4e0b\uff1a<\/p>\n<p>\u7236\u7c7b\u7684\u7c7b\u4e0a\u548c\u65b9\u6cd5\u4e0a\u6709\u81ea\u5b9a\u4e49\u7684\u6ce8\u89e3<code>@MyAnnotation<\/code>\uff0c\u5b50\u7c7b\u7ee7\u627f\u4e86\u8fd9\u4e2a\u7236\u7c7b\uff0c\u5206\u522b\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>\u5b50\u7c7b\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u7236\u7c7b\u4e0a\u7684\u62bd\u8c61\u65b9\u6cd5<\/li>\n<li>\u5b50\u7c7b\u65b9\u6cd5\uff0c\u7ee7\u627f\u4e86\u7236\u7c7b\u4e0a\u7684\u65b9\u6cd5<\/li>\n<li>\u5b50\u7c7b\u65b9\u6cd5\uff0c\u8986\u76d6\u4e86\u7236\u7c7b\u4e0a\u7684\u65b9\u6cd5<\/li>\n<\/ul>\n<h2>\u6d4b\u8bd5\u4ee3\u7801<\/h2>\n<h3>MyAnnotation\u81ea\u5b9a\u4e49\u6ce8\u89e3<\/h3>\n<pre><code class=\"language-java\">package  test.annotation;\nimport  java.lang.annotation.Inherited;\nimport  java.lang.annotation.Retention;\n\n\/**\n * \u81ea\u5b9a\u4e49\u6ce8\u89e3\n *\/\n@Inherited  \/\/\u53ef\u4ee5\u88ab\u7ee7\u627f\n@Retention (java.lang.annotation.RetentionPolicy.RUNTIME)  \/\/\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u8bfb\u53d6\u6ce8\u89e3\npublic   @interface  MyAnnotation {\n    String value();\n}<\/code><\/pre>\n<h3>\u7236\u7c7b<\/h3>\n<pre><code class=\"language-java\">package test.annotation;\n\n@MyAnnotation(value = &quot;\u7c7b\u540d\u4e0a\u7684\u6ce8\u89e3&quot;)\npublic abstract class ParentClass {\n\n    @MyAnnotation(value = &quot;\u7236\u7c7b\u7684abstractMethod\u65b9\u6cd5&quot;)\n    public abstract void abstractMethod();\n\n    @MyAnnotation(value = &quot;\u7236\u7c7b\u7684doExtends\u65b9\u6cd5&quot;)\n    public void doExtends() {\n        System.out.println(&quot;ParentClass doExtends ...&quot;);\n    }\n\n    @MyAnnotation(value = &quot;\u7236\u7c7b\u7684doHandle\u65b9\u6cd5&quot; )\n    public void doHandle() {\n        System.out.println(&quot;ParentClass doHandle ...&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u5b50\u7c7b<\/h3>\n<pre><code class=\"language-java\">package test.annotation;\n\npublic class SubClass extends ParentClass {\n\n    \/\/\u5b50\u7c7b\u5b9e\u73b0\u7236\u7c7b\u7684\u62bd\u8c61\u65b9\u6cd5\n    @Override\n    public void abstractMethod() {\n        System.out.println(&quot;\u5b50\u7c7b\u5b9e\u73b0\u7236\u7c7b\u7684abstractMethod\u62bd\u8c61\u65b9\u6cd5&quot;);\n    }\n\n    \/\/\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\n\n    \/\/\u5b50\u7c7b\u8986\u76d6\u7236\u7c7b\u7684doHandle\u65b9\u6cd5\n    @Override\n    public void doHandle() {\n        System.out.println(&quot;\u5b50\u7c7b\u8986\u76d6\u7236\u7c7b\u7684doHandle\u65b9\u6cd5&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u6d4b\u8bd5\u7c7b<\/h3>\n<pre><code class=\"language-java\">package test.annotation;\n\nimport java.lang.reflect.Method;\n\npublic class MainTest {\n    public static void main(String[] args) throws SecurityException, \n            NoSuchMethodException {\n\n        Class&lt;SubClass&gt; clazz = SubClass.class;\n\n        if (clazz.isAnnotationPresent(MyAnnotation.class )) {\n            MyAnnotation cla = clazz.getAnnotation(MyAnnotation.class);\n            System.out.println(&quot;\u5b50\u7c7b\u7ee7\u627f\u5230\u7236\u7c7b\u7c7b\u4e0aAnnotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a&quot; + cla.value());\n        } else {\n            System.out.println(&quot;\u5b50\u7c7b\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7b\u7c7b\u4e0aAnnotation&quot;);\n        }\n\n        \/\/\u5b9e\u73b0\u62bd\u8c61\u65b9\u6cd5\u6d4b\u8bd5\n        Method method = clazz.getMethod(&quot;abstractMethod&quot;, new Class[] {});\n        if (method.isAnnotationPresent(MyAnnotation.class)) {\n            MyAnnotation ma = method.getAnnotation(MyAnnotation.class);\n            System.out.println(&quot;\u5b50\u7c7b\u5b9e\u73b0\u7236\u7c7b\u7684abstractMethod\u62bd\u8c61\u65b9\u6cd5\uff0c\u7ee7\u627f\u5230\u7236\u7c7b\u62bd\u8c61\u65b9\u6cd5\u4e2d\u7684Annotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a&quot; + ma.value());\n        } else {\n            System.out.println(&quot;\u5b50\u7c7b\u5b9e\u73b0\u7236\u7c7b\u7684abstractMethod\u62bd\u8c61\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7b\u62bd\u8c61\u65b9\u6cd5\u4e2d\u7684Annotation&quot;);\n        }\n\n        \/\/\u8986\u76d6\u6d4b\u8bd5   \n        Method methodOverride = clazz.getMethod(&quot;doExtends&quot;, new Class[] {});\n        if (methodOverride.isAnnotationPresent(MyAnnotation. class)) {\n            MyAnnotation ma = methodOverride.getAnnotation(MyAnnotation.class );\n            System.out.println(&quot;\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\uff0c\u7ee7\u627f\u5230\u7236\u7c7bdoExtends\u65b9\u6cd5\u4e2d\u7684Annotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a&quot; + ma.value());\n        } else {\n            System.out.println(&quot;\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7bdoExtends\u65b9\u6cd5\u4e2d\u7684Annotation&quot;);\n        }\n\n        \/\/\u7ee7\u627f\u6d4b\u8bd5\n        Method methodExtend = clazz.getMethod(&quot;doHandle&quot;, new Class[] {});\n        if (methodExtend.isAnnotationPresent(MyAnnotation.class)) {\n            MyAnnotation ma = methodExtend.getAnnotation(MyAnnotation.class);\n            System.out.println(&quot;\u5b50\u7c7b\u8986\u76d6\u7236\u7c7b\u7684doHandle\u65b9\u6cd5\uff0c\u7ee7\u627f\u5230\u7236\u7c7bdoHandle\u65b9\u6cd5\u4e2d\u7684Annotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a&quot; + ma.value());\n        } else {\n            System.out.println(&quot;\u5b50\u7c7b\u8986\u76d6\u7236\u7c7b\u7684doHandle\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7bdoHandle\u65b9\u6cd5\u4e2d\u7684Annotation&quot; );\n        }\n    }\n}<\/code><\/pre>\n<p>\u7f16\u5199\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\u672a\u5199<code>@Inherited<\/code>\u7684\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>\u5b50\u7c7b\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7b\u7c7b\u4e0aAnnotation\n\u5b50\u7c7b\u5b9e\u73b0\u7236\u7c7b\u7684abstractMethod\u62bd\u8c61\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7b\u62bd\u8c61\u65b9\u6cd5\u4e2d\u7684Annotation\n\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\uff0c\u7ee7\u627f\u5230\u7236\u7c7bdoExtends\u65b9\u6cd5\u4e2d\u7684Annotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\n\u5b50\u7c7b\u8986\u76d6\u7236\u7c7b\u7684doHandle\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7bdoHandle\u65b9\u6cd5\u4e2d\u7684Annotation<\/code><\/pre>\n<p>\u7f16\u5199\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\u5199\u4e86<code>@Inherited<\/code>\u7684\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre><code>\u5b50\u7c7b\u7ee7\u627f\u5230\u7236\u7c7b\u7c7b\u4e0aAnnotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a\u7c7b\u540d\u4e0a\u7684\u6ce8\u89e3\n\u5b50\u7c7b\u5b9e\u73b0\u7236\u7c7b\u7684abstractMethod\u62bd\u8c61\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7b\u62bd\u8c61\u65b9\u6cd5\u4e2d\u7684Annotation\n\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\uff0c\u7ee7\u627f\u5230\u7236\u7c7bdoExtends\u65b9\u6cd5\u4e2d\u7684Annotation\uff0c\u5176\u4fe1\u606f\u5982\u4e0b\uff1a\u7236\u7c7b\u7684doExtends\u65b9\u6cd5\n\u5b50\u7c7b\u8986\u76d6\u7236\u7c7b\u7684doHandle\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ee7\u627f\u5230\u7236\u7c7bdoHandle\u65b9\u6cd5\u4e2d\u7684Annotation<\/code><\/pre>\n<p>\u7ed3\u8bba<\/p>\n<p>\u7236\u7c7b\u7684\u7c7b\u4e0a\u548c\u65b9\u6cd5\u4e0a\u6709\u81ea\u5b9a\u4e49\u7684\u6ce8\u89e3\uff0c\u5b50\u7c7b\u7ee7\u627f\u4e86\u8fd9\u4e2a\u7236\u7c7b\u7684\u60c5\u51b5\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\"><\/th>\n<th style=\"text-align: center;\">\u7f16\u5199\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\u672a\u5199<code>@Inherited<\/code>\u7684\u8fd0\u884c\u7ed3\u679c<\/th>\n<th style=\"text-align: center;\">\u7f16\u5199\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\u5199\u4e86<code>@Inherited<\/code>\u7684\u8fd0\u884c\u7ed3\u679c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">\u5b50\u7c7b\u7684\u7c7b\u4e0a\u80fd\u5426\u7ee7\u627f\u5230\u7236\u7c7b\u7684\u7c7b\u4e0a\u7684\u6ce8\u89e3\uff1f<\/td>\n<td style=\"text-align: center;\">\u5426<\/td>\n<td style=\"text-align: center;\">\u80fd<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u5b50\u7c7b\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u7236\u7c7b\u4e0a\u7684\u62bd\u8c61\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u5426\u7ee7\u627f\u5230\u6ce8\u89e3\uff1f<\/td>\n<td style=\"text-align: center;\">\u5426<\/td>\n<td style=\"text-align: center;\">\u5426<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u5b50\u7c7b\u65b9\u6cd5\uff0c\u7ee7\u627f\u4e86\u7236\u7c7b\u4e0a\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u5426\u7ee7\u627f\u5230\u6ce8\u89e3\uff1f<\/td>\n<td style=\"text-align: center;\">\u80fd<\/td>\n<td style=\"text-align: center;\">\u80fd<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u5b50\u7c7b\u65b9\u6cd5\uff0c\u8986\u76d6\u4e86\u7236\u7c7b\u4e0a\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u5426\u7ee7\u627f\u5230\u6ce8\u89e3\uff1f<\/td>\n<td style=\"text-align: center;\">\u5426<\/td>\n<td style=\"text-align: center;\">\u5426<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u901a\u8fc7\u6d4b\u8bd5\u7ed3\u679c\u6765\u770b\uff0c<code>@Inherited<\/code>\u53ea\u662f\u53ef\u63a7\u5236\u5bf9\u7c7b\u540d\u4e0a\u6ce8\u89e3\u662f\u5426\u53ef\u4ee5\u88ab\u7ee7\u627f\u3002\u4e0d\u80fd\u63a7\u5236\u65b9\u6cd5\u4e0a\u7684\u6ce8\u89e3\u662f\u5426\u53ef\u4ee5\u88ab\u7ee7\u627f\u3002<\/p>\n<blockquote>\n<p>\u9644\u6ce8\uff1aSpring \u5b9e\u73b0\u4e8b\u52a1\u7684\u6ce8\u89e3<code>@Transactional<\/code>\u662f\u53ef\u4ee5\u88ab\u7ee7\u627f\u7684\uff0c\u901a\u8fc7\u67e5\u770b\u5176\u6e90\u7801\u53ef\u4ee5\u770b\u5230<code>@Inherited<\/code><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7f16\u5199\u81ea\u5b9a\u4e49\u6ce8\u89e3\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a@Inherited\u6ce8\u89e3\uff0c\u6307\u660e\u81ea\u5b9a\u4e49\u6ce8\u89e3\u662f\u5426\u53ef\u4ee5\u88ab\u7ee7\u627f\u3002\u4f46\u5b9e\u73b0\u60c5\u51b5\u53c8\u53ef\u7ec6\u5206\u4e3a\u591a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}