Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot how to read properties file outside jar

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 boot, we use following configuration in spring.xml to use @value

<context:property-placeholder location="classpath*:*.properties"/>

and in java code like:

@Value("${name}")

private String name;

but in spring boot, i don't know how to do the same in java code.

i have tried following, but not work

@Configuration
@PropertySource(value = "classpath:aaa.properties")
public class AppConfig {
    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
    }
}
like image 534
Benjamin Liu Avatar asked Jan 20 '17 01:01

Benjamin Liu


3 Answers

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

like image 199
Alberto Avatar answered Oct 29 '22 15:10

Alberto


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.

like image 28
Cristian Caram Peñalver Avatar answered Oct 29 '22 13:10

Cristian Caram Peñalver


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:

  1. A /config subdirectory of the current directory.
  2. The current directory
  3. A classpath /config package
  4. 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)
})
like image 24
jayantS Avatar answered Oct 29 '22 15:10

jayantS