0

I'm asking this question with a risk of having it marked as duplicate, but I honestly don't think it is, so here it goes.

I'm having trouble running a basic Spring Boot application. The error I'm getting is this:

Parameter 0 of constructor in com.example.demo.service.EventService required a bean of type 'com.example.demo.dao.EventRepository' that could not be found.

Now this seems like a pretty common error (and question on SO) that should point the developer to check the following issues:

  1. Most commonly the project structure is wrong (or let's say 'custom') and the bean is not getting scanned because of hierarchy [Questions: this, this, this, this and probably many more]. Solution should be to place the SpringBootApplication in the top package and all the components in its sub-packages so it could scan everything properly or to custom-define the packages that need to be scanned.
  2. The bean was not defined, i.e. there's a missing annotation of type @Service, @Repository etc. so the bean is not being created.
  3. Another reason might be using two different ways of defining beans (as posted here). I'm using purely annotation-style defining, so I think I should be good here.

Also, this question has an answer mentioning to exclude the autoconfiguration of JPA repositories from application.properties file, but I don't have this configuration set.

There were also a couple of questions/answers mentioning issues with dependencies and pom.xml file which fixed this problem, but my pom.xml is a pretty basic file created from Spring Initializr, so again, I don't think this is the solution.

The error message also says:

Consider defining a bean of type 'com.example.demo.dao.EventRepository' in your configuration.

The 'missing bean' is this repository:

package com.example.demo.dao;

import com.example.demo.entity.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EventRepository extends JpaRepository<Event, Integer> {
}

You can see it has @Repository annotation (although I've created repositories without this annotation before and it worked fine so I don't think this is required, but I added it now just in case this was the issue), it extends JpaRepository so it should be a valid repository and it's inside com.example.demo.dao package.

The class that's autowiring this is a service:

package com.example.demo.service;

import com.example.demo.dao.EventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EventService {

    EventRepository eventRepository;

    @Autowired
    public EventService(EventRepository eventRepository) {
        this.eventRepository = eventRepository;
    }
}

I'll also provide the main application so you can see its package is com.example.demo which is a parent package for both the service and the repository:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

I've also tried rebuilding the project and the infamous "closing and reopening" of IntelliJ, because it was known for sometimes acting stupid in my experience and this would solve it, but not this time.

Also as a side note, I've successfully created these kinds of projects before, so I'm really not sure what's the issue right now.

Am I missing something obvious here? What else can I check?

EDIT:

Here's the entity class as well (generated by an IDE tool):

package com.example.demo.entity;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Objects;

@Entity
public class Event {
    private int id;
    private String name;
    private Timestamp dateFrom;
    private Timestamp dateTo;

    // getters & setters abstracted
}
basarito
  • 161
  • 1
  • 13
  • Can you also show the definition of your `com.example.demo.entity.Event` class? May be `@Entity` annotation is missing. – yuppie-flu Nov 09 '19 at 14:36
  • @yuppie-flu It's not missing, I've used a tool that generates entity from table, so it should create it correctly. But I will add it to the question in case someone else was wondering as well. – basarito Nov 09 '19 at 14:43

1 Answers1

0

Turns out it was an issue with pom.xml after all.

I've added the JPA dependency later and accidentally added the wrong one. My pom.xml had

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
</dependency>

instead of

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Apparently the IDE tool used for generating entities from tables was missing javax.persistence so it added that jar manually through lib folder. Everything looked fine to IntelliJ, but something was messed between dependencies.

Anyway, I changed the dependency in pom.xml and removed the extra jar added. Now everything works fine.

basarito
  • 161
  • 1
  • 13