java 编程基础 注解 :可重复的注解
重复注解
@Results({ @Result(name="failure", location="failed.jsp"), @Result(name="success ", location ="succ.jsp") }) public Acton FooAction{ ... }
java8以后可以这样写
@Result(name="failure", location="failed.jsp"), @Result(name="success ", location ="succ.jsp") public Acton FooAction{ ... }
可重复注解的开发流程
package com.zmd.repeatAnotation; import java.lang.annotation.*; /** * @ClassName ActionTodo * @projectName: object1 * @author: Zhangmingda * @description: 可重复注解 * date: 2021/5/19. */ //@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Repeatable(ActionTodos.class) public @interface ActionTodo { String actionListenerName(); }
package com.zmd.repeatAnotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @ClassName ActionTodos * @projectName: object1 * @author: Zhangmingda * @description: 容器注解 * date: 2021/5/19. */ //容器 @Retention(RetentionPolicy.RUNTIME) //@Target(ElementType.TYPE) public @interface ActionTodos { ActionTodo[] value(); }
3,具体的使用:
package com.zmd.repeatAnotation; import javax.swing.*; import java.lang.annotation.Annotation; import java.lang.reflect.Field; /** * @ClassName UseRepeatable * @projectName: object1 * @author: Zhangmingda * @description: XXX * date: 2021/5/19. */ @ActionTodo(actionListenerName = "打鸟") @ActionTodo(actionListenerName = "掏蛋") @ActionTodo(actionListenerName = "煮着吃") public class UseRepeatable { @ActionTodo(actionListenerName = "打鸟") @ActionTodo(actionListenerName = "掏蛋") @ActionTodo(actionListenerName = "煮着吃") private static JButton okButton = new JButton("一石N鸟"); public static void main(String[] args) throws NoSuchFieldException { /** * 获取注解容器 */ Annotation[] annotations = UseRepeatable.class.getAnnotations(); for (Annotation annotation : annotations){ //获取所有注解,包含容器注解 System.out.println(annotation); //获取所有注解,包含容器注解 } Field okbtnField = UseRepeatable.class.getDeclaredField("okButton"); Annotation[] actions = okbtnField.getAnnotations(); //获取所有注解,包含容器注解 //ActionTodo[] actions = okbtnField.getAnnotationsByType(ActionTodo.class); //可重复注解 for (Annotation actionTodo :actions) { System.out.println(actionTodo); } /** * 获取注解类的重复注解 */ ActionTodo[] actionTodos = UseRepeatable.class.getAnnotationsByType(ActionTodo.class); for (ActionTodo actionTodo : actionTodos){ System.out.println(actionTodo); } /** * 获取属性(类变量)的重复注解 */ //获取变量反射对象 Field[] fields = UseRepeatable.class.getDeclaredFields(); //循环判断 for (Field field:fields){ field.setAccessible(true); //是否被容器注解修饰 if (field.isAnnotationPresent(ActionTodos.class)){ //获取注解列表 ActionTodo[] actionTodos1 = field.getAnnotationsByType(ActionTodo.class); for (ActionTodo actionTodo:actionTodos1){ System.out.println(actionTodo); } } } } }
测试使用输出
@com.zmd.repeatAnotation.ActionTodos(value={@com.zmd.repeatAnotation.ActionTodo(actionListenerName="打鸟"), @com.zmd.repeatAnotation.ActionTodo(actionListenerName="掏蛋"), @com.zmd.repeatAnotation.ActionTodo(actionListenerName="煮着吃")})
@com.zmd.repeatAnotation.ActionTodos(value={@com.zmd.repeatAnotation.ActionTodo(actionListenerName="打鸟"), @com.zmd.repeatAnotation.ActionTodo(actionListenerName="掏蛋"), @com.zmd.repeatAnotation.ActionTodo(actionListenerName="煮着吃")})
@com.zmd.repeatAnotation.ActionTodo(actionListenerName="打鸟")
@com.zmd.repeatAnotation.ActionTodo(actionListenerName="掏蛋")
@com.zmd.repeatAnotation.ActionTodo(actionListenerName="煮着吃")
@com.zmd.repeatAnotation.ActionTodo(actionListenerName="打鸟")
@com.zmd.repeatAnotation.ActionTodo(actionListenerName="掏蛋")
@com.zmd.repeatAnotation.ActionTodo(actionListenerName="煮着吃")
posted on 2021-05-19 19:25 zhangmingda 阅读(1217) 评论(0) 编辑 收藏 举报