在dao中使用注解
@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{
@Override
public Object addObject(String sqlid, Object obj) {
// TODO Auto-generated method stub
return null;
}
由以下原因引起:java.lang.IllegalArgumentException:需要“dataSource”或“jdbcTemplate”
我不想使用:
<bean id="termsDao" class="com.manage.base.dao.impl.TestDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
此代码在xml中设置,“jdbcTemplate”已在其他“spring-xml”中定义。
如何通过注解解决这个问题:“'dataSource' or 'jdbcTemplate' is required”
最佳答案
您可以使用以下方法之一。第一个 - 首选/推荐使用数据源,因为您不会在公共(public)接口(interface)中公开 SpringFramework 类。两者都可以。
@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{
@Autowired
TestDaoImpl(DataSource dataSource) {
setDataSource(dataSource);
}
}
或者
@Repository("testDao")
public class TestDaoImpl extends JdbcDaoSupport implements BaseDao{
@Autowired
TestDaoImpl(JDBCTemplate template) {
setJdbcTemplate(template);
}
}
关于spring3-annotation-JdbcDaoSupport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570521/