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

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

问题描述

根据Spring document
使用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是使用数据源的setter在Component类中创建的。

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?

SGB

推荐答案

你可以做你想做的事。 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天全站免登陆