通过Spring获取指定路径下的class

本文介绍了如何在Spring框架中实现指定包的class文件扫描。通过分析Spring的ComponentScanBeanDefinitionParser和ClassPathBeanDefinitionScanner,重点探讨了使用ClassPathResource和PathMatchingResourcePatternResolver的getResources方法来获取类路径,从而实现类的扫描。这是一个学习Spring资源处理和类路径扫描的实战案例。
摘要由CSDN通过智能技术生成

实战背景:

         博主在上篇文章Spring启动后加载类或启动类方法大致聊到背景,是为了开发一个自定义注解,根据注解生成说明的菜单树,用于前端编辑。这就要求Spring启动后就扫描特定的包,然后解析自定义注解,“填入”菜单树的业务领域对象中。这个细节将在另外一篇文章具体解说——有关自定义注解和解析实战。

        我们知道,虽然JAVA有提供几个操作资源的接口,但是不是很给力。而Spring框架提供了很优秀且很丰富的资源获取和处理接口,包括file/url/class等资源。但是,有关指定包的class文件扫描,具体解说的还是比较少的。我只能根据Spring component-scan的流程获取些灵感。所以,先大概看看component-scan是怎么处理的

 

component-scan解析流程:

        通过Eclipse使用快捷键Ctrl+Shift+T,根据直觉输入ComponentScan,可以定位到唯一一个类ComponentScanBeanDefinitionParser,应该就是这个类处理component-scan了。查看其方法,可以发现只有一个公共方法,如下

 

public BeanDefinition parse(Element element, ParserContext parserContext) {
	String[] basePackages = StringUtils.tokenizeToStringArray(element.getAttribute(BASE_PACKAGE_ATTRIBUTE),
				ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);

	// Actually scan for bean definitions and register them.
	ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
	Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
	registerComponents(parserContext.getReaderContext(), beanDefinitions, element);

	return null;
}
Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);

返回值是BeanDefinitionHolder,应该就是这个方法,继续进入ClassPathBeanDefinitionScanner类,看到ClassPathBean应该就没错了,对应处理方法

 

 

protected Se
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,你可以使用`Resource`和`InputStream`来读取指定路径的文件,并将文件内容返回为文件流。以下是一个示例代码: ```java import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @RestController public class FileController { @GetMapping("/file/{filename}") public ResponseEntity<Resource> getFile(@PathVariable String filename) throws IOException { // 指定文件路径 String filePath = "/path/to/your/file/" + filename; // 创建文件资源对象 Resource resource = new FileSystemResource(filePath); // 检查文件是否存在 if (!resource.exists()) { return ResponseEntity.notFound().build(); } // 获取文件输入流 InputStream inputStream = resource.getInputStream(); // 返回文件流 return ResponseEntity.ok() .contentLength(Files.size(Paths.get(filePath))) .header("Content-Disposition", "attachment; filename=\"" + filename + "\"") .body(new InputStreamResource(inputStream)); } } ``` 在上述代码中,你需要将`/path/to/your/file/`替换为实际存储文件的路径。当访问`/file/{filename}`时,该方法会检查文件是否存在,然后将文件内容返回为文件流。 请注意,上述示例假设文件路径是绝对路径。如果你需要处理相对路径或基于路径的文件,请使用适当的方法获取资源。 希望这对你有所帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值