0

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!

3 Answers3

0

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

import org.springframework.stereotype.Repository; @Repository

Donkassu
  • 46
  • 3
  • Yep I know that its beacause wrong DI. But Removing @Repository doesnt help – Марат Jan 07 '23 at 10:41
  • sorry, if it didn't help... I m also a newbie in the Spring. Could you try RequiredArgsConstructor Annotation instead of Autowired Annotation and creating custom repository? I think wrong DI appears on that part. – Donkassu Jan 08 '23 at 02:48
  • I guess the answers other than mine could help your problem, they seem to be very professional seniors in the Spring. – Donkassu Jan 08 '23 at 02:54
0

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.

Sy3d
  • 197
  • 2
  • 2
  • 18
0

It's because of Spring Boot's Component Scan principle. By default, SpringBoot must register @Bean, @Service and @Controller as Singleton Bean in the BeanContainer to use DI for each other.

And it's the @ComponentScan that does this. SpringBoot has a @ComponentScan inside the @SpringBootApplication. The most important thing is where this annotation is located.

The solution is simple. The location of the BootTestFinalApplication class is not a config package. Move to com.example.boottestfinal , which is the highest package. Then all your Bean will be registered in the container and DI will work normally.

It's good to see the official document.

jojoldu
  • 40
  • 5