0

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.

sandy
  • 21
  • 5
  • What is the package structure? – Boris Jun 08 '21 at 17:42
  • This is the structure: com.sample-- controller domain dto jparepo(Main Method) repository service – sandy Jun 08 '21 at 17:52
  • But the error logs mentions `com.example`, can you please double check? You can use, for example `$ tree`. – Boris Jun 08 '21 at 18:12
  • Hey @Boris, I have updated the project structure in the main question. Please check and suggest. – sandy Jun 08 '21 at 18:41
  • 1
    https://stackoverflow.com/questions/29221645/cant-autowire-repository-annotated-interface-in-spring-boot This might help. Please have a look. – noob_nerd Jun 08 '21 at 18:42
  • I would move JpademoApplication to package 'com.example' so the package auto scanning will work. – emeraldjava Jun 09 '21 at 12:59
  • **UPDATE SOLUTION:** Spring boot application context was not scanning the repo when extended the JPA repository. But it was able to scan the custom repository. This solved the issue: When extending a JPA repository I added these two annotations in the main class. **SpringBootApplication(scanBasePackages = {"base package"}) EnableJpaRepositories(basePackages = {"repository package"}) EntityScan(basePackages = {"entity package"})** Hope this helps. – sandy Jun 09 '21 at 16:34

2 Answers2

1

If you add @EnableJpaRepositories on top of your JpademoApplication class, It should work.

0

Your application failed to start because the @SpringBootApplication annotation placed on your main class is misconfigured:

@SpringBootApplication(scanBasePackages = {"com.example"})

To fix the issue remove the scanBasePackages property which has no effect on Spring Data Repository scanning. The annotation implicitly defines a base “search package”:

@SpringBootApplication

Also to avoid failures on start up, I recommend to have a test class which checks all required beans are available:

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class JpademoApplicationTest {

  @Test
  void contextLoads() {
  }

}
Boris
  • 22,667
  • 16
  • 50
  • 71