问题描述
使用Spring Batch Admin时,它将尝试为dataSource,transactionManager等提供一些默认值.
When using Spring Batch Admin, it tries to provide some defaults for dataSource, transactionManager etc.
如果要覆盖这些默认值,请在 META-INF/spring/batch/servlet/override/文件夹下创建自己的xml bean定义,并在引导过程中确保默认属性将被覆盖.
If you want to override these defaults, you create your own xml bean definitions under META-INF/spring/batch/servlet/override/ folder and during the bootstrap it guarantees that the default properties will be overridden.
在spring-batch-admin中,在data-source-context.xml中使用此定义定义了dataSource默认值
In spring-batch-admin, a dataSource default is defined in data-source-context.xml with this definition
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
<property name="testWhileIdle" value="${batch.jdbc.testWhileIdle}"/>
<property name="validationQuery" value="${batch.jdbc.validationQuery}"/>
</bean>
现在,我想用JNDI数据源覆盖此dataSource,因此我删除了batch.jdbc.driver
,batch.jdbc.url
之类的属性行,并具有以下jndi定义
Now, I want to override this dataSource with a JNDI datasource so I removed the property lines like batch.jdbc.driver
, batch.jdbc.url
and have the following jndi definition
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dbconn" />
</bean>
您可能会很容易猜到,系统首先尝试初始化data-source-context.xml中定义的dataSource bean,并且由于找不到属性值batch.jdbc.*的任何值,因此它会失败并发生异常.
As you may easily guess the system first tries to initialize the dataSource bean defined in data-source-context.xml and since it cannot find any values for property values batch.jdbc.* it fails with an exception.
由于我将使用JNDI,并且不想处理这些属性值,因此无法继续.
Since I will be using JNDI and do not want to deal with these property values, I cannot proceed.
关于在这种情况下如何覆盖dataSource的任何想法?
Any idea on how to override dataSource in this situation?
推荐答案
在Spring Batch Admin中,有2个Spring ApplicationContext正在加载:
Within Spring Batch Admin, there are 2 Spring ApplicationContexts that are being loaded:
- servlet-config.xml
- webapp-config.xml
servlet-config.xml具有以下导入:
servlet-config.xml has these imports:
<import resource="classpath*:/META-INF/spring/batch/servlet/resources/*.xml" />
<import resource="classpath*:/META-INF/spring/batch/servlet/manager/*.xml" />
<import resource="classpath*:/META-INF/spring/batch/servlet/override/*.xml" />
webapp-config.xml具有以下导入:
webapp-config.xml has these imports:
<import resource="classpath*:/META-INF/spring/batch/bootstrap/**/*.xml" />
<import resource="classpath*:/META-INF/spring/batch/override/**/*.xml" />
servlet-config.xml配置servlet,webapp-config.xml配置应用程序(其后端部分).问题在于dataSource bean是第二个配置(而不是第一个)的一部分/在第二个配置中定义.因此,当您将dataSource bean添加到servlet config(/META-INF/spring/batch/servlet/override/*.xml)的覆盖中时,您会在第一个上下文中添加一个新bean.覆盖第二个上下文的dataSource bean的方法.
servlet-config.xml configurers the servlet, webapp-config.xml configures (the backend part of the) the application.The problem is that the dataSource bean is part of/defined in the second config, not the first.Hence, when you add the dataSource bean to an override for the servlet config(/META-INF/spring/batch/servlet/override/*.xml), as you are doing, you add a new bean to the first context, instead of overwriting the dataSource bean of the second context.
因此,您需要将自定义data-source-context.xml放在META-INF/spring/batch/override/下,而不是META-INF/spring/batch/servlet/override/下.
然后它起作用,您甚至都不会收到Could not resolve placeholder 'batch.jdbc.driver' in string value [${batch.jdbc.driver}]
错误.
Then it works and you won't even get the Could not resolve placeholder 'batch.jdbc.driver' in string value [${batch.jdbc.driver}]
error.
这篇关于将JNDI数据源与Spring Batch Admin结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!