Introduction: In a SpringBoot-built project, in some cases, you need to read some file contents on your own. How can you read the file contents in a quick and easy way?(
Read based on ApplicationContext
How to get an ApplicationContext reference in Spring Bean:
@Component public class MyBean implement ApplicationContextAware { private static ApplicationContext context; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static ApplicationContext getApplicationContext() { return context; } .......... }
See my previous article on how to dynamically inject the ApplicationContext: Quick Guide to Dynamic Injection of Bean Instances in Spring
Once you have an instance of the ApplicationContext, you can dynamically read the contents of the file based on the applicationContext:
Use fileSystem
Resource resource = appContext.getResource("file:d:\testing.jpg");
Use url path
Resource resource = appContext.getResource("url:http://www.test.com/testing.txt");
classpath-based
Resource resource = appContext.getResource("classpath:resources/common/test.txt");
After you get the resource object, you can get the object of the file so that you can read the contents of the file.
Based on ResourceLoader
A ResourceLoader interface is specifically provided in Spring to facilitate path access to classpath s.(
Based on Ware mode, ResourceLoader is injected as follows:
public class MyBean implements ResourceLoaderAware { private ResourceLoader resourceLoader; public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public Resource getResource(String location){ return resourceLoader.getResource(location); } ....... }
The second way is to create a corresponding instance in SpringBoot directly based on @Bean, as follows:
..... @Bean public ResourceLoader createResourceLoader() { return new DefaultResourceLoader(); } .....
The ResourceLoader object can be created in either way.
Once you get an object instance of ResourceLoader, you can obtain configuration information based on the following:
Resource resource = resourceLoader.getResource("classpath:resources/config.xml");
Another way to read the contents of a file directly is:
String content = IOUtil.toString(resourceLoader.getResource(
"classpath:resources/test.txt").getInputStream());
IOUtil is an IO class library package in the Apache common project that can directly add dependencies in the following ways:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
What is a classpath
In the Maven project, all resources files will be copied to the classes directory.The classpath in the Tomcat project is / classes, / lib, and other paths under tomcat.(
For developers, the directory where classes are located is usually the starting point and base path of the classpath path path.
summary
The old saying goes like this: if you want to do good work, you must first use the tools.Good tools and methods can make the work easier, such as getting the most out of the water, completing more tasks with less code, summarizing more analysis, and accumulating more, so as to improve the efficiency.(
About myself
These techniques and summaries come from actual work. Feedback and comments are welcome.(
The article will also be synchronized with the headline number on today's headlines, which can be found by searching for "program gas stations".