需求:今天作项目的时候,须要获取某个包下面全部的类里面的@Resources字段的名字以及所注入的接口所对应的路径java
要是这文件夹只有几个Action的话,我却是能够考虑直接手动,但是 有整整70多个Action,若是我手动输入的话岂不是要花不少不少时间,并且也不保证效率,因而乎就索性写了个小程序来获得这些数据小程序
具体步骤代码以下:code
一:获取指定包下全部的类server
public static void scan(String packageName ,List<Class<?>> list) throws Exception{ //packageName为指定的目录如com.xxx.xxx String path="E:\\dev\\eachserver\\eachpre\\src\\main\\java\\com\\each\\http\\business\\action"; File dir=new File(path); File[] files=dir.listFiles(); //获取此目录下的文件列表 Class<?> cla=null; for(File f:files){ cla=Class.forName(packageName+"."+f.getName().split("\\.")[0]); list.add(cla);\\class添加进list } }
二:扫瞄类里面特定的注解所对应的类对象
public static void getAnnotion(List<Class<?>> list) throws Exception{ System.out.println(list.size()); //打印文件数量 Map<String,String> map = new HashMap<String,String>(); for(Class<?> cla:list){ Field[] field = Class.forName(cla.getName()).getDeclaredFields();\\获取class里面所声明的字段 for (Field field1:field){ if (field1.getAnnotation(Resource.class)!=null){ \\判断此字段是否有@Resource map.put(field1.getAnnotation(Resource.class).name(),field1.getType().getName()); \\以防重复 这个地方是用map储存 } } } for ( Map.Entry<String,String> entry:map.entrySet()){ // System.out.println("<dubbo:reference group=\""+entry.getKey()+"\" id=\""+entry.getKey()+"\" interface=\""+entry.getValue()+"\" version=\"1.0\"/>"); System.out.println(entry.getKey()+" "+entry.getValue()); \\遍历打印 } }
源代码:接口
package com.each; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by zhu on 2016/4/28. */ public class TestMain { public static void scan(String packageName ,List<Class<?>> list) throws Exception{ String path="E:\\dev\\eachserver\\eachpre\\src\\main\\java\\com\\each\\http\\business\\action"; File dir=new File(path); File[] files=dir.listFiles(); Class<?> cla=null; for(File f:files){//E:\dev\eachserver\eachpre\src\main\java\com\each\http\business\action if(f.isDirectory()){ String childName=packageName+"."+f.getName(); scan(childName, list); }else{ cla=Class.forName(packageName+"."+f.getName().split("\\.")[0]); list.add(cla); } } } public static void getAnnotion(List<Class<?>> list) throws Exception{ System.out.println(list.size()); Map<String,String> map = new HashMap<String,String>(); for(Class<?> cla:list){ Field[] field = Class.forName(cla.getName()).getDeclaredFields(); for (Field field1:field){ if (field1.getAnnotation(Resource.class)!=null){ map.put(field1.getAnnotation(Resource.class).name(),field1.getType().getName()); } } } for ( Map.Entry<String,String> entry:map.entrySet()){ // System.out.println("<dubbo:reference group=\""+entry.getKey()+"\" id=\""+entry.getKey()+"\" interface=\""+entry.getValue()+"\" version=\"1.0\"/>"); System.out.println(entry.getKey()+" "+entry.getValue()); } } public static void main(String[] args) throws Exception { List<Class<?>> list=new ArrayList<Class<?>>(); scan("com.each.http.business.action",list);//把这个对象的路径写入,就能够了。 getAnnotion(list); } }