简体   繁体   English

使用基于Spring Java的配置注入Bean依赖项

[英]Injecting bean dependencies with Spring Java based configuration

I'm trying to understand Spring Java based configuration. 我试图了解基于Spring Java的配置。 Typically I might have an XML configuration containing something like: 通常,我可能有一个包含以下内容的XML配置:

<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. 我生产等效但使用基于Spring Java的配置的工作已停滞。 I'm not sure what to do when trying to inject my dataSource into a jdbcTemplate: 我不确定将尝试将dataSource注入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? 我的猜测是没有直接的等价物,而我可能需要以一种微妙的不同方式去做事情?

As JdbcTemplate contains JdbcTemplate(DataSource dataSource) constructor. 由于JdbcTemplate包含JdbcTemplate(DataSource dataSource)构造函数。

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

You don't need to create jdbcTemplate in the configuration. 您无需在配置中创建jdbcTemplate You could create it in the DAO class. 您可以在DAO类中创建它。

In the DAO Class: 在DAO类中:

private JdbcTemplate jdbcTemplate;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM