Mark McLaren
Mark McLaren

Reputation: 11540

Injecting bean dependencies with Spring Java based configuration

I'm trying to understand Spring Java based configuration. Typically I might have an XML configuration containing something like:

<context:property-placeholder location="jdbc.properties"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
  p:driverClassName="${jdbc.driverClassName}"
  p:ur="${jdbc.url}"
  p:username="${jdbc.username}"
  p:password="${jdbc.password}"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
        p:dataSource-ref="dataSource" />

My effort to produce an equivalent but using Spring Java based configuration has stalled. I'm not sure what to do when trying to inject my dataSource into a jdbcTemplate:

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

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

  @Bean(name = "dataSource")
  public DataSource dataSource(
    @Value("${jdbc.driverClassName}") final String driverClassName,
    @Value("${jdbc.url}") final String url,
    @Value("${jdbc.username}") final String username,
    @Value("${jdbc.password}") final String password) {
    BasicDataSource datasource = new BasicDataSource();
    datasource.setDriverClassName(driverClassName);
    datasource.setUrl(url);
    datasource.setUsername(username);
    datasource.setPassword(password);
    return datasource;
  }

  @Bean(name = "jdbcTemplate")
  public JdbcTemplate jdbcTemplate() {
    return new JdbcTemplate(
      /* Property 'dataSource' is required and needs to be referenced
       * What goes here?
       */
    );
  }

}

My guess is there isn't a direct equivalent and I may need to go about things in a subtly different way?

Upvotes: 2

Views: 2656

Answers (2)

Naman Gala
Naman Gala

Reputation: 4692

As JdbcTemplate contains JdbcTemplate(DataSource dataSource) constructor.

You can try this way

@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());
    }
}

Upvotes: 5

Mithun
Mithun

Reputation: 8077

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

In the DAO Class:

private JdbcTemplate jdbcTemplate;

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

Upvotes: 1

Related Questions