异常问题,如题,详细见下
Error creating bean with name 'monitorDao':
Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field:
private org.springframework.jdbc.core.JdbcTemplate
com.yonyou.iuap.disconf.web.service.monitor.service.dao.MonitorDao.jt;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined:
expected single matching bean but found 2: onedbJdbcTemplate,twodbJdbcTemplate
原因:
创建了两个jdbctemplate的bean,但有其他方法默认调用,使得spring不清楚该使用哪个bean。
修改方法:
原配置文件只配置dataSource数据源就即可,而在新使用jdbctemplate的地方Dao层,添加如下方法:
@Service
public class AppAuthDaoImpl extends JdbcDaoSupport implements AppAuthDao{
private JdbcTemplate jdbcTemplate;
@Autowired(required = false)
@Qualifier("YYCdataSource")
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public List<String> listUserIdFromConfCenterDataAuth(String inviterId) {
String sql = "select user_id from user where inviter_id = ?";
return jdbcTemplate.queryForList(sql, new Object[] {inviterId},String.class);
}
}

本文解析了一个关于Spring框架中依赖注入失败的问题,具体为在创建MonitorDao bean时无法确定使用哪一个JdbcTemplate bean,导致了NoUniqueBeanDefinitionException异常。文章提供了修改方案,即在Dao层通过@Autowired和@Qualifier注解明确指定要使用的数据源。
685

被折叠的 条评论
为什么被折叠?



