Spring data from multiple data sources from multiple files

I have 2 (or more) configuration properties files located in a project, and I want to upload them for different data sources.

I tried to do the following:

@Bean
@ConfigurationProperties(locations = {"#{myconfigroot.getRootFolder()}/datasource1.properties"}
public static DataSource getFirstDatasource() {
    return DataSourceBuilder.create().build();
}

But obviously, this will not work because the ConfigurationProperties annotation location property does not go through spEL. (Or maybe I'm writing this incorrectly?) Myconfigroot.getRootFolder () is a static method that returns the path to datasource1.properties.

I ask for advice. Thank.

===== Edited =======

I believe this is a common problem when someone wants their application to want to download various configuration files. For some reason, the location and file name cannot be placed in the startup script or on the command line, or the path can only be determined at run time, which requires spring to load them during the creation of the bean.

I tried using PropertySourcePlaceHolderConfigurer but it doesn't seem to work.

Can anyone share some lights?

+4
source share
2 answers

The latest Spring boot (version 1.3.5) does not support SpEL in this case.

See JavaDoc @ConfigurationProperties annotations

, {@code @Value} SpEL , .

Spring :

, database.properties -, - .

username=mike
password=password

, POJO- :

@Component
@ConfigurationProperties(locations = "myConfiguration")// myConfiguration is customized placeholder
public class MyProperties{
   String username;
   String password;
   //Getters, Setters…
}

, StandardEnvironment:

public class MyEnvironment extends StandardEnvironment {
   @Override
   public String resolvePlaceholders(String location) {
      if (location.equals("myConfiguration")) {
         //Whatever you can do, SpEL, method call...
         //Return database.properties path at runtime in this case
         return getRootFolder() + "datasource.properties"; 
      } else {
         return super.resolvePlaceholders(text);
      }
   }
}

, main Spring:

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
    new SpeedRestApplication()
        .configure(new SpringApplicationBuilder(SpeedRestApplication.class).environment(new MyEnvironment()))//Replace default StandardEnvironment
        .run(args);
   }
}

Spring MyProperties bean .properties. MyProperties bean beans .

, !

+2

, , :

public class DatasourcePostProcessor implements EnvironmentPostProcessor {
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Properties p = new Properties();
        p.load(new FileInputStream(new File(getRootFolder() + "/datasource1.properties")));
        Map<String, Object> propMap = new HashMap<>();
        for (Map.Entry<Object, Object> entry : p.entrySet()) {
            propMap.put(entry.getKey().toString(), entry.getValue());
        }
        MapPropertySource source = new MapPropertySource("datasource1", propMap);
        environment.getPropertySources().addLast(source);
    }
}

spring.:

 org.springframework.boot.env.EnvironmentPostProcessor=com.myorg.test.DatasourcePostProcessor

, , , . Google, :

: https://github.com/spring-projects/spring-boot/issues/4595

, : Spring @ConfigurationProperties

+1

All Articles