My application in main method reads parameters and runs Spring application. My Configuration with DataSource bean inside needs to read one of the parameter to use in initialization of datasource.
Application class
public static void main(String[] args) throws ParseException, IOException {
SpringApplication application = new SpringApplication(BpMaterializedViewToolApplication.class);
CommandLine cmd = getCmd(args);
String credentialsPropertyPrefix = cmd.getOptionValue(CREDENTIALS_PROPERTY_PREFIX_ARGUMENT);
application.run(credentialsPropertyPrefix);
}
@Override
public void run(String... args) {
someMethod(args[0]);
}
Configuration class
@Configuration
public class DataSourceConfig {
@Value("${credentialsPropertyPrefix}") // doesn't work, but need something like this
private String credentialsPropertyPrefix;
@Autowired
private Environment env;
@Bean
@Primary
public DataSource getDataSource() {
String username = env.getProperty(credentialsPropertyPrefix ".username"); // credentialsPropertyPrefix needs to be present there
...
}
So I need my DataSource getDataSource()
to properly read that credentialsPropertyPrefix
argument and use in datasource.
CodePudding user response:
Try
System.setProperty("credentialsPropertyPrefix",cmd.getOptionValue(CREDENTIALS_PROPERTY_PREFIX_ARGUMENT));
application.run();