Reputation: 39
Spring Boot application is throwing an exception while starting the server.
Exception is:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'yhcmain.healthcare.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
APPLICATION FAILED TO START
Description:
Field userRepository in yhcmain.healthcare.service.user.UserServiceImpl required a bean of type 'yhcmain.healthcare.repositories.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'yhcmain.healthcare.repositories.UserRepository' in your configuration
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:
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
public User createUser(User user) {
return this.userRepository.save(user);
}
}
Repository:
@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, String> {
}
Main Application:
@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) {
...
...
}
}
Upvotes: 1
Views: 3699
Reputation: 6016
You need to provide Entity's primary key
(ID in Long or Integer) in CrudRepository Interface in your repository definition and make sure that @ComponentScan("RootDirectoryURL")
is working fine.
...
@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, PrimaryKey> {}
...
Happy Coding.. :)
Upvotes: 2
Reputation: 3677
You need to add the following annotation to your configuration class (HealthcareApplication):
@EnableJpaRepositories("<repository-package>")
Upvotes: 0