spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.31k stars 40.71k forks source link

Spring boot autowire bean null issue #43241

Closed AnithaPerumalsamy closed 1 day ago

AnithaPerumalsamy commented 1 day ago

We are basically trying to acheive password change without restart of service as defined in https://blog.jdriven.com/2021/06/configure-hikari-connection-pool-when-using-rds-iam/ Implemented as shown below in the snippet I have to call the CustomDatasource 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();
}

}

@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

bclozel commented 1 day ago

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.