Spring Download - Add External Property Files

I have a simple MVC application in SpringBoot created using java-config (I don't have web.xml).
This application has a JPA database connection. Everything has been fine so far, but now I have to move db.properties from inside the WAR to the location indicated by the OS variable ("CONFIG_LOCATION").

In the spring doc , not much is written about this. You can only say that this is possible, but how should I install this in my Spring application?
I believe this should be done before initialization.

Then I see only two options:
- SpringApplication - somewhere there is a place where I have to insert the file location from the OS variable, but I can not find it,
- some annotation that will replace the OS variable and add files from it to the Spring context before creating the EntityManager.

I am open to suggestions on how to do this.

+1
source share
5 answers

Ok, I found a way.

I created a class that returns PropertySourcesPlaceholderConfigurer.
In this PSPC, I get the OS variable and scan all the files in this place.
After scanning, I added all the properties extension files to PSCS as locations.
The @ Bean method, and the class is @Configuration. After that, everything works fine :)

imports...

@Configuration
public class AppServiceLoader {
    @Bean(name = "propLoader")
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();

        String mainConfigPath = StringUtils.isNotEmpty(System.getenv("CONFIG_LOCATION"))
                ? System.getenv("CONFIG_LOCATION") : System.getProperties().getProperty("CONFIG_LOCATION");

        File configFolder = new File(mainConfigPath);

        if(configFolder.isDirectory()) {
            FilenameFilter ff = new FilenameFilter() {

                @Override
                public boolean accept(File file, String string) {
                    return string.endsWith(".properties");
                }
            };
            File[] listFiles = configFolder.listFiles(ff);
            Resource[] resources = new Resource[listFiles.length];
            for (int i = 0; i < listFiles.length; i++) {
                if(listFiles[i].isFile()) {
                    resources[i] = new FileSystemResource(listFiles[i]);
                }
            }
            pspc.setLocations(resources);
        }

        pspc.setIgnoreUnresolvablePlaceholders(true);
        return pspc;

    }
}
0
source

@PropertySource - ( ). Java 8 , ! , :

@SpringBootApplication
@PropertySource("classpath:/db.properties")
@PropertySource(ignoreResourceNotFound = true, value = "file:${MY_APP_HOME}/db.properties")
@PropertySource(ignoreResourceNotFound = true, value = "file:${user.home}/.myapp/db.properties")
@ComponentScan("com.myorg")
public class Application {
     // ....
}

, MY_APP_HOME, . , ignoreResourceNotFound true.

. src/main/resources/db.properties. , .

. Resolving ${...} placeholders within @PropertySource resource locations javadoc.

+2

spring -boot, jar war - spring.config.location.

:

$ java -jar myproject.jar --spring.config.location=/opt/webapps/db.properties

+1

, Spring .
:

@Configuration
@PropertySource("file:${user.dir}/your_external_file.properties")
public class TestConfig {
  @Autowired
  Environment env;
}

${user.dir} ${user.home}.

+1

@PropertySource. , .

: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

:

@Configuration @PropertySource("classpath:/com/myco/app.properties") public class AppConfig { ...

0

Source: https://habr.com/ru/post/1017048/


All Articles