跟随《Spring实战第6版》用JDBC Template时遇依赖注入异常
问题描述
学习《Spring实战第6版》第3章「使用JDBC Template存储数据」内容时,遇到如下异常:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcIngredientRepository' defined in file [D:\Java Projects\taco-cloud\target\classes\tacos\data\JdbcIngredientRepository.class]: Unsatisfied dependency expressed through constructor parameter
相关代码
JdbcIngredientRepository
package tacos.data; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import tacos.Ingredient; @Repository public class JdbcIngredientRepository implements IngredientRepository { private JdbcTemplate jdbcTemplate; @Autowired public JdbcIngredientRepository(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public Iterable<Ingredient> findAll() { return jdbcTemplate.query( "select id, name, type from Ingredient", this::mapRowToIngredient); } @Override public Optional<Ingredient> findById(String id) { List<Ingredient> results = jdbcTemplate.query( "select id, name, type from Ingredient where id=?", this::mapRowToIngredient, id); return results.size() == 0 ? Optional.empty() : Optional.of(results.get(0)); } @Override public Ingredient save(Ingredient ingredient) { jdbcTemplate.update( "insert into Ingredient (id, name, type) values (?, ?, ?)", ingredient.getId(), ingredient.getName(), ingredient.getType().toString()); return ingredient; } private Ingredient mapRowToIngredient(ResultSet row, int rowNum) throws SQLException { return new Ingredient( row.getString("id"), row.getString("name"), Ingredient.Type.valueOf(row.getString("type"))); } }
IngredientRepository
package tacos.data; import java.util.Optional; import tacos.Ingredient; public interface IngredientRepository { Iterable<Ingredient> findAll(); Optional<Ingredient> findById(String id); Ingredient save(Ingredient ingredient); }
已尝试更换JDBC依赖、更新Maven项目,问题仍未解决,寻求帮助。
排查方向
- 检查数据源配置:Spring容器无法创建
JdbcTemplate实例,通常是因为缺少有效的数据源配置。确保application.properties或application.yml中配置了正确的数据库连接信息,示例:spring.datasource.url=jdbc:mysql://localhost:3306/taco_cloud?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=yourpassword spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver - 确认JDBC依赖完整性:除了Spring JDBC依赖,还需要引入对应数据库的驱动依赖。Maven依赖示例:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> - 检查组件扫描范围:确保
JdbcIngredientRepository所在的tacos.data包被Spring组件扫描覆盖。如果主启动类不在tacos根包下,需在启动类上添加@ComponentScan(basePackages = "tacos")指定扫描路径。 - 查看完整异常栈:原异常仅显示根因开头,完整异常栈会指明具体无法注入的依赖细节(比如
JdbcTemplate创建失败的具体原因:数据源连接失败、驱动类找不到等),建议查看日志中完整的异常信息定位问题。
内容的提问来源于stack exchange,提问作者Pankaj
相关产品推荐
相关产品推荐

