2016-03-01 99 views
1

我發現這個問題是回答什麼,我一直在尋找: how to pass values dynamically in config fileSpringBatch:動態數據源值

的事情是,當我嘗試它,我有一個例外..

Error creating bean with name 'jobOperator' defined in class path resource [atlentic-Spring-Batch-common.xml]: Cannot resolve reference to bean 'jobExplorer' while setting bean property 'jobExplorer' [...] 
Error creating bean with name 'connex' defined in class path resource [batch-calendar-context.xml]: Error setting property values;[...] Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? 

我試圖讀取一個.ini文件,在那裏我得到數據庫信息,然後我想將它們注入到我的XML數據源配置中。

這裏是我的XML,

<beans:bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
    <beans:property name="driverClassName" value="${DB_DRIVER}" /> 
    <beans:property name="url" 
     value="${DB_PROTOCOL}:@${DB_HOST}:${DB_PORT}:${DB_NAME}" /> 
    <beans:property name="username" value="#{connex.user}" /> 
    <beans:property name="password" value="#{connex.pass}" /> 
</beans:bean> 

<beans:bean id="connex" class="com.sponge.bob.calendar.entity.CustomConnexion"> 
    <beans:property name="dataSource" ref="dataSource" /> 
</beans:bean> 

然後我CustomConnexiob.class,我使用的構造方法實例我attributs(這是不性感,但我開始與SpringBatch):

@Component 
@Scope("step") 
public class CustomConnexion { 
    public String user; 
    public String pass; 
    public String base; 

    @Autowired 
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomConnexion.class); 

    public CustomConnexion() { 
     initConnexion(); 
    } 

    public void initConnexion() { 
     IniReader reader = new IniReader(); 

     setUser(reader.getProperty(Constants.MYCOMMON, Constants.USER)); 
     setBase(reader.getProperty(Constants.MYCOMMON, Constants.BASE)); 
     setPass(reader.getProperty(Constants.MYCOMMON, Constants.PASS)); 
    } 

    /* getters and setters after this line (not printed here but they have the default name */ 
} 

是否有可能通過這種方式獲得此密碼和用戶動態使用,我開始失去主意?

+0

沒有也不會。你的bean也不會被一步作用,xml中的那個不是一步作用域,而是一個單一作用域。接下來'DriverManagerDataSource'是一個單例,並在啓動時創建。如果你想要不同的憑證,請在你的實際'DataSource'周圍使用'UserCredentialsDataSourceAdapter'。 –

+0

你好,謝謝你的回答!我試圖使用UserCrendentialsDataSourceAdapter,但我沒有設法使它工作。 – Chaveex

回答

0

Deinum, 謝謝你的回答!我試圖使用UserCrendentialsDataSourceAdapter,但我沒有設法使它工作。但是你對範圍的觀察讓我嘗試了一些我在寫這篇文章之前嘗試過的東西。 最後,我用這個:

<beans:bean id="connex" class="com.sponge.bob.calendar.entity.CustomConnexion"> 
    </beans:bean> 

    <beans:bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
     <beans:property name="driverClassName" value="${DB_DRIVER}" /> 
     <beans:property name="url" value="${DB_PROTOCOL}:@${DB_HOST}:${DB_PORT}:${DB_NAME}" /> 
     <beans:property name="username" value="#{connex.user}"/> 
     <beans:property name="password" value="#{connex.pass}"/> 
    </beans:bean> 

@Component 
@Scope("singleton") // <-- I changed this (it was "step" before) 
public class CustomConnexion { 
    public String user; 
    public String pass; 
    public String base; 

    @Autowired 
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomConnexion.class); 

    public CustomConnexion() { 
     initConnexion(); 
    } 

    public void initConnexion() { 
     IniReader reader = new IniReader(); 

     setUser(reader.getProperty(Constants.MYCOMMON, Constants.USER)); 
     setBase(reader.getProperty(Constants.MYCOMMON, Constants.BASE)); 
     setPass(reader.getProperty(Constants.MYCOMMON, Constants.PASS)); 
    } 

    /* getters and setters after this line (not printed here but they have the default name */ 
} 

我IniReader()只是解析的.ini

0

我覺得你得到的用戶名和密碼爲空。

從其構造函數中移除調用initConnexion()。

添加下面的註釋上initConnexion(頂部) @PostConstruct