我正在编写一个 API,它提供基于类注释的功能。API 将被导入,因此它不知道客户端的包。所以问题是:如何在不知道包名的情况下扫描类?
我找到了这些方法:
Package.getPackages()
但它只返回包的一个子集。
ClassPathScanningCandidateComponentProvider.findCandidateComponents(basePackageName)
但 is 不接受通配符,例如*
or .
。
编辑对@Rakesh 的回答
@Component
public class Application {
@Autowired
public ApplicationContext appContext;
public static void main(String[] args) {
Application a = new Application();
String[] beanNames=a.appContext.getBeanNamesForAnnotation(Run.class);
for (String beanName : beanNames) {
System.out.println(beanName);
Object bean = a.appContext.getBean(beanName);
Class<? extends Object> class1 = bean.getClass();
System.out.println(class1);
}
}
}