I have a simple SpringBoot app which has following classes:
org.domain.abcpoc.controllers.DataController.java
package org.domain.abcpoc.controllers;
import org.domain.abcpoc.entities.UserEntity;
import org.domain.abcpoc.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/data")
public class DataController {
@Autowired
private UserRepository repo;
@GetMapping()
public String test() {
return "test";
}
@GetMapping("/user")
public UserEntity getUser() {
return repo.findById("E7####J2####1##").orElse(null);
}
}
org.domain.abcpoc.repositories.UserRepository.java
package org.domain.abcpoc.repositories;
import org.domain.abcpoc.entities.UserEntity;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<UserEntity, String> {
}
My main Spring class is:
package org.domain.abcpoc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClmConnectorService {
public static void main(String[] args) {
SpringApplication.run(ClmConnectorService.class, args);
}
}
And the exception I get is:
APPLICATION FAILED TO START
Description:
Field repo in org.domain.abcpoc.controllers.DataController required
a bean of type ‘org.domain.abcpoc.repositories.UserRepository’ that
could not be found.The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type
‘org.domain.abcpoc.repositories.UserRepository’ in your
configuration.
Could something else be the issue based on this exception and provided classes?
Database configuration?
pom.xml?
This is part of a Java module, could there be something in the parent pom.xml?
>Solution :
There’s one annotation missing right in the main Spring
class. Try adding @EnableJpaRepositories
as:
@SpringBootApplication
@EnableJpaRepositories
public class ClmConnectorService {
public static void main(String[] args) {
SpringApplication.run(ClmConnectorService.class, args);
}
}