使用 Spring JdbcTemplate - 注入数据源 vs jdbcTemplate [英] using Spring JdbcTemplate - injecting datasource vs jdbcTemplate

查看:47
本文介绍了使用 Spring JdbcTemplate - 注入数据源 vs jdbcTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Spring 文档,Spring JdbcTemplate的使用步骤如下:

As per Spring documentation, the steps to use Spring JdbcTemplate is as follows:

    <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <!-- Scans within the base package of the application for @Components to configure as beans -->
        <context:component-scan base-package="org.springframework.docs.test" />

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>

        <context:property-placeholder location="jdbc.properties"/>

    </beans>

然后,

    @Repository
    public class JdbcCorporateEventDao implements CorporateEventDao {

        private JdbcTemplate jdbcTemplate;

        @Autowired
        public void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
        }

        // JDBC-backed implementations of the methods on the CorporateEventDao follow...
    }

基本上,JdbcTemplate 是在 Component 类中使用数据源的 setter 创建的.

Basically, the JdbcTemplate is created inside the Component class using the setter for datasource.

这样做有什么问题吗,这样应用程序中就只有一个 jdbcTemplate 实例?

Is there anything wrong with doing it this way instead so that there is exactly ONE instance of jdbcTemplate in the application?

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
    p:dataSource-ref="dataSource" 
/>

然后将jdbcTemplate本身直接注入到Component中

And then injecting the jdbcTemplate itself directly into the Component

@Repository
public class JdbcCorporateEventDao implements CorporateEventDao {
    @Resource("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;


    // JDBC-backed implementations of the methods on the CorporateEventDao follow...
}

jdbcTemplate 本身有什么原因不能直接注入到组件类中吗?

Is there a reason why the jdbcTemplate itself must not be injected into the component class directly?

新加坡银行

推荐答案

你可以为所欲为.JdbcTemplate 的javadoc 更清楚说:

You can do what you want. The javadoc of JdbcTemplate even clearly says it:

可以通过使用 DataSource 引用直接实例化在服务实现中使用,或者在应用程序上下文中准备好并作为 bean 引用提供给服务.

Can be used within a service implementation via direct instantiation with a DataSource reference, or get prepared in an application context and given to services as bean reference.

这篇关于使用 Spring JdbcTemplate - 注入数据源 vs jdbcTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆