Home > Blockchain >  Repository that extends JpaRepository cannot be autowired into my Service
Repository that extends JpaRepository cannot be autowired into my Service

Time:01-07

I have UserRepository Interface that extends Jpa REpository Interface when Im trying to autowire it into a Servise or a Controller I recieve such an error -

Parameter 0 of constructor in com.example.boottestfinal.UserHelper required a bean of type 'com.example.boottestfinal.UserRepository' that could not be found.

my project structure just in case - click

UserRepository -

package com.example.boottestfinal;

import com.example.boottestfinal.enities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}

Service -

package com.example.boottestfinal;

import org.springframework.beans.factory.annotation.Autowired;
import com.example.boottestfinal.enities.User;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserHelper {
    private final UserRepository userRepository;
    @Autowired
    public UserHelper(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public void addUser(User user){
        userRepository.save(user);
    }
    public List<User> findAllUsers(){
        return userRepository.findAll();
    }
}

Entity -

package com.example.boottestfinal.enities;

import jakarta.persistence.*;
import lombok.*;

import java.io.Serializable;

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@ToString
@Entity
@Table(name = "usertable")
public class User implements Serializable {
    @Id
//    @SequenceGenerator(name = "userSequence", sequenceName = "userSequence", allocationSize = 1)
//            @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userSequence")
            @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;
    int age;
    String name;

    public User(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

If its not enough for an answear I can provide with more Information I am new to Spring Boot, hope someone helps!

CodePudding user response:

the error seems because of the wrong DI. Try to delete this part.

import org.springframework.stereotype.Repository; @Repository

CodePudding user response:

You are using constructor injection and field injection at the same time, you really should be using 1. So either:

@Autowired    
private final UserRepository userRepository;

or

private final UserRepository userRepository;

public UserHelper(UserRepository userRepository) {
   this.userRepository = userRepository;
}

But I don't believe this is whats stopping you from creating a bean and using it for dependency injection. Looking at your project structure, I see your main class BootTestFinalApplication is in the config folder. Could you move that class into the package com.example.boottestfinal.

So the issue you was facing was that your main application was unable to register the UserRepository as a bean in the application context as by default it only does component scanning in the package its present in. Since your main class is in the config package, it will only do component scanning there and won't pickup the UserRepository.

  • Related