Spring Boot添加JDBC后启动失败:JdbcTemplate Bean未找到
错误日志
将启动ApplicationContext失败。若要显示条件评估报告,请启用'debug'模式重新运行应用。
2023-09-21T09:51:20.646+05:30 ERROR 18538 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :应用启动失败
描述:
com.snigdha.snigdhahospitals.controller中的jdbcTemplate字段需要类型为'org.springframework.jdbc.core.JdbcTemplate'的Bean,但未找到。
注入点注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
操作建议:
请在配置中定义类型为'org.springframework.jdbc.core.JdbcTemplate'的Bean。
问题场景
添加JDBC依赖后运行Spring Boot应用,启动失败,以下是项目的pom.xml和controller.java代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.snigdha</groupId> <artifactId>snigdhahospitals</artifactId> <version>0.0.1-SNAPSHOT</version> <name>snigdhahospitals</name> <description>Demo project for Spring Boot</description> <properties> <java.version>20</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> <!-- <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> --> </project>
controller.java
package com.snigdha.snigdhahospitals; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import com.snigdha.snigdhahospitals.model.Doctor; @RestController public class controller { @Autowired JdbcTemplate jdbcTemplate; @GetMapping("/d") public ResponseEntity<List<Doctor>> getDoctors(){ List<Doctor> doctors = jdbcTemplate.query("SELECT * FROM DOCTOR", new BeanPropertyRowMapper<Doctor>(Doctor.class)); return new ResponseEntity<>(doctors,HttpStatus.OK); } @RequestMapping("/") public ModelAndView home(){ ModelAndView mv = new ModelAndView(); mv.setViewName("home"); return mv; } @RequestMapping("/home") public ModelAndView homepage(){ ModelAndView mv = new ModelAndView(); mv.setViewName("home"); return mv; } @RequestMapping("/doctors") public ModelAndView doctors(){ ModelAndView mv = new ModelAndView(); mv.setViewName("doctors"); return mv; } @RequestMapping("/login") public ModelAndView login(){ ModelAndView mv = new ModelAndView(); mv.setViewName("login"); return mv; } @RequestMapping("/signup") public ModelAndView signup(){ ModelAndView mv = new ModelAndView(); mv.setViewName("signup"); return mv; } @RequestMapping("/appointments") public ModelAndView appointments(){ ModelAndView mv = new ModelAndView(); mv.setViewName("appointments"); return mv; } @RequestMapping("/contactus") public ModelAndView contactus(){ ModelAndView mv = new ModelAndView(); mv.setViewName("contactus"); return mv; } @RequestMapping("/grievance") public ModelAndView grievance(){ ModelAndView mv = new ModelAndView(); mv.setViewName("grievance"); return mv; } @RequestMapping("/aboutus") public ModelAndView aboutus(){ ModelAndView mv = new ModelAndView(); mv.setViewName("aboutus"); return mv; } }
解决方案
1. 添加数据库连接配置
Spring Boot的JdbcTemplate自动配置依赖有效的数据源配置,在src/main/resources/application.properties(或application.yml)中添加MySQL连接信息:
# 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名?useSSL=false&serverTimezone=UTC spring.datasource.username=你的数据库用户名 spring.datasource.password=你的数据库密码 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2. 移除重复依赖
pom.xml中重复添加了spring-boot-starter-jdbc依赖,删除其中一处,保留一个即可:
删除以下重复的依赖块:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
3. 手动配置JdbcTemplate(可选)
如果上述步骤后仍无法自动创建JdbcTemplate Bean,可手动配置一个配置类生成Bean:
package com.snigdha.snigdhahospitals.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @Configuration public class JdbcConfig { @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } }
完成以上步骤后,重新启动应用即可解决JdbcTemplate Bean未找到的问题。
内容的提问来源于stack exchange,提问作者Vignesh

