'Initialize datasource bean on condition

So, I need to make an initialization of DataSource by condition. I take all the db-config data from the .env file, where there is a special variable spring_profiles_active. When this variable is equal to "p2" I need to initialize secondDataSource, otherwise not. How do I do this? My configuration:

@Configuration
public class JpaConfig {
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;
    @Value("${jdbc.driverClassName}")
    private String jdbcDriverClassName;

    @Value("${second.jdbc.url}")
    private String secondJdbcUrl;
    @Value("${second.jdbc.username}")
    private String secondJdbcUsername;
    @Value("${second.jdbc.password}")
    private String secondJdbcPassword;
    @Value("${second.jdbc.driverClassName}")
    private String secondJdbcDriverClassName;

    @Bean
    @Primary
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(jdbcDriverClassName);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(jdbcUsername);
        dataSource.setPassword(EncryptionUtil.decryptProperty(jdbcPassword));

        return dataSource;
    }

    @Bean
    @Qualifier("secondDataSource")
    public DataSource secondDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(secondJdbcDriverClassName);
        dataSource.setUrl(secondJdbcUrl);
        dataSource.setUsername(secondJdbcUsername);
        dataSource.setPassword(EncryptionUtil.decryptProperty(secondJdbcPassword));

        return dataSource;
    }
 

    @Bean
    @Primary
    public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); }

    @Bean
    @Qualifier("secondJdbcTemplate")
    public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource secondDataSource) {
        return new JdbcTemplate(secondDataSource);
    }
}

  • update: I try to do this with @Condition annotation, but get an exception java.lang.NullPointerException: null in return activeProfile.equals("p2"). code:
    @Configuration
    @Conditional(SecondJpaConfigCondition.class)
    public class SecondJpaConfig {
    
        @Value("${second.jdbc.url}")
        private String secondJdbcUrl;
        @Value("${second.jdbc.username}")
        private String secondJdbcUsername;
        @Value("${second.jdbc.password}")
        private String secondJdbcPassword;
        @Value("${second.jdbc.driverClassName}")
        private String secondJdbcDriverClassName;
        
        @Bean
        @Qualifier("secondDataSource")
        public DataSource secondDataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
    
            dataSource.setDriverClassName(secondJdbcDriverClassName);
            dataSource.setUrl(secondJdbcUrl);
            dataSource.setUsername(secondJdbcUsername);
            dataSource.setPassword(EncryptionUtil.decryptProperty(secondJdbcPassword));
    
            return dataSource;
        }
        
        @Bean
        @Qualifier("secondJdbcTemplate")
        public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource secondDataSource) {
            return new JdbcTemplate(secondDataSource);
        }
    }
@Component
public class SecondJpaConfigCondition implements Condition {

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Override
    public boolean matches(ConditionContext arg0, AnnotatedTypeMetadata arg1) {
        return activeProfile.equals("p2");
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source