I have a simple spring boot application that will connect to the Oracle DB and perform CRUD operations using the JPA repository. All the annotations and pom.xml looks good but the application fails saying required a bean that could not be found. Here are the classes - I'm just posting the required pieces. Executing this in Intellij.
Main class:
@SpringBootApplication(scanBasePackages = {"com.example"})
public class JpademoApplication {
public static void main(String[] args) {
SpringApplication.run(JpademoApplication.class, args);
}
}
Controller:
@RestController
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerService customerService;
@GetMapping
public List<CustomerData> getCustomer(){
return customerService.getAllCustomers();
}
Service:
@Service
public class DefaultCustomerService implements CustomerService {
@Autowired
private CustomerRepository customerRepository;
@Override
public CustomerData saveCustomer(CustomerData customer) {
Customer customerModel=populateCustomerEntity(customer);
return populateCustomerData(customerRepository.save(customerModel));
}
Repository:
@Repository
public interface CustomerRepository extends JpaRepository<Customer,Long> {
}
Pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Here is the error:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-06-08 11:39:31.121 ERROR 16064 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field customerRepository in com.example.service.DefaultCustomerService required a bean of type 'com.example.repository.CustomerRepository' 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 'com.example.repository.CustomerRepository' in your configuration.
main/
├── java/
│ └── com.example/
| ├── controller/
| | └── CustomerController.java
| ├── domain/
| | └── Customer.java
| ├── dto/
| | └── CustomerData.java
| ├── jparepo/
| | ├── JpademoApplication.java
| |
| ├── repository/
| | └── CustomerRepository.java
| └── service/
| └── CustomerService.java
└── DefaultCustomerService.java
└── resources/
└── application.properties
Please suggest if someone have seen a similar issue and steps to resolve this.