Вопрос

I have different DAO's that have the same class that require different jdbcTemplates who all use the same type of dataSources. Is there a way to consolidate my code, so I don't need to use so much copy and paste.

An example of what I have in xml is:

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

As seen by the code, jdbcDataSource1 and jdbcDataSource2 are the same. So is there a way to consolidate the two?

Это было полезно?

Решение

This would be the way to pass the same datasource to the two JDBC templates:

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

Другие советы

You can keep one template and one datasource in bean definitions. If your dao's template are already predefined and you don't want to change it, you can still use only one jdbc template as follows:

    <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>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top