2016-03-01 98 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