Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBatch:动态数据源值_Java_Spring_Spring Batch - Fatal编程技术网

Java SpringBatch:动态数据源值

Java SpringBatch:动态数据源值,java,spring,spring-batch,Java,Spring,Spring Batch,我找到了这个主题,它回答了我所寻找的问题: 问题是,当我尝试时,我有一个例外 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 '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文件,从中获取DB信息,然后将它们注入到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>
有没有可能通过这种方式动态获取此密码和用户,我开始失去理智?

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>

@组件
@范围(“单例”)/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>

@组件

@Scope(“singleton”)/我认为您得到的用户名和密码为null

从其构造函数中删除调用initConnexion()的操作

在initConnexion()顶部添加以下注释
@PostConstruct

我认为您的用户名和密码为空

从其构造函数中删除调用initConnexion()的操作

在initConnexion()顶部添加以下注释
@施工后

不,不会的。您的bean也不会是步骤作用域,xml中的bean不是步骤作用域,而是单例。接下来,DriverManager数据源是一个单例,在启动时创建。如果您需要不同的凭据,请在实际的
数据源周围使用
UserCredentialsDataSourceAdapter
。您好,谢谢您的回答!我试图使用UserCrendentialsDataSourceAdapter,但我没有成功使它工作。不,它不会。您的bean也不会是步骤作用域,xml中的bean不是步骤作用域,而是单例。接下来,DriverManager数据源是一个单例,在启动时创建。如果您需要不同的凭据,请在实际的
数据源周围使用
UserCredentialsDataSourceAdapter
。您好,谢谢您的回答!我尝试使用UserCrendentialsDataSourceAdapter,但没有成功。
@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 */
}