expected single matching bean but found 2: onedbJdbcTemplate,twodbJdbcTemplate

异常问题,如题,详细见下

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 listUserIdFromConfCenterDataAuth(String inviterId) {
		String sql = "select user_id from user where inviter_id = ?";
		return jdbcTemplate.queryForList(sql, new Object[] {inviterId},String.class);
	}

}

 

你可能感兴趣的:(java,spring)