首页 文章

使用jndi数据源和spring batch admin

提问于
浏览
7

使用Spring Batch Admin时,它会尝试为dataSource,transactionManager等提供一些默认值 .

如果要覆盖这些默认值,可以在 META-INF/spring/batch/servlet/override/ 文件夹下创建自己的xml bean定义,并在引导期间保证将覆盖默认属性 .

在spring-batch-admin中,dataSource缺省值在data-source-context.xml中使用此定义定义

<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.driverbatch.jdbc.url ,并具有以下jndi定义

<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 . *的任何值,它会失败并出现异常 .

无法在字符串值[$]中解析占位符'batch.jdbc.driver'

由于我将使用JNDI并且不想处理这些属性值,所以我无法继续 .

有关如何在这种情况下覆盖dataSource的想法?

2 回答

  • 3

    在Spring Batch Admin中,有两个正在加载的Spring ApplicationContexts:

    • servlet-config.xml

    • webapp-config.xml

    servlet-config.xml具有以下导入:

    <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具有以下导入:

    <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配置的覆盖(/ META-INF / spring / batch / servlet / override / * .xml)时,就像您一样,将新bean添加到第一个上下文中,而不是覆盖第二个上下文的dataSource bean .

    So, you need to put your custom data-source-context.xml under META-INF/spring/batch/override/ instead of META-INF/spring/batch/servlet/override/

    然后它工作,你甚至不会得到 Could not resolve placeholder 'batch.jdbc.driver' in string value [${batch.jdbc.driver}] 错误 .

  • 6

    从Spring 3.1开始,有'profiles'功能,允许您根据您所处的环境设置数据源'source'(用于本地测试的嵌入式测试,用于部署的JNDI) .

    这看起来像下面这样;

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- "production" datasource -->
        <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/dbconn"/>
    
        <!-- profile for "local" testing -->
        <beans profile="local">
                <!-- datasource that only gets created in that active profile -->
            <jdbc:embedded-database id="dataSource" type="H2"/>
        </beans>
    
    
    </beans>
    

    在此示例中,当“活动配置文件”设置为“本地”时,它将覆盖jndi-lookup数据源 .

相关问题