spring - 错误,Spring NamedJdbcTemplate 中缺少什么

标签 spring spring-data

我已将我的代码配置为使用 NamedParameterJdbcTemplate,

spring.xml

<bean id="netezzaDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="org.netezza.Driver" />
    <property name="url" value="jdbc:netezza://server/DB" />
    <property name="username" value="user" />
    <property name="password" value="password" />
 </bean> 

<bean id="netezzaJDBCTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="netezzaDataSource"/>
</bean>    


@Component
public class EventDAO 
{
    @Autowired
    private DataSource netezzaDataSource;
    @Autowired
    private NamedParameterJdbcTemplate netezzaNamedTemplate;

    public NamedParameterJdbcTemplate getNetezzaNamedTemplate() 
    {
        return netezzaNamedTemplate;
    }

    public void setNetezzaNamedTemplate(NamedParameterJdbcTemplate netezzaNamedTemplate) 
    {
        this.netezzaNamedTemplate = netezzaNamedTemplate;
    }
    ..........
}

但它无法 Autowiring 并引发异常。我错过了什么吗?

异常堆栈
Feb 25, 2014 10:56:48 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@336bc75c: startup date [Tue Feb 25 10:56:48 EST 2014]; root of context hierarchy
Feb 25, 2014 10:56:48 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-local-dev.xml]
    Feb 25, 2014 10:56:49 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5fc7a9de: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,eventDAO,mySQLDataSource,mySQLJDBCTemplate,netezzaDataSource,netezzaJDBCTemplate,netezzaNamedTemplate,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
    Feb 25, 2014 10:56:49 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
    INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
    Feb 25, 2014 10:56:49 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
    INFO: Loaded JDBC driver: org.netezza.Driver
    Feb 25, 2014 10:56:49 AM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5fc7a9de: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,eventDAO,mySQLDataSource,mySQLJDBCTemplate,netezzaDataSource,netezzaJDBCTemplate,netezzaNamedTemplate,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eventDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate com.omnitracs.fra.dao.EventDAO.netezzaNamedTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'netezzaNamedTemplate' defined in class path resource [spring-local-dev.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>()
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.omnitracs.fra.main.CriticalEventMain.main(CriticalEventMain.java:21)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate com.omnitracs.fra.dao.EventDAO.netezzaNamedTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'netezzaNamedTemplate' defined in class path resource [spring-local-dev.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>()
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 13 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'netezzaNamedTemplate' defined in class path resource [spring-local-dev.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>()
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
        ... 15 more
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>()
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000)
        ... 26 more
    Caused by: java.lang.NoSuchMethodException: org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getDeclaredConstructor(Unknown Source)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
    ... 27 more

最佳答案

使用构造函数注入(inject)代替。

<bean id="netezzaJDBCTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="netezzaDataSource"/>
</bean>  

由于 NamedParameterJdbcTemplate 没有使用 args 构造函数,并且因为您正在使用
 @Autowired
    private NamedParameterJdbcTemplate netezzaNamedTemplate;

配置需要是这样的。
   <bean id="netezzaNamedTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
      <constructor-arg ref="netezzaDataSource"/>
   </bean>

关于spring - 错误,Spring NamedJdbcTemplate 中缺少什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22019796/

相关文章:

java - spring data jedis连接池配置

java - 我在使用 Spring crud reprositry 时遇到问题

java - 当对象不在持久性上下文中时,Hibernate 合并不查询数据库

spring - 使用 Elasticsearch 和 Spring Data 确定新闻源的优先级

mysql - Spring JDBC 使用 application.properties 文件

java - 我在 Spring 使用加载时间编织。我如何在 jetty 中设置类加载器?

spring - Spring JUnit 测试中的自动模拟实例化

java - JPA 示例 Spring Boot 应用程序抛出 BeanCreationException

spring - 如何控制在测试环境和生产环境之间变化的 Spring 注入(inject)?

java - SpringBoot创建DynamicDataSource : Requested bean is currently in creation: Is there an unresolvable circular reference?