Using jndi datasource with spring batch admin

被刻印的时光 ゝ 提交于 2019-12-03 07:13:13
Integrating Stuff

Within Spring Batch Admin, there are 2 Spring ApplicationContexts that are being loaded:

  • servlet-config.xml
  • webapp-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 has these imports:

<import resource="classpath*:/META-INF/spring/batch/bootstrap/**/*.xml" />
<import resource="classpath*:/META-INF/spring/batch/override/**/*.xml" />

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.

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/

Then it works and you won't even get the Could not resolve placeholder 'batch.jdbc.driver' in string value [${batch.jdbc.driver}] error.

since Spring 3.1 there's the 'profiles' feature which allows you to set your datasource 'source' based on the environment you're in. (an embedded one for local testing, a JNDI one for deployment.)

this would look something like the following;

<?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>

in this example, when the 'active profile' is set to "local", it overwrites the jndi-lookup datasource.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!