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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay