gpt4 book ai didi

java - SpringBoot - UnsatisfiedDependencyException Spring Boot 不是扩展 CrudRepository 的 Autowiring Repository 接口(interface)

转载 作者:行者123 更新时间:2023-11-30 02:03:51 26 4
gpt4 key购买 nike

Spring Boot 应用程序在启动服务器时抛出异常。
异常(exception)情况是:

Exception encountered during context initialization - cancellingrefresh attempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'userController': Unsatisfied dependencyexpressed through field 'userService'; nested exception isorg.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'userService': Unsatisfied dependencyexpressed through field 'userRepository'; nested exception isorg.springframework.beans.factory.NoSuchBeanDefinitionException: Noqualifying bean of type'yhcmain.healthcare.repositories.UserRepository' available: expectedat least 1 bean which qualifies as autowire candidate. Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}


<小时/>

应用程序无法启动

<小时/>

描述:

yhcmain.healthcare.service.user.UserServiceImpl 中的字段 userRepository 需要类型为“yhcmain.healthcare.repositories.UserRepository”的 bean,但无法找到。

行动:

考虑在配置中定义“yhcmain.healthcare.repositories.UserRepository”类型的 bean

Controller :

@RestController
@CrossOrigin("*")
public class UserController {

@Autowired
private ServiceResponse response;

@Autowired
private UserService userService;

@RequestMapping(value = "/index", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<ServiceResponse> signUpAttempt(@RequestBody User user) {
...
...
}
}

服务:

@Service("userService")
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

public User createUser(User user) {
return this.userRepository.save(user);
}
}

存储库:

@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, String> {
}

主要应用:

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class HealthcareApplication implements WebMvcConfigurer {

public static void main(String[] args) {
SpringApplication.run(HealthcareApplication.class, args);
}

@Override
public void addCorsMappings(CorsRegistry registry) {
...
...
}
}

最佳答案

您需要在CrudRepository接口(interface)中提供实体的主键(Long或Integer形式的ID)存储库定义并确保 @ComponentScan("RootDirectoryURL") 工作正常。

...

@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, PrimaryKey> {}

...

快乐编码..:)

关于java - SpringBoot - UnsatisfiedDependencyException Spring Boot 不是扩展 CrudRepository 的 Autowiring Repository 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51924392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com