I have a line of code that is : File file = new File(getFile())
in a java class HandleData.java
Method - getFile()
takes the value of the property fileName
. And fileName
is injected through application_context.xml
with a bean section of the class - HandleData as below:
<bean id="dataHandler" class="com.profile.transaction.HandleData">
<property name="fileName" value="DataFile.xml"></property>
</bean>
I build the project successfully and checked that - DataFile.xml
is present in WEB-INF/classes
. And the HandleData.class is present in WEB-INF/classes/com/profile/transacon
But when I run it it throws me filenotfound exception.
If I inject the absolute path (C:\MyProjectWorkspace\DataProject\target\ProfileService\WEB-INF\classes\DataFile.xml
it finds the file successfully.).
Could someone help in figuring out the proper path to be injected so that the file is taken from the classpath ?
While injecting a File
is generally the preferred approach, you can also leverage Spring's ResourceLoader for dynamic loading of resources.
Generally this is as simple as injecting the ResourceLoader
into your Spring bean:
@Autowired
private ResourceLoader resourceLoader;
Then to load from the classpath:
resourceLoader.getResource("classpath:myfile.txt");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With