重写spring jdbcTemplate里面的queryForObject方法的问题

Eagle昊 2016-10-12 02:39:22
最近实用jdbcTemplate里面的queryForObject方法,在返回空值的时候总是抛出异常,而我想要的是它返回null,后面看了源码,结合度娘得知重写queryForObject里面调用的requiredSingleResult方法可以解决问题,然后写了一个类重写了这个方法,代码如下
public class OverrideJdbcDao extends JdbcTemplate{

/**
* 重写JdbcTemplate里面的queryForObject方法源码调用的requiredSingleResult,当查询到的结果为空时返回null(原来是抛出异常)
*/
@Override
public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException {
// TODO Auto-generated method stub
return super.queryForObject(sql, requiredType);
}

public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {
int size = (results != null ? results.size() : 0);
if (size == 0) {
return null;
}
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1, size);
}
return results.iterator().next();
}
}


但是在使用的时候却报错了,错误如下
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classesDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com.dao.impl.ClassesDaoImpl.db; 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: jdbcTemplate,OverrideJdbcDao
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classesDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com.dao.impl.ClassesDaoImpl.db; 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: jdbcTemplate,OverrideJdbcDao
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.frame.main.MainFrame.<init>(MainFrame.java:39)
at com.frame.main.MainFrame$1.run(MainFrame.java:50)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com.dao.impl.ClassesDaoImpl.db; 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: jdbcTemplate,OverrideJdbcDao
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 28 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] is defined: expected single matching bean but found 2: jdbcTemplate,OverrideJdbcDao
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 30 more


我该怎么做才能试用我重写的那个类?
...全文
313 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
江油儿 2018-12-28
  • 打赏
  • 举报
回复
怎么解决的啊,能不能说清楚,我用的是springboot
Eagle昊 2016-10-12
  • 打赏
  • 举报
回复
我自己找到解决办法了,是因为
<!--  <bean id= "jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate">
               <property name= "dataSource" ref ="dataSource"></property>
        </bean> -->
        
        <!--  配置spring jdbc -->
        <bean id="overrideJdbc" class="com.dao.OverrideJdbc">
         	   <property name= "dataSource" ref ="dataSource"></property>
        </bean>
原本为jdbcTemplate注入了一次dataSource,后面为overrideJdbc又注入了一遍,从而导致冲突报错
Eagle昊 2016-10-12
  • 打赏
  • 举报
回复
各位前辈帮忙啊
@Repository
public class OJdbcTemplateDaoImpl implements OJdbcTemplateDao {

	@Autowired
	OverrideJdbcDao ojd;
	
	
	@Override
	public int queryforInt(String sql) {
		return ojd.queryForObject(sql,Integer.class);
	}
}
我现在在调用OverrideJdbcDao的时候报错了,最后一步了

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧