2017-10-20 138 views
0

这里是我的代码:如何注入多个JdbcOperations到Spring测试用例

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/load-BMS-data-job-launcher-context.xml" }) 
public class SimpleJobLaunchFunctionalTests { 

    @Autowired 
    private JobLauncherTestUtils jobLauncherUtils; 

    @Autowired 
    private JdbcOperations jdbcTemplate; 

    @Autowired 
    private JdbcOperations jdbcTemplateBMS; 

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

    @Autowired 
    public void setDataSource(DataSource BMSdataSource) { 
     this.jdbcTemplateBMS = new JdbcTemplate(BMSdataSource); 
    } 

    @Before 
    public void setUp() { 
     jdbcTemplate.update("DELETE from SHADOW_BMS"); 
    // jdbcTemplate.update("DELETE from CMNREF.CNTRCT_EXTRNL_KEY_REF_V"); 
     jdbcTemplateBMS.update("DELETE from CNTRCT_EXTRNL_KEY_REF_V"); 

我想连线两个独立JdbcTemplates对这个测试类内部的两个不同的数据库操作。我不知道如何设置不同的数据源 - 当我尝试调用第二个setDataSource(DataSource BMSdataSource)方法时,我得到一个异常。

我该怎么做?

回答

0

您可以使用name =“template1”和name =“template2”(带注释@Bean(name =“template1”))创建bean。在测试中使用它们如下:

@Qualifier("template1") 
@Autowired 
private JdbcOperations jdbcTemplate1; 

@Qualifier("template2") 
@Autowired 
private JdbcOperations jdbcTemplate2; 
+0

谢谢你 - 我想我不得不调用的setDataSource(DS DS)两次:一次为每个JDBC tmplate ......但他们已经预先有线与数据源。再次感谢。 – JamesD

相关问题