[英]SpringBatch : dynamic datasource values
我发现这个主题可以回答我所寻找的内容: 如何在配置文件中动态传递值
事情是,当我尝试时,我有一个异常。
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 */
}
是否有可能通过这种方式动态地获取此密码和用户,我开始失去了理智?
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
我认为您将用户名和密码设置为null。
从其构造函数中删除调用initConnexion()的操作。
在initConnexion()@PostConstruct顶部添加以下注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.