Rectangle 27 1

Injecting bean dependencies with Spring Java based configuration?


private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(final DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}

You don't need to create jdbcTemplate in the configuration. You could create it in the DAO class.

Note
Rectangle 27 1

Injecting bean dependencies with Spring Java based configuration?


private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(final DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}

You don't need to create jdbcTemplate in the configuration. You could create it in the DAO class.

Note
Rectangle 27 1

Injecting bean dependencies with Spring Java based configuration?


@PropertySource("classpath:jdbc.properties")
@Configuration
public class Config {

    @Autowired
    private Environment env;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean(name = "dataSource")
    public DataSource dataSource() {
        BasicDataSource datasource = new BasicDataSource();
        datasource.setDriverClassName(env.getProperty("jdbc.driverClassName"));

//      Similarly other values
//      datasource.setUrl(url);
//      datasource.setUsername(username);
//      datasource.setPassword(password);
        return datasource;
    }

    @Bean(name = "jdbcTemplate")
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }
}
JdbcTemplate(DataSource dataSource)

You can try this way

Note