Gulsheharabano Shaikh
Gulsheharabano Shaikh

Reputation: 60

How do I get a dynamically configured DataSource property in a Spring Boot Library Project at runtime?

I have a multi-module project, consider A as my library project and B as an implementation project that imports A. Now my DataSource is configured in B using @Configuration and getDataSource() method dynamically. I want to access a property from this dynamically configured DataSource in project A from project B. I tried using @ConfigurationProperties but it is not possible. Is there any way to achieve this?

Thanks in advance!

Upvotes: 0

Views: 385

Answers (3)

Grzegorz
Grzegorz

Reputation: 197

You could try inject Environment into this service:

private Environment env;

and then

env.getProperty("b.datasource.url");

or inject ApplicationContext:

private ApplicationContext context;

and then get whole bean:

context.getBean(DataSource.class);

or by name:

context.getBean("BDataSource"); // <- you need to know proper bean name

Upvotes: 0

Keshavram Kuduwa
Keshavram Kuduwa

Reputation: 1060

While configuring your DataSource, add a System property like System.setProperty("key", value); and access it in your library like System.getProperty("key");

Upvotes: 1

Ayush v
Ayush v

Reputation: 371

Consider using annotations like @Primary or @Qualifier.

Upvotes: 0

Related Questions