承德网站制作公司优选网十科技,琴行网站开发学术论文,网络怎么推广,河北9市最新消息java方法示例注释 “注释”是编程语言定义的一种#xff0c;并用作“标记”。 可以将它们视为编程语言引擎可以理解的注释行。 它们不会直接影响程序的执行#xff0c;但会直接影响程序的执行。 定义 注释使用interface关键字定义#xff0c;并且与接口相似。 它具有定义类… java方法示例注释 “注释”是编程语言定义的一种并用作“标记”。 可以将它们视为编程语言引擎可以理解的注释行。 它们不会直接影响程序的执行但会直接影响程序的执行。 定义 注释使用interface关键字定义并且与接口相似。 它具有定义类似于接口方法的属性。 属性可以具有默认值。 让我们定义一个名为“ Page”的注释它定义应用程序的UI页面 public interface Page {int id();String url();String icon() default [none];String name(); default [none];
} 用法 批注广泛用于通知编译器或编译时/运行时/部署时处理。 注释的使用更简单 Page(id1, url”studentView”, icon“icons/student.png”, name”Students”)
public class StudentWindow extends Window { … } 还可以为方法和属性定义注释 AnAnnotation
public String getElementName() {…}AnAnnotation(type”manager”, score3)
public int income; 例子 1反射/代码生成 具有特定注释的方法可以在运行时进行处理 public interface MyAnnotation { ... }
public class TestClass {MyAnnotationpublic static method1() { ... }MyAnnotationpublic static method2() { ... }MyAnnotationpublic static method3() { ... }
}public static void main(String[] args) {for (Method method : Class.forName(TestClass).getMethods()) {if (method.isAnnotationPresent(MyAnnotation.class)) {// do what you want}}
} 2 Spring bean配置本节需要Spring bean配置知识 让我们再次使用“页面”注释 package com.cmp.annotation;
public interface Page {int id();String url();String icon() default [none];String name(); default [none];
} 假设我们在包中有几个带有Page批注的类 Page(id1, url”studentView”, icon“icons/student.png”, name”Students”)
public class StudentWindow extends Window { … } 如果我们在Spring application-context.xml文件中如下定义bean配置则Spring将创建类实例“在给定包中放置了Page注释”。 context:component-scan base-packagecom.cmp.ui annotation-configtrue
context:include-filter typeannotation expressioncom.cmp.annotation.Page/
/context:component-scan 因此我们被强制Spring在运行时仅实例化某些类。 有关注释的更多详细信息请参阅 http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html 参考 CodeBalance博客上来自我们JCG合作伙伴 Cagdas Basaraner的Java注释和真实世界的Spring示例 。 相关文章 克隆可序列化和不可序列化的Java对象 Java递归基础 有益的CountDownLatch和棘手的Java死锁 Java Secret加载和卸载静态字段 使用java.util.prefs.Preferences代替java.util.Properties 翻译自: https://www.javacodegeeks.com/2012/01/java-annotations-real-world-spring.htmljava方法示例注释