java - 如何在你的 spring 项目中计算所有具有 @Controllers 属性的类
问题描述
有一次面试官问我这个问题,我也答不上来。我也在谷歌上搜索过,但我没有得到任何正确的答案。
解决方案
请尝试下面给定的代码。
Map<String,Object> beans = ctx.getBeansWithAnnotation(Controller.class);
System.out.println(beans.size());
或者你可以用反射库试试这个。下面给出的片段可以在整个项目中搜索。
maven依赖:
org.reflections 反射 0.9.10
import org.reflections.Reflections;
public class FindAnnotation {
public static void main(String[] args) {
System.out.println("Scanning using Reflections:");
Reflections ref = new Reflections("com.some.package");
for (Class<?> cl : ref.getTypesAnnotatedWith(Controller.class)) {
//count
}
}}
推荐阅读
- python - 如何遍历目录中的 .csv 文件,执行计算并将结果保存到新数据框?
- sql - 使用 SQL 创建 2 个带有条件的累积和“墨盒”
- java - Java 大数的数计算问题
- azure - Azure 中的 Redisgraph
- c - 将C中的字符串拆分为不同的变量
- python - Python Tkinter: RecursionError: maximum recursion depth exceeded
- amcharts4 - amcharts 图例标签丢失
- android - 从字符串格式转换后无法打印时间
- angular - Angular 8 与 .NetCore WebAPI 将对象作为参数传递
- node.js - 有没有办法在客户端访问和连接到 Wi-Fi 接入点?