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(); } } }