2017-10-20 297 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

相關問題