Spring boot how to read properties file outside jar

93,246

Solution 1

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

Solution 2

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.

Solution 3

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)
})

Solution 4

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

Solution 5

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

Share:
93,246

Related videos on Youtube

Benjamin Liu
Author by

Benjamin Liu

Updated on April 07, 2020

Comments

  • Benjamin Liu
    Benjamin Liu about 4 years

    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();
        }
    }
    
  • Benjamin Liu
    Benjamin Liu over 7 years
    Thanks, this offer great value
  • Benjamin Liu
    Benjamin Liu over 7 years
    Do you know if i have multiply properties files, how to read it in separate config class?
  • Alberto
    Alberto over 7 years
    Are you referring to multiple external files?
  • Benjamin Liu
    Benjamin Liu over 7 years
    yes, such as a file for db settings, a file for redis settings and other files for other settings. we want to split different settings.
  • SashikaXP
    SashikaXP almost 7 years
    This appears to be a more versatile solution. If you want to set multiple locations using setLocations method instead.
  • milosdju
    milosdju almost 6 years
    Can you show how to use environment variable in order to define configuration location?
  • Benjamin Liu
    Benjamin Liu almost 5 years
    the location in @ConfigurationProperties(prefix = "asdf", locations = "file:conf/aaa.properties") can be defined in application-{profile}.yml files. and change to locations = "${configFilePath}". And for dev its value can be classpath:*.properties, for prod its value canbe file:*.properties.
  • Xiaokun
    Xiaokun over 3 years
    It doesn't work for me. My configuration is @PropertySource(value = {"classpath:config/springConfig.properties"}) . It only works when I compile it to jar.