SpringBoot 无法注入 JDBCTemplate
SpringBoot 无法注入 JDBCTemplate
直接在 controller 用的,下面是代码
@Controller
public class MainController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/test")
@ResponseBody
public String data() {
Map<String, Object> objectMap = jdbcTemplate.queryForMap("select * from userinfo");
return JSONUtils.toJSONString(objectMap);
}
}
启动类
package com.junbaor.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=rootroot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.data-source-class-name=com.alibaba.druid.pool.DruidDataSource
异常:
2016-04-16 12:02:27.830 INFO 8516 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@547c04c4{/,file:/C:/Users/junbaor/AppData/Local/Temp/jetty-docbase.4679667705669156115.8080/,AVAILABLE}
2016-04-16 12:02:27.831 INFO 8516 --- [ main] org.eclipse.jetty.server.Server : Started @5967ms
2016-04-16 12:02:27.853 WARN 8516 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate com.junbaor.springboot.controller.MainController.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2016-04-16 12:02:27.863 INFO 8516 --- [ main] o.e.jetty.server.handler.ContextHandler : Stopped o.s.b.c.e.j.JettyEmbeddedWebAppContext@547c04c4{/,file:/C:/Users/junbaor/AppData/Local/Temp/jetty-docbase.4679667705669156115.8080/,UNAVAILABLE}
2016-04-16 12:02:27.871 ERROR 8516 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate com.junbaor.springboot.controller.MainController.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at com.junbaor.springboot.Application.main(Application.java:12) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_31]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_31]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate com.junbaor.springboot.controller.MainController.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 22 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 24 common frames omitted
2016-04-16 12:02:27.877 INFO 8516 --- [ main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/charsets.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/deploy.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/access-bridge-64.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/cldrdata.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/dnsns.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/jaccess.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/jfxrt.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/localedata.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/nashorn.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/sunec.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/sunjce_provider.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/sunmscapi.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/sunpkcs11.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/ext/zipfs.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/javaws.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/jce.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/jfr.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/jfxswt.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/jsse.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/management-agent.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/plugin.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/resources.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/jre/lib/rt.jar, file:/D:/Github/untitled1/target/classes/, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.3.3.RELEASE/spring-boot-starter-web-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot-starter/1.3.3.RELEASE/spring-boot-starter-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot/1.3.3.RELEASE/spring-boot-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-core/4.2.5.RELEASE/spring-core-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-context/4.2.5.RELEASE/spring-context-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.3.3.RELEASE/spring-boot-autoconfigure-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.3.3.RELEASE/spring-boot-starter-logging-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/ch/qos/logback/logback-classic/1.1.5/logback-classic-1.1.5.jar, file:/D:/Tools/.m2/repository/ch/qos/logback/logback-core/1.1.5/logback-core-1.1.5.jar, file:/D:/Tools/.m2/repository/org/slf4j/slf4j-api/1.7.16/slf4j-api-1.7.16.jar, file:/D:/Tools/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.16/jcl-over-slf4j-1.7.16.jar, file:/D:/Tools/.m2/repository/org/slf4j/jul-to-slf4j/1.7.16/jul-to-slf4j-1.7.16.jar, file:/D:/Tools/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.16/log4j-over-slf4j-1.7.16.jar, file:/D:/Tools/.m2/repository/org/yaml/snakeyaml/1.16/snakeyaml-1.16.jar, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot-starter-validation/1.3.3.RELEASE/spring-boot-starter-validation-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.0.32/tomcat-embed-el-8.0.32.jar, file:/D:/Tools/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar, file:/D:/Tools/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/D:/Tools/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar, file:/D:/Tools/.m2/repository/com/fasterxml/classmate/1.1.0/classmate-1.1.0.jar, file:/D:/Tools/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.6.5/jackson-databind-2.6.5.jar, file:/D:/Tools/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.6.5/jackson-annotations-2.6.5.jar, file:/D:/Tools/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.5/jackson-core-2.6.5.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-web/4.2.5.RELEASE/spring-web-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-aop/4.2.5.RELEASE/spring-aop-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-beans/4.2.5.RELEASE/spring-beans-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-expression/4.2.5.RELEASE/spring-expression-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-webmvc/4.2.5.RELEASE/spring-webmvc-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/boot/spring-boot-starter-jetty/1.3.3.RELEASE/spring-boot-starter-jetty-1.3.3.RELEASE.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-servlets/9.2.15.v20160210/jetty-servlets-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-continuation/9.2.15.v20160210/jetty-continuation-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-http/9.2.15.v20160210/jetty-http-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-util/9.2.15.v20160210/jetty-util-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-io/9.2.15.v20160210/jetty-io-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-webapp/9.2.15.v20160210/jetty-webapp-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-xml/9.2.15.v20160210/jetty-xml-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-servlet/9.2.15.v20160210/jetty-servlet-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-security/9.2.15.v20160210/jetty-security-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-server/9.2.15.v20160210/jetty-server-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/websocket-server/9.2.15.v20160210/websocket-server-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/websocket-common/9.2.15.v20160210/websocket-common-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/websocket-api/9.2.15.v20160210/websocket-api-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/websocket-client/9.2.15.v20160210/websocket-client-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/websocket-servlet/9.2.15.v20160210/websocket-servlet-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/javax-websocket-server-impl/9.2.15.v20160210/javax-websocket-server-impl-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-annotations/9.2.15.v20160210/jetty-annotations-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-plus/9.2.15.v20160210/jetty-plus-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/jetty-jndi/9.2.15.v20160210/jetty-jndi-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/javax/annotation/javax.annotation-api/1.2/javax.annotation-api-1.2.jar, file:/D:/Tools/.m2/repository/org/ow2/asm/asm/5.0.1/asm-5.0.1.jar, file:/D:/Tools/.m2/repository/org/ow2/asm/asm-commons/5.0.1/asm-commons-5.0.1.jar, file:/D:/Tools/.m2/repository/org/ow2/asm/asm-tree/5.0.1/asm-tree-5.0.1.jar, file:/D:/Tools/.m2/repository/org/eclipse/jetty/websocket/javax-websocket-client-impl/9.2.15.v20160210/javax-websocket-client-impl-9.2.15.v20160210.jar, file:/D:/Tools/.m2/repository/javax/websocket/javax.websocket-api/1.0/javax.websocket-api-1.0.jar, file:/D:/Tools/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar, file:/D:/Tools/.m2/repository/com/alibaba/druid/1.0.18/druid-1.0.18.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/lib/jconsole.jar, file:/C:/Program%20Files/Java/jdk1.8.0_31/lib/tools.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-jdbc/4.2.5.RELEASE/spring-jdbc-4.2.5.RELEASE.jar, file:/D:/Tools/.m2/repository/org/springframework/spring-tx/4.2.5.RELEASE/spring-tx-4.2.5.RELEASE.jar, file:/D:/Program%20Files%20(x86)/JetBrains/IntelliJ%20IDEA%202016.1/lib/idea_rt.jar]
Process finished with exit code 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解决方案:
修改 pom.xml
额,貌似你都没配置jdbctemplate吧?难道spring boot能自己识别你的配置生成jdbcTemplate ?
我也有这样的问题,只不过,我的是dao层的。
而且我也使用starter-jdbc了。
我也遇到同样的问题了。后来换个思路,通过javaconfig方式配置解决的。如下: