Spring Boot应用程序在启动服务器时引发异常。
例外是:
上下文初始化期间遇到异常-取消 刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为“ userController”的bean时出错:不满意的依赖关系 通过字段“ userService”表示;嵌套异常为 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为'userService'的bean时出错:不满意的依赖关系 通过字段“ userRepository”表示;嵌套异常为 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 类型的合格豆 “ yhcmain.healthcare.repositories.UserRepository”可用:预期 至少1个符合自动装配候选条件的bean。相依性 注释: {@ org.springframework.beans.factory.annotation.Autowired(required = true)}
申请无法开始
说明:
yhcmain.healthcare.service.user.UserServiceImpl中的字段userRepository需要一个类型为“ yhcmain.healthcare.repositories.UserRepository”的bean。
操作:
考虑在您的配置中定义类型为'yhcmain.healthcare.repositories.UserRepository'的bean
控制器:
@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) {
...
...
}
}
答案 0 :(得分:2)
@ jsm24 ..您需要在存储库定义的primary key
接口中提供实体的CrudRepository
(Long或Integer的ID),并确保@ComponentScan("RootDirectoryURL")
正常工作。>
@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, PrimaryKey> {
}
答案 1 :(得分:0)
您需要在配置类(HealthcareApplication)中添加以下注释:
@EnableJpaRepositories("<repository-package>")