Resources lay on the classpath of Java application, not on the filesystem. You almost never will have IOException, unless you made a mistake in your code. Loading of resources is closer to class import than to reading of file from the filesystem. You don’t surround your imports with try-catch, do you ?
I am working at Elasticsearch expert company and in my daily life, I see a lot of JSON: requests, responses, mappings, settings, etc. In 90% of cases, the functionality of software we make involves the manipulation of JSON. And it’s pretty common for us to have JSON files inside resources of Java code.
One day, after writing code to load and parse JSON for 100500th time I understood one thing: reading content of resources in Java is harder than it should be!
Just take a look. Let’s assume we have a resource com/adelean/junit/jupiter/resource.txt containing text:
“The quick brown fox jumps over the lazy dog.”
This is one of existing ways to load its content:
Looks scary, doesn’t it? Need to open and close multiple streams/readers, handle IOException, all just to get text content.
This Stackoverflow topic (https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder), and many others, explain how to load resources. You can give it a look, or better, to skip it and continue to read this article.
Luckily for us, there are great libraries like google Guava, that significantly simplify the task. Here is the same operation made with Guava:
Even if it’s simpler, it is still too complicated for a task that probably must be done with one line. And we still have to handle exceptions.
I always found it surprising why we must handle exceptions while loading java resources. While exception handling is justified for the files, because file can be absent, or doesn’t have right permissions, the situation is different for resources.
*Resources lay on the classpath of Java application, not on the filesystem. *You almost never will have IOException, unless you made a mistake in your code. Loading of resources is closer to class import than to reading of file from the filesystem. You don’t surround your imports with try-catch, do you ?
What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).
Spring IoC container is the core of the Spring Framework.In spring-based applications, objects live inside a spring containerThe container instan.
This video on "Java Spring AOP Tutorial For Beginners" will help the learners with a detailed briefing of the Spring AOP concepts. This Spring AOP Tutorial will include a practical session for a better learning experience.
Step by Step to your First Spring App
A bunch of examples of using jdbcTemplate in Spring framework (Java)