1、java注解介绍java
介绍:@Target ,@Retention、@Document web
一、@Target : 就是注解放到什么地方!ElementType的枚举类型以下:jvm
TYPE:类,接口或者enum声明ide
FIELD:域(属性)声明测试
METHOD:方法声明.net
PARAMETER:参数声明debug
CONSTRUCTOR:构造方法声明code
ANNOTATION_TYPE:注释类型声明对象
PACKAGE:包声明接口
二、@Rentation: 表示须要在什么级别保存该注解信息。RetentionPolicy的枚举类型以下:
SOURCE:注解将被编译器丢弃
CLASS:注解将会被编译至class文件中,但在运行时jvm不保留
RUNTIME:运行时保留注解
三、@Documented:此注解将包含在javadoc中
2、代码示例
一、工程截图
二、代码
注解类代码
package com.yujin.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 自定义注解 * 介绍:@Target,@Retention、@Document * 一、@Target : 就是注解放到什么地方!ElementType的枚举类型以下: * TYPE:类,接口或者enum声明 * FIELD:域(属性)声明 * METHOD:方法声明 * PARAMETER:参数声明 * CONSTRUCTOR:构造方法声明 * ANNOTATION_TYPE:注释类型声明 * PACKAGE:包声明 * 二、@Rentation: 表示须要在什么级别保存该注解信息。RetentionPolicy的枚举类型以下: * SOURCE:注解将被编译器丢弃 * CLASS:注解将会被编译至class文件中,但在运行时jvm不保留 * RUNTIME:运行时保留注解 * 三、@Documented:此注解将包含在javadoc中 * @author yujin */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface HelloClass { String value() default "helloClass"; }
注解方法代码
package com.yujin.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 定义方法注解 * @author Administrator */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface HelloMethod { String name(); }
com.yujin.action包下类
package com.yujin.action; import com.yujin.annotation.HelloClass; import com.yujin.annotation.HelloMethod; @HelloClass(value="roleAction") public class RoleAction { @HelloMethod(name="addRole") public void addRole(){ System.err.println("addRole()-------------------"); } public void listRoles(){ System.err.println("listRoles()-------------------"); } public void addRoleUI(){ System.err.println("addRoleUI()-------------------"); } @HelloMethod(name="editRole") public void editRole(){ System.err.println("editRole()-------------------"); } public void editRoleUI(){ System.err.println("editRoleUI()-------------------"); } @HelloMethod(name="deleteRole") public void deleteRole(){ System.err.println("editRoleUI()-------------------"); } }
package com.yujin.action; import com.yujin.annotation.HelloClass; import com.yujin.annotation.HelloMethod; @HelloClass(value="helloAction") public class HelloAction { @HelloMethod(name="addUser") public void addUser(){ System.err.println("addUser()-------------------"); } public void listUsers(){ System.err.println("listUsers()-------------------"); } public void addUserUI(){ System.err.println("addUserUI()-------------------"); } @HelloMethod(name="editUser") public void editUser(){ System.err.println("editUser()-------------------"); } public void editUserUI(){ System.err.println("editUserUI()-------------------"); } @HelloMethod(name="deleteUser") public void deleteUser(){ System.err.println("editUserUI()-------------------"); } }
测试类(主要是得到注解的类和方法)
java测试
package com.yujin.test; import java.io.File; import java.io.FilenameFilter; import java.lang.reflect.Method; import com.yujin.annotation.HelloClass; import com.yujin.annotation.HelloMethod; public class TestAnnotation { /** * 经过包名获取全部类 * @param pname * @return */ public static String[] getClassByPackage(String pname) { //经过包名获取全部类 String pr = pname.replace(".", "/"); String pp = TestAnnotation.class.getClassLoader().getResource(pr).getPath(); File file = new File(pp); String[] fs = file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if(name.endsWith(".class")) return true; return false; } }); return fs; } public static void init(String pname){ try { String [] strs = getClassByPackage("com.yujin.action"); for(String p : strs){ String pc = pname+"."+p.substring(0,p.lastIndexOf(".class")); //获得了类的class对象 Class clz = Class.forName(pc); if(!clz.isAnnotationPresent(HelloClass.class)) continue; System.err.println("************"+clz.getName()+"*************"); //获取每一个类中的方法,以此肯定哪些角色能够访问哪些方法 Method[] methods = clz.getDeclaredMethods(); //由于是注解到method上的,因此首先要获取这个方法 for(Method method : methods){ if(method.isAnnotationPresent(HelloMethod.class)){ //有注解 HelloMethod helloMethod = method.getAnnotation(HelloMethod.class); if(helloMethod != null){ System.err.println("注解:"+method.getName()+"---name:"+helloMethod.name()); } }else{ System.err.println("没注解:"+method.getName()); } } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public static void main(String[] args) { init("com.yujin.action"); } }
java web 测试
public class HelloWorld implements ApplicationListener<ContextRefreshedEvent> { Logger logger = LoggerFactory.getLogger(HelloWorld.class); @Override public void onApplicationEvent(ContextRefreshedEvent event) { logger.debug("------初始化执行----"); try { // 获取上下文 ApplicationContext context = event.getApplicationContext(); // 获取全部beanNames String[] beanNames = context.getBeanNamesForType(Object.class); for (String beanName : beanNames) { HelloClass helloClass = context.findAnnotationOnBean(beanName, HelloClass.class); //判断该类是否含有HelloClass注解 if (helloClass != null) { Method[] methods = context.getBean(beanName).getClass() .getDeclaredMethods(); for (Method method : methods) { //判断该方法是否有HelloMethod注解 if (method.isAnnotationPresent(HelloMethod.class)) { HelloMethod helloMethod = method .getAnnotation(HelloMethod.class); String id = helloMethod.id(); String name = helloMethod.name(); // do something logger.debug("注解方法:" + method.getName() + "," + id + "," + name); } } } } } catch (Exception e) { e.printStackTrace(); } } }