我发现这个问题是回答什么,我一直在寻找: 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 */
}
是否有可能通过这种方式获得此密码和用户动态使用,我开始失去主意?
没有也不会。你的bean也不会被一步作用,xml中的那个不是一步作用域,而是一个单一作用域。接下来'DriverManagerDataSource'是一个单例,并在启动时创建。如果你想要不同的凭证,请在你的实际'DataSource'周围使用'UserCredentialsDataSourceAdapter'。 –
你好,谢谢你的回答!我试图使用UserCrendentialsDataSourceAdapter,但我没有设法使它工作。 – Chaveex