JdbcDaoSupport配合@Repository无法注入DataSource

JdbcDaoSupport@Repository配合使用的时候,会出现DataSource无法注入的问题,Google+查看源码,发现JdbcDaoSupport的设计可能有点问题,或者说不适合配合注解使用。
通常来说,我们首先会有配置DataSource的xml,这是必不可少的。

<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"></propert>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>

然后我们会有一个Dao接口,定义了数据库访问层(DAL)的操作:

import java.util.List;
public interface StudentDao {
    List<Student> findAllStudents();
}

最后该我们创建DaoImpl了,创建DaoImpl概括可以分为两种方式:
1. xml配置方式。
xml首先配置一个JdbcTemplate bean,再配置一个StudentDaoImpl bean,并将JdbcTemplate bean通过xml配置的方式注入到StudentDaoImpl bean里。
2. 注解方式。
通过@Repository注解的方式配置StudentDaoImpl bean

我个人不太喜欢xml配置的方式,因为bean较多的话会把xml搞的很冗长,看起来头疼。所有一般我都是采用注解的方式来注入bean,这样xml会比较简洁,只需要配一下扫描路径即可。

这里就说一下在使用@Repository配合JdbcDaoSupport会出现的问题(xml配置方式不会出现)。
我们看下JdbcDaoSupport部分源码:

public abstract class JdbcDaoSupport extends DaoSupport {
    private JdbcTemplate jdbcTemplate;
    /**
     * Set the JDBC DataSource to be used by this DAO.
     */
    public final void setDataSource(DataSource dataSource) {
        if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
            this.jdbcTemplate = createJdbcTemplate(dataSource);
            initTemplateConfig();
        }
    }
    ...

实质上JdbcDaoSupport只是一个帮助类,就是帮助我们注入DataSource的,我们完全可以自己在DaoImpl实现类中自己加上一个DataSource或者JdbcTemplate的变量,然后@Autowired@Inject进来,而不用继承JdbcDaoSupport类.
这里值得注意一点是setDataSource这个方法是final的,在子类中无法重写,这样就导致
注解方式无法注入(@Inject@AutowiredDataSource,如:

@Repository
public class JdbcStudentDao extends JdbcDaoSupport implements StudentDao {

    //Error, cannot override the final method from JdbcDaoSupport
    @Autowired
    public void setDataSource(DataSource dataSource){
        this.setDataSource(dataSource);
    }

    @Override
    public List<Student> findAllStudents() {
        List<Student> students = getJdbcTemplate().query("select * from student", new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                long id = rs.getLong("id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                int age = rs.getInt("age");
                long phone = rs.getLong("phone");
                Student student = new Student();
                student.setAddress(address);
                student.setAge(age);
                student.setId(id);
                student.setName(name);
                student.setPhone(phone);
                student.setTeacherId(rs.getLong("teacherId"));
                return student;
            }
        });
        return students;
    }
}

所以,JdbcDaoSupport配合注解使用就会出现无法注入的问题。已经有人向Spring提了issue要求去掉final修饰符,但是给到的回复是won’t fix。==!。

网上有人提供了解决方案,To quickly fix it, uses @PostConstruct to inject the dataSource like this :

@Repository
public class UserDetailsDaoImpl extends JdbcDaoSupport implements UserDetailsDao {

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    private void initialize() {
        setDataSource(dataSource);
    }
    ...
}

如果这样去注入DataSource的话,JdbcDaoSupport存在的意义又是什么呢?还不如直接在DaoImpl里注入JdbcTemplate bean来的直接。

参考资料:

  1. Spring JdbcDaoSupport源码
  2. How to autowire DataSource in JdbcDaoSupport
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值