Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bean required in Spring application with JPARepository

I have a problem with bean instantiation.

Parameter 0 of constructor in table.football.play.service.impl.PlayerServiceImpl required a bean of type 'table.football.play.repository.PlayerRepository' that could not be found.

error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is my class

package table.football.play.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import table.football.play.entity.Player;

@Repository
public interface PlayerRepository extends JpaRepository<Player,Long> {
  
}

and this is service implementation :

package table.football.play.service.impl;

import static table.football.play.util.ConvertEntityToDto.convertListPlayerEntityToListPlayerDto;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import table.football.play.dto.PlayerDTO;
import table.football.play.entity.Player;
import table.football.play.repository.PlayerRepository;
import table.football.play.service.interf.PlayerService;

@Service
public class PlayerServiceImpl implements PlayerService{
  
  private PlayerRepository playerRepository;
  
  @Autowired
  public PlayerServiceImpl(PlayerRepository playerRepository){
    this.playerRepository = playerRepository;
  }
  
  @Override
  public List<PlayerDTO> findAll(){
    List<Player> listPlayer = playerRepository.findAll();
    List<PlayerDTO> playerDTOList = convertListPlayerEntityToListPlayerDto(listPlayer);
    return playerDTOList;
  }


}

there a entity class

package table.football.play.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;

@Data
@Entity
@Table(name="players")
public class Player {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  @Column(name="name")
  private String name;
  @Column(name="surname")
  private String surname;
  
}

lastly this is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>table.football</groupId>
    <artifactId>play</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>game</name>
    <description>calcio balilla project</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>3.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${project.parent.version}</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I try @Autowired also on field (it’s a better practice? or it’s good to post @Autowired on constructor? or it the same?)

I have read about some people talk about create a default constructor …
But Repository is an interface , how can i resolve it?

Can someone also explain me the theory behind this error ? It is not enough for me just that it works, I would like to understand.

Thank you very much!

>Solution :

The problem is in dependency injection that you are using in Service class(it not work so, please read the documentation).

Instead of:

private PlayerRepository playerRepository;
  
  @Autowired
  public PlayerServiceImpl(PlayerRepository playerRepository){
    this.playerRepository = playerRepository;
  }

Write field injection

@Autowired
private PlayerRepository playerRepository;

Or Constructor Injection:

private final PlayerRepository playerRepository;
  
  
  public PlayerServiceImpl(PlayerRepository playerRepository){
    this.playerRepository = playerRepository;
  }

And also the second problem is here

<dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

Because you are using spring boot > 3.x that are using Jakarta.persistence not javax just remove that dependency and follow next one steps.

To fix it replace

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

To

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

And in Entity class replace all javax. imports to jakarta.

It is going to fix the problem.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading