in my target folder, there are 2 folders, lib and conf. all the properties files are placed in conf folder, and jars are placed in lib foulder.
previous to spring
I'm a bit confused by the title of the question and the description. Hopefully I won't confuse you even more with my comments.
In general, Spring Boot is VERY opiniated about project structure as well as the binary created. The recomended way (Spring Boot opinion) is to build a jar with all dependencies inside (fat jar). If you need configuration properties defined outside your fat jar (or war if that's what you built), Spring Boot offers many options (see reference 1). I like my apps to point to an external file using the flag (spring.config.location) which can be set with a system property:
java -jar -Dspring.config.location=<path-to-file> myBootProject.jar
Notice that you can do something similar by using an environment variable to define where your external file lives.
I hope this helps!
References: 1. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Actually, the most easiest way is to put application.properties
and your.jar
into the same directory, and just java -jar your.jar
will automatically load this external config file.
And if you are trying to load some context/files from resource folder via ClassPathResource
, when you read the file don't use pathResource.getFile()
, instead use pathResource.getInputStream()
, getFile()
works well within your IDE, however it doesn't work with the standalone jar
.
Found an solution:
first create a class and add @ConfigurationProperties
@ConfigurationProperties(prefix = "asdf", locations = "file:conf/aaa.properties")
public class ASDF {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Please noted in locations, i use file, not classpath.
then in your application class, add @EnableConfigurationProperties
@SpringBootApplication
@EnableConfigurationProperties({ASDF.class, BBB.class})
public class InitialBeanTestApplication {
public static void main(String[] args) {
SpringApplication.run(InitialBeanTestApplication.class, args);
}
}
then you can read config file in the conf folder
Found another solution.
put every configuration in one application.properties files, and in code use @Value("${name}") to read.
and use a assembly file to copy the resource folders' file into target config folder.
and after deploy, just need to change application.properties file in config folder and the run the application.
this because spring boot read application.properties file in follow sequence.
• The /config subdirectory located in the current directory
• The current directory
• A classpath /config package
• The classpath root
but this works for one properties file. not for multiply properties files
I am not sure if you are dealing with the same situation than me, but in my case I have a jar and a *.properties file outside of it. What I did to get the *.properties file located outside the jar was the next:
@Configuration
public class ApplicationContext {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource("application.properties"));
properties.setIgnoreResourceNotFound(false);
return properties;
}
}
When I am setting the location of the application.properties file I created FileSystemResource object, which allow me get the properties.files which is located next to the jar. If your .properties files are in classpath, for example, you can use other classes (like ClassPathResource). You can read other classes that spring offers to get a Resource object under the package org.springframework.core.io. .
I hope this comments helps.
As mentioned in the Spring Boot docs,
SpringApplication will load properties from
application.properties
files in the following locations and add them to the Spring Environment:
- A /config subdirectory of the current directory.
- The current directory
- A classpath /config package
- The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
One way is to simply rename your 'conf' directory to 'config' and it will work without a problem. So there is no need to do extra configuration until and unless you want your properties file at some location other than the 4 mentioned above.
In that case you can define the property source explicitly.
@PropertySource("classpath:config.properties")
and for multiple properties file
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:logging.properties"),
@PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
})