0

I'm working on to retrieve a password using cyberarc API before configuring data source in SpringBoot.

I tried to call password retrieval API in main class before run() but did not help, here is my code:

//programmatic way of building a data source.
@Configuration
@PropertySource(value = {
    "classpath:application.properties"
})
public class DataSourceConfig {

    @Autowired
    Environment environment;

    private CyberarcPassword CyberarcPassword;

    private static final Logger log = LoggerFactory.getLogger(DataSourceConfig.class);

    @Bean
    public DataSource dataSource() {    
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();    
        dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));    
        dataSource.setUrl(environment.getProperty("spring.datasource.url"));    
        dataSource.setUsername(environment.getProperty(CyberarcPassword.getUsername()));    
        dataSource.setPassword(environment.getProperty(CyberarcPassword.getPassword()));

        return dataSource;    
    }
}

I am expecting to set the CyberarcPassword before calling data source bean.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
  • This might help: https://stackoverflow.com/questions/7868335/spring-make-sure-a-particular-bean-gets-initialized-first – geneqew May 14 '19 at 09:57
  • Can you just make `CyberarcPassword CyberarcPassword` a parameter of your `dataSource` method? I think that might work. I've never done this for a dataSource bean, but I don't see why it wouldn't work and would be a lot easier that having to set dependencies and initialization order and all of that. – CryptoFool May 15 '19 at 04:17
  • I just realized that your CyberarcPassword def isn't annotated with @Autowired. How do you expect that to get set at all? Again, this should be solved by making it a parameter to your `dataSource()` method. – CryptoFool May 15 '19 at 04:22
  • I have annotated CyberarcPassword and marking it as primary bean while forming getCyberarcPassword () , in addition used @DependsOn("CyberarcPassword") which resolves the problem in datasourceconfig – Anshuman Sathe May 17 '19 at 12:44

0 Answers0