Home > OS >  Parameter 0 of constructor in .... Spring Boot
Parameter 0 of constructor in .... Spring Boot

Time:10-18

I am a fish in Spring Boot and Data Jpa, I Tried to create a basic Spring boot application but every time I am encountering the error. Can you help me?

That's my code: Spring Boot Application class:

   @SpringBootApplication
   @ComponentScan(basePackages = "com.project.*")
   @EnableJpaRepositories(basePackages = "com.project.repository.*")
   @EntityScan(basePackages = "com.project.entities.*")
   @EnableAutoConfiguration
   public class MainApplication {
   public static void main(String[] args) {
    SpringApplication.run(MainApplication.class, args);
    }
 }

Controller Class:

 @RestController
 @RequestMapping(value = "/api")
 public class controller {

private IUserServices userServices;

@Autowired
public controller(IUserServices userServices) {
    this.userServices = userServices;
}

@GetMapping(value = "/merhaba")
public String sayHello(){
    return "Hello World";
}

@GetMapping(value = "/getall")
public List<User> getAll(){
    return this.userServices.getAllUsers();
}

}

Repository Class:

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

IServices Class:

@Service
public interface IUserServices {
void saveUser(User user);
List<User> getAllUsers();

}

ServicesImpl Class:

@Service
public class UserServicesImpl implements  IUserServices{

private UserRepository userRepository;

@Autowired
public UserServicesImpl(UserRepository userRepository) {
    this.userRepository = userRepository;
}

@Override
public void saveUser(User user) {
    this.userRepository.save(user);
}

@Override
public List<User> getAllUsers() {
    return this.userRepository.findAll();
}

}

Entity Class:

@Entity
@Table(catalog = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;

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

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return "User{"  
            "id="   id  
            ", name='"   name   '\''  
            '}';
   }
}

AND THIS MY ERROR MESSAGE:

***************************
 APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.project.services.UserServicesImpl required a bean of 
type 'com.project.repository.UserRepository' that could not be found.


   Action:

  Consider defining a bean of type 'com.project.repository.UserRepository' in your 
  configuration.


  Process finished with exit code 0

SO This is application properties file:

spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/u
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.properties.javax.persistence.validation.mode = none

CodePudding user response:

There are some issues that you should fix them.

First

When you have the spring boot application with @SpringBootApplication you don't need other stuff such as @EnableAutoConfiguration and etc, So remove them all.
You can read more about it here.

Second

You don't need to annotate your service interface with @Service, because you did it in the UserServicesImpl class.

Third

You defined id as an integer in your user entity but in the repository, you wrote your id as Long. It's wrong. It should be something like this.

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

Try the above solutions and let me know the result.

  • Related