Spring为不同的JdbcTemplates提供相同的数据源

Spring为不同的JdbcTemplates提供相同的数据源,spring,datasource,jdbctemplate,Spring,Datasource,Jdbctemplate,我有不同的DAO,它们具有相同的类,需要使用相同类型数据源的不同jdbcTemplates。有没有一种方法可以整合我的代码,这样我就不需要使用太多的复制和粘贴 xml中的一个示例是: <bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="jdbcDataSource1" /> </bean>

我有不同的DAO,它们具有相同的类,需要使用相同类型数据源的不同jdbcTemplates。有没有一种方法可以整合我的代码,这样我就不需要使用太多的复制和粘贴

xml中的一个示例是:

<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="jdbcDataSource1" />
</bean>
<bean id="jdbcDataSource1" class="com.zaxxer.hikari.HikariDataSource"
    destroy-method="shutdown">
    <constructor-arg>
        <bean class="com.zaxxer.hikari.HikariConfig">
            <constructor-arg>
                <props>
                    <prop key="dataSource.url">dataSourceUrl
                    </prop>
                    <prop key="dataSource.user">user</prop>
                    <prop key="dataSource.password">password</prop>
                </props>
            </constructor-arg>
            <property name="dataSourceClassName"
                value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
        </bean>
    </constructor-arg>
</bean>
<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="jdbcDataSource2" />
</bean>
<bean id="jdbcDataSource2" class="com.zaxxer.hikari.HikariDataSource"
    destroy-method="shutdown">
    <constructor-arg>
        <bean class="com.zaxxer.hikari.HikariConfig">
            <constructor-arg>
                <props>
                    <prop key="dataSource.url">dataSourceUrl
                    </prop>
                    <prop key="dataSource.user">user</prop>
                    <prop key="dataSource.password">password</prop>
                </props>
            </constructor-arg>
            <property name="dataSourceClassName"
                value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
        </bean>
    </constructor-arg>
</bean>

数据源URL
使用者
暗语
数据源URL
使用者
暗语

正如代码所示,
jdbcDataSource1
jdbcDataSource2
是相同的。那么有没有一种方法可以整合这两个模板呢?

这就是将相同的数据源传递给两个JDBC模板的方法:

<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="jdbcDataSource" />
</bean>

<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="jdbcDataSource" />
</bean>

<bean id="jdbcDataSource" class="com.zaxxer.hikari.HikariDataSource"
    destroy-method="shutdown">
    <constructor-arg>
        <bean class="com.zaxxer.hikari.HikariConfig">
            <constructor-arg>
                <props>
                    <prop key="dataSource.url">dataSourceUrl
                    </prop>
                    <prop key="dataSource.user">user</prop>
                    <prop key="dataSource.password">password</prop>
                </props>
            </constructor-arg>
            <property name="dataSourceClassName"
                value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
        </bean>
    </constructor-arg>
</bean>

数据源URL
使用者
暗语

您可以在bean定义中保留一个模板和一个数据源。如果dao的模板已经预定义,并且您不想更改它,那么您仍然只能使用一个jdbc模板,如下所示:

    <bean id="yourDao1" class="package.YourDao1">
        <property name="jdbcTemplate1" ref="jdbcTemplate" />
    </bean>
    <bean id="yourDao2" class="package.YourDao2">
        <property name="jdbcTemplate2" ref="jdbcTemplate" />
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="jdbcDataSource" />
    </bean>
    <bean id="jdbcDataSource" class="com.zaxxer.hikari.HikariDataSource"
        destroy-method="shutdown">
        <constructor-arg>
            <bean class="com.zaxxer.hikari.HikariConfig">
                <constructor-arg>
                    <props>
                        <prop key="dataSource.url">dataSourceUrl
                        </prop>
                        <prop key="dataSource.user">user</prop>
                        <prop key="dataSource.password">password</prop>
                    </props>
                </constructor-arg>
                <property name="dataSourceClassName"
                    value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
            </bean>
        </constructor-arg>
    </bean>

数据源URL
使用者
暗语

我已经发布了一个答案,但是如果这不是一个选项,您可以发布为什么需要两个JDBCTemplate访问同一个数据源,以及为什么在这种情况下不在任何地方使用相同的模板?