【问题标题】:SpringBatch : dynamic datasource valuesSpring Batch:动态数据源值
【发布时间】:2016-03-01 14:06:19
【问题描述】:

我发现这个主题回答了我正在寻找的内容: how to pass values dynamically in config file

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

    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 我使用构造函数来实例化我的属性(它并不性感,但我从 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 */
}

这种方式可以动态获取这个密码和用户吗,我开始迷茫了?

【问题讨论】:

  • 不,不会的。您的 bean 也不会是步进范围的,xml 中的 bean 不是步进范围的,而是单例的。接下来DriverManagerDataSource 是一个单例,在启动时创建。如果您想要不同的凭据,请在您的实际 DataSource 周围使用 UserCredentialsDataSourceAdapter
  • 您好,谢谢您的回答!我尝试使用 UserCrendentialsDataSourceAdapter,但没有成功。

标签: java spring spring-batch


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    在 initConnexion() 之上添加下面的注解 @PostConstruct

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 2018-01-27
      • 2017-02-16
      • 2019-09-26
      • 2013-12-18
      • 1970-01-01
      • 2021-02-21
      • 2016-06-16
      • 2016-05-01
      相关资源
      最近更新 更多