DEV Community

Anitha Perumalsamy
Anitha Perumalsamy

Posted on

Spring boot Autowire issue

  1. We are basically trying to achieve password change without restart of service as defined in https://blog.jdriven.com/2021/06/configure-hikari-connection-pool-when-using-rds-iam/
  2. Implemented as shown below in the snippet
  3. I have to call the Custom Datasource with new keyword as we have to create new connection as its Multitenant enabled service

serviceA bean is null in getPassword() method in CustomDatasource

@Service
public class CustomDatasource extends HikariDataSource {

@Autowired
private ServiceA serviceA;

 @Autowired
public CustomDatasource(HikariConfig hikariConfig) {
    super(hikariConfig);
}

 @Override
public String getPassword() {
   serviceA.someMethod();
}
Enter fullscreen mode Exit fullscreen mode

}

@Service
public class HikariDataSourceBuilder {

CacheStore.getCache().setDataSource(new CustomDatasource(getHikariConfig(tenantId)).

public HikariConfig getHikariConfig(String tenant) {
return hikariConfig;
}
}

@Service
public class ServiceA {

public String someMethod() {
return "";
}
}

Options Tried

  • Made CustomDatasource as @scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE), so that everytime new instance of the class is returned, and made changes to get the bean from Application context in HikariDataSourceBuilder.java class
  • Tried few other options by defining Postcontruct in CustomDatasource and also by changing the way ServiceA set in CustomDatasource instance of the clas
Retry later

Top comments (0)

Retry later
Retry later