how to give classpath for a property file using spring - spring

Resource resource = new ClassPathResource("classpath:src/main/resources/template/datafields.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);

Your problem is that your file is actually not in the application classpath. Looking at your folder paths I am assuming that you have a maven project structure and your properties file is present within resources directory. When your project is compiled, everything inside the resources directory is at the root of the classpath along with your compiled java classes. So you should instead use
Resource resource = new ClassPathResource("template/datafields.properties");
Classpath resource loads resources from the application classpath, so you need to be aware what all directories/jar files are in your classpath and their directory structure to successfully load resources.

Related

Give external path in #Value Spring annotation and Resource

In spring boot application how do I give an external windows path using #Value Spring annotation and Resource
The below example works fine that look into resources folder but I want to give the path outside of application like c:\data\sample2.csv
#Value("classPath:/sample2.csv")
private Resource inputResource;
...
#Bean
public FlatFileItemReader<Employee> reader() {
FlatFileItemReader<Employee> itemReader = new FlatFileItemReader<Employee>();
itemReader.setLineMapper(lineMapper());
itemReader.setLinesToSkip(1);
itemReader.setResource(inputResource);
and if I want to get the value from properties file in annotaion, whats the format to put the path in windows?
i tried these, none of them worked:
in code
#Value("${inputfile}")
in properties file:
inputfile="C:\Users\termine\dev\sample2.csv"
inputfile="\\C:\\Users\\termine\\dev\\sample2.csv"
inputfile="C:/Users/termine/dev/sample2.csv"
inputfile="file:\\C:\Users\termine\dev\sample2.csv"
inputfile="file://C://Users//termine///dev//sample2.csv"
When you use classpath spring will try to search with the classpath even if you provide the outside file path.
so instead of using classpath: you can use file:
Ex.
#Value("file:/sample2.csv") //provide full file path if any
Use the key spring.config.location in properties to set the config location. Spring-boot will by default load properties from the locations, with precedence like below :
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
and apart from this when you start the jar or in application.properties you can provide the location of the config file like :
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
You can serve static files from the local disk, by making the resource(s) "sample2.csv" as a static resource. An easy way to do this is by adding spring.resources.static-locations configuration to your applicaiton.properties file. Example:
spring.resources.static-locations=file:///C:/Temp/whatever/path/sample2.csv",classpath:/static-files, classpath:/more-static-resource
When I did this in one of the projects, I was able to access the file form the browser using localhost:8080/sample2.csv.

giving Freemarker resource folder in deployed application for spring boot

I'm trying to give resource folder for Freemarker template below is my bean config
Configuration freeMarkerConfig() throws IOException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
cfg.setDirectoryForTemplateLoading(new ClassPathResource("ftl").getFile());
return cfg;
}
and I have ftl folder in the resources folder
-- java
-- resources
-- ftl
-- template.ftl
while deploying it works fine in my local machine but fails in my docker container with exception
java.io.FileNotFoundException: class path resource [ftl] cannot be resolved to absolute file
path because it does not reside in the file system: jar:file:!/BOOT-INF/classes!/ftl
I need to keep ftl in the resources folder and give the directory path to configuration. I don't know how to debug further.
Property spring.freemarker.template-loader-path is available.
spring.freemarker.template-loader-path=classpath:/ftl/
Or, use setTemplateLoaderPath("classpath:/ftl/") instead of setDirectoryForTemplateLoading().
I found the issue after repeated searching. Issue is because of this
Classpath resource not found when running as jar

Spring Boot how to add another resources folder in test folder

Is it possible to add another resources folder in test folder, which will also be on the classpath?
I need it because I don't want to add application-test.properties file in default resources folder because it belongs in test folder.
I tried to add folder manually but it does not work.
I soloved this problem, in Intellij IDEA by:
Right clicking on the project -> Projectu structure,
and I marked newlycreated folder as Resources file.
It is gradle project or maven? If you have gradle just add the line below to the build.gradle file:
ext {
resourcesDir = projectDir.path + "/other/resources"
}
where the /other/resources is your dedicated resource folder

Spring configuration error WEB-INF/servlet-context.xml FileNotFoundException

I have a very simple spring test app. But I get exception even though everything seems to be on order. I might be missing something. Please check the pic to see the project structure and web.xml file contains as well as exception:-
efinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/servlet-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/servlet-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
There are two kinds of resources in a servlet environment:
servlet resources - files uner the root of the web application (loaded via ServletContext)
classpath resources - resources on web application's classpath (loaded via ClassLoader)
When Spring is supposed to load its configuration it needs to know which mechanism to use.
classpath:foo/bar.xml - will load as classpath resource
checking WEB-INF/classes, contents of WEB-INF/lib/*.jar and other shared servlet container's classpath locations
when using maven and its project structure, all files from src/main/resources will be placed on classpath
foo/bar.xml - will load as servlet resource
when using maven and its project structure the src/main/webapp folder is the root of your application
TL;DR As I wrote in the comment, either remove classpath: prefix when referencing XML file or move your XML file to src/main/resources and remove the WEB-INF part.

Spring Boot and external configurations

I am trying to make a Spring Boot application. Everything is fine once I deploy to the fat jar file with everything contained in it. But, what I actually want is the configuration files to be located externally. for example I have the following directory structure:
bin - contains startup and shutdown scripts
conf - all configurations. i.e. application.properties, logback.xml i18n.properties
logs - log files
libs - app.jar
If I use this directory structure and execute the jar using
java -cp ./conf -jar ../libs/app.jar
then the properties in the conf directory are not loaded or recognized. Is there a better way to do this maintaining the directory structure above? Or, what is the alternative/best practice?
Boot external config is what you are looking for.
Especially it mentions:
SpringApplication will load properties from application.properties
files in the following locations and add them to the Spring
Environment:
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
So I would say adding the config folder on classpath is good step. Them it should find application.properties and load it automatically.
For different config files I use:
#Configuration
#EnableAutoConfiguration
#PropertySource({
"classpath:path/some.properties",
"classpath:another/path/xmlProperties.xml"
})
public class MyConfiguration {
// ...
}
Edit:
As Dave pointed out (Thank Dave!) there is either -cp or -jar, so you can't add it to classpath like that. But there are options. This should help you to solve the problem: Call "java -jar MyFile.jar" with additional classpath option.
Additionally #PropertySource doesn't require the resources to be classpath resources if I'm not mistaken.
It should also be mentioned that there is a spring.config.location parameter that allows one to specify a file system / classpath location for externalized configuration files. This is documented in the following section of the Spring Boot reference guide:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Resources