SpringBatch:动态数据源值

时间:2016-03-01 14:06:20

标签: java spring spring-batch

我发现这个主题能够满足我的需求: https://developer.mozilla.org/en-US/docs/Web/API/History_API

问题是,当我尝试它时,我有一个例外..

<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>

我正在尝试读取.ini文件,我获取了数据库信息,然后我想将它们注入我的XML数据源配置。

这是我的xml,

@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 */
}

然后我的CustomConnexiob.class使用构造函数来实例化我的attributs(它不性感,但我从SpringBatch开始):

{{1}}

是否有可能以这种方式动态获取此密码和用户,我开始迷失自己的想法?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我认为您将用户名和密码设为null。

从构造函数中删除调用initConnexion()。

在initConnexion()之上添加以下注释 @PostConstruct