使用名称' application'创建bean时出错:注入自动连接的依赖项失败;

时间:2015-08-19 11:09:49

标签: java spring spring-boot db2 spring-jdbc

我是Spring的新手,遇到以下问题,有人可以帮我吗?

我正在尝试使用Spring Guide gs-relational-data-access项目来使用本地db2实例而不是嵌入式h2数据库,但它失败了。我已将db2的驱动程序添加到构建路径,并已从pom.xml文件中删除了h2依赖项。我还将db2连接属性添加到类路径根目录中的application.properties文件中。

spring.datasource.url=jdbc:db2//localhost:50000/SAMPLE
spring.datasource.username=dsuser
spring.datasource.password=dspass
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2DataSource
spring.datasource.schema=SPRING

使用嵌入式h2实例它工作正常,但我需要构建一个与db2实例一起使用的应用程序。

完整堆栈跟踪:
         线程" main"中的例外情况org.springframework.beans.factory.BeanCreationException:使用名称' application'创建bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework.jdbc.core.JdbcTemplate hello.Application.jdbcTemplate;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.springframework.jdbc.core.JdbcTemplate]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 2015-08-19 16:19:07.465 ERROR 12016 --- [main] o.s.boot.SpringApplication:应用程序启动失败 org.springframework.beans.factory.BeanCreationException:使用名称' application'创建bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework.jdbc.core.JdbcTemplate hello.Application.jdbcTemplate;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.springframework.jdbc.core.JdbcTemplate]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)     在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)     at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)     在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)     在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)     在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:320)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:957)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:946)     at hello.Application.main(Application.java:21) 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework.jdbc.core.JdbcTemplate hello.Application.jdbcTemplate;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.springframework.jdbc.core.JdbcTemplate]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)     在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)     ...省略了15个常见帧 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.springframework.jdbc.core.JdbcTemplate]的限定bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)     在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ...省略了17个常用帧 at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)     在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)     在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)     在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)     在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:320)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:957)     在org.springframework.boot.SpringApplication.run(SpringApplication.java:946)     at hello.Application.main(Application.java:21) 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework.jdbc.core.JdbcTemplate hello.Application.jdbcTemplate;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.springframework.jdbc.core.JdbcTemplate]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)     在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)     ......还有15个 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.springframework.jdbc.core.JdbcTemplate]的限定bean依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)     在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)     在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)     ......还有17个

我已经使用maven安装了db2依赖项并将它们添加到pom.xml中     

mvn install:install-file -Dfile=db2jcc4.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc4 -Dversion=10.1 -Dpackaging=jar
mvn install:install-file -Dfile=db2jcc_license_cu.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc_license_cu  -Dversion=10.1 -Dpackaging=jar

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ibm.db2</groupId>
        <artifactId>db2jcc4</artifactId>
        <version>10.1</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.db2</groupId>
        <artifactId>db2jcc_license_cu</artifactId>
        <version>10.1</version>
    </dependency>
</dependencies>

0 个答案:

没有答案