public interface GenConfigRepository extends JpaRepository<GenConfig,Long> { GenConfig findByTableName(String tableName); } @Service @RequiredArgsConstructor public class GenConfigServiceImpl { public GenConfig find(String tableName) { GenConfig genConfig = genConfigRepository.findByTableName(tableName); if(genConfig == null){ return new GenConfig(tableName); } return genConfig; } } 启动时报错如下 *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.jsweique.generator.service.impl.GenConfigServiceImpl required a bean of type 'com.jsweique.generator.repository.GenConfigRepository' that could not be found. Action: Consider defining a bean of type 'com.jsweique.generator.repository.GenConfigRepository' in your configuration. Disconnected from the target VM, address: '127.0.0.1:61355', transport: 'socket' Process finished with exit code 1
时间: 2023-07-01 19:24:56 浏览: 42
这个错误提示是说在 `GenConfigServiceImpl` 类的构造函数中需要一个名为 `GenConfigRepository` 的 Bean,但是在 Spring 容器中没有找到该 Bean。
解决该问题的方法是在 Spring 的配置类中添加一个 `@Repository` 注解,将 `GenConfigRepository` 接口标记为一个 Spring 组件。
示例:
```java
@Repository
public interface GenConfigRepository extends JpaRepository<GenConfig,Long> {
GenConfig findByTableName(String tableName);
}
```
如果你已经加了 `@Repository` 注解,那么可能是 Spring 没有扫描到该接口所在的包。可以尝试在 Spring 的配置类中添加 `@ComponentScan` 注解扫描该包:
```java
@Configuration
@ComponentScan(basePackages = "com.jsweique.generator.repository")
public class AppConfig {
// ...
}
```
这样就可以解决该错误了。