Issue
I have a spring boot application with datasource configured on external tomcat, How can I get the driverClassName of the datasource configured by the external tomcat into my spring boot application.
I tried to write some code as
PoolProperties poolProps = new PoolProperties();
Properties dbProperties = poolProps.getDbProperties();
String databaseUrl = dbProperties.getProperty("databaseUrl");
But the databaseUrl in the above code returns null. Can anyone suggest How can I get driverClassName into my java code?
Solution
You can retrieve the JavaBean properties of your datasource through reflection. Just use one of the many tools that help with introspection.
Since you already have spring-beans
as dependency you can use:
final BeanWrapper accessor = PropertyAccessorFactory.forBeanPropertyAccess(dataSource);
final String url = String.valueOf(accessor.getPropertyValue("url"));
You can also use reflection directly, but it becomes easily difficult to read:
try {
final String value = String.valueOf(dataSource.getClass().getMethod("getUrl").invoke(dataSource));
} catch (ReflectiveOperationException e) {
...
}
Answered By - Piotr P. Karwasz