Home > Java > javaTutorial > Detailed explanation of how Java uses reflection to find classes using specified annotations

Detailed explanation of how Java uses reflection to find classes using specified annotations

黄舟
Release: 2017-09-30 10:24:16
Original
1936 people have browsed it

This article mainly introduces to you the relevant information about how Java uses reflection to find classes using specified annotations. The article introduces it in detail through sample code. It has certain reference learning value for everyone's study or work. It is needed Friends, please follow the editor to learn together.

Preface

I have some free time recently, and I want to write an annotation that is the same as the annotation in spring, and then I hope I can find it and use it. I wrote the annotated class myself. Let’s introduce the implementation method

statement. The following code is written without having seen the spring source code. It is basically compiled from blogs found online

Definition annotations

##Controller.java


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Controller {

}
Copy after login

RequestMapping.java


@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestMapping {

 String value();

}
Copy after login

Use annotations

##

@Controller
public class IndexController {

 @RequestMapping("/")
 public void index() {
 System.out.println("index method")
 }

 @RequestMapping("/about")
 public void about(String args) {
 System.out.println("about method")
 }

}
Copy after login

Scan all classes under the package
The following code is excerpted from

/**
 * 从包package中获取所有的Class
 *
 * @param packageName
 * @return
 */
public static List<Class<?>> getClasses(String packageName) {

 // 第一个class类的集合
 List<Class<?>> classes = new ArrayList<Class<?>>();
 // 是否循环迭代
 boolean recursive = true;
 // 获取包的名字 并进行替换
 String packageDirName = packageName.replace(&#39;.&#39;, &#39;/&#39;);
 // 定义一个枚举的集合 并进行循环来处理这个目录下的things
 Enumeration<URL> dirs;
 try {
 dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
 // 循环迭代下去
 while (dirs.hasMoreElements()) {
  // 获取下一个元素
  URL url = dirs.nextElement();
  // 得到协议的名称
  String protocol = url.getProtocol();
  // 如果是以文件的形式保存在服务器上
  if ("file".equals(protocol)) {
  // 获取包的物理路径
  String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
  // 以文件的方式扫描整个包下的文件 并添加到集合中
  findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes);
  } else if ("jar".equals(protocol)) {
  // 如果是jar包文件
  // 定义一个JarFile
  JarFile jar;
  try {
   // 获取jar
   jar = ((JarURLConnection) url.openConnection()).getJarFile();
   // 从此jar包 得到一个枚举类
   Enumeration<JarEntry> entries = jar.entries();
   // 同样的进行循环迭代
   while (entries.hasMoreElements()) {
   // 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
   JarEntry entry = entries.nextElement();
   String name = entry.getName();
   // 如果是以/开头的
   if (name.charAt(0) == &#39;/&#39;) {
    // 获取后面的字符串
    name = name.substring(1);
   }
   // 如果前半部分和定义的包名相同
   if (name.startsWith(packageDirName)) {
    int idx = name.lastIndexOf(&#39;/&#39;);
    // 如果以"/"结尾 是一个包
    if (idx != -1) {
    // 获取包名 把"/"替换成"."
    packageName = name.substring(0, idx).replace(&#39;/&#39;, &#39;.&#39;);
    }
    // 如果可以迭代下去 并且是一个包
    if ((idx != -1) || recursive) {
    // 如果是一个.class文件 而且不是目录
    if (name.endsWith(".class") && !entry.isDirectory()) {
     // 去掉后面的".class" 获取真正的类名
     String className = name.substring(packageName.length() + 1, name.length() - 6);
     try {
     // 添加到classes
     classes.add(Class.forName(packageName + &#39;.&#39; + className));
     } catch (ClassNotFoundException e) {
     e.printStackTrace();
     }
    }
    }
   }
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 }
 } catch (IOException e) {
 e.printStackTrace();
 }

 return classes;
}
Copy after login

Pass in a package name, it will automatically scan all the following classes

to find the annotated classes

//找也用了Controller注解的类
private List<Class<?>> controllers;

public List<Class<?>> getControllers() {
 if (controllers == null) {
 controllers = new ArrayList<>();
 List<Class<?>> clsList = getAllClass();
 if (clsList != null && clsList.size() > 0) {
  for (Class<?> cls : clsList) {
  if (cls.getAnnotation(Controller.class) != null) {
   Map<Class<?>, Object> map = new HashMap<>();
   controllers.add(cls);
  }
  }
 }
 }
 return controllers;
}
Copy after login

Find the method annotated with RequestMapping and find out the injected parameters

for (Class<?> cls : getControllers()) {
 Method[] methods = cls.getMethods();
 for (Method method : methods) {
 RequestMapping annotation = method.getAnnotation(RequestMapping.class);
 if (annotation != null) {
  String value = annotation.value();//找到RequestMapping的注入value值
  if (value.equals("/about")) {//判断是不是/about,是的话,就调用about(args)方法
  method.invoke(cls.newInstance(), "args"); //第二个参数是方法里的参数
  }
 }
 }
}
Copy after login

In this way, projects written with pure servlets in java projects can It’s very convenient to make your own annotation mapping routing

Summary

The above is the detailed content of Detailed explanation of how Java uses reflection to find classes using specified annotations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template