Java 扫描指定包下面的类

思路重点

String f = Thread.currentThread()
.getContextClassLoader()
.getResource(packName.replace(“.”, “/”))
.getFile();

重点是上面那行代码,它是获取编译后根目录的地址。

  • 比如在eclipse
    它获取到的地址其实就是bin的地址,查看项目下面的.classpath就知道它指向的是bin
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
		<attributes>
			<attribute name="module" value="true"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="lib" path="E:/eclipse/jar/mybatis-3.5.4.jar"/>
	<classpathentry kind="lib" path="E:/eclipse/jar/mysql-connector-java-8.0.30.jar"/>
	<classpathentry kind="lib" path="E:/eclipse/jar/fastjson2-2.0.19.jar"/>
	<classpathentry kind="output" path="bin"/>
</classpath>

  • idea
    .ideamisc.xml中就可以找到编译文件输出到out下面,所以上面那行代码就是拿到out这个文件夹的路径。
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
  </component>
</project>

代码

class Main{
	// 扫描指定包下的类
	public Map<String, Method> scanPack(String packName) throws ClassNotFoundException {
		Map<String, Method> hashMap = new HashMap<>();
		String f = Thread.currentThread()
				.getContextClassLoader()
				.getResource(packName.replace(".", "/"))
				.getFile();
		// 队列
		Deque<File> folder = new ArrayDeque<>();
		folder.offer(new File(f));
		while (!folder.isEmpty()) {
			File file = folder.poll();
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; ++i) {
					if (files[i].isDirectory()) folder.offer(files[i]);
					else handlerClass(files[i], packName, hashMap);
				}
			} else {
				handlerClass(file, packName, hashMap);
			}
		}
		return hashMap;
	}
	
	// 获取到文件,加载class。
	private void handlerClass(File file, String packName, Map<String, Method> hashMap) throws ClassNotFoundException {
		String name = packName + "." + file.getName().replace(".class", "");
		Class<?> clazz = Class.forName(name);
		if (clazz != null) {
			Method[] methods = clazz.getMethods();
			for (int i = 0; i < methods.length; ++i) {
				if (methods[i].isAnnotationPresent(com.org.util.Set.class)) {
					methods[i].setAccessible(true);
					hashMap.put(name + "." + methods[i].getName(), methods[i]);
				}
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BeanDefinitionRegistryPostProcessor 是 Spring Framework 中的一个接口,用于在应用程序上下文加载bean定义之前对BeanDefinition进行修改或自定义。它提供了一种扫描的方式来注册bean定义。 要实现BeanDefinitionRegistryPostProcessor 扫描,您可以使用ClassPathBeanDefinitionScanner 扫描指定路径,并将扫描到的注册为bean定义。 下面是一个示例代码: ```java import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.context.annotation.Configuration; import org.springframework.core.type.filter.AnnotationTypeFilter; @Configuration public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { private String basePackage = "com.example.package"; @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { // 创建一个 ClassPathBeanDefinitionScanner 对象 ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry); // 设置自定义的 BeanNameGenerator scanner.setBeanNameGenerator(new MyBeanNameGenerator()); // 添加自定义的过滤器,这里使用了 AnnotationTypeFilter 作为示例 scanner.addIncludeFilter(new AnnotationTypeFilter(MyAnnotation.class)); // 扫描指定包下,并将它们注册为bean定义 scanner.scan(basePackage); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { // 可以在这里进行一些其他的操作 } private class MyBeanNameGenerator implements BeanNameGenerator { // 自定义的 BeanNameGenerator 实现 } } ``` 在上面的示例中,我们实现了 BeanDefinitionRegistryPostProcessor 接口,并覆盖了 postProcessBeanDefinitionRegistry 方法。在该方法中,我们创建了一个 ClassPathBeanDefinitionScanner 对象,并设置了一些自定义的配置,例如路径、BeanNameGenerator 和过滤器。最后,我们调用 scanner 的 scan 方法来扫描指定包下,并将它们注册为bean定义。 请注意,这只是一个示例代码,并不是完整的实现。在实际使用时,您可能需要根据自己的需求进行适当的修改和扩展。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值