package com.exam.examserver;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

import com.exam.models.Role;
import com.exam.models.User;
import com.exam.models.UserRole;
import com.exam.service.UserService;

@ComponentScan({"com.exam.service"})
@SpringBootApplication
public class ExamserverApplication implements CommandLineRunner {

    @Autowired
    private UserService userService ;
    
    public static void main(String[] args) {
        SpringApplication.run(ExamserverApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("starting");     
        
        User user=new User();
        user.setFirstName("yogender");
        user.setLastName("sharma");
        user.setUserName("yogen139");
        user.setPassword("abc");
        user.setEmail("[email protected]");
 
        Role role1= new Role();
        role1.setRoleId(44L);
        role1.setRoleName("Admin");
        
        
        Set<UserRole> userRoleSet= new HashSet<>();
        UserRole userRole= new UserRole();
        userRole.setRole(role1);
        userRole.setUser(user);
        User user1= this.userService.CreateUser(user, userRoleSet);
        
        System.out.println(user1.getUserName());
    }
}

`Error starting ApplicationContext. To display the condition 
evaluation report re-run your application with 'debug' enabled.

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

Description:
Field userRepo in com.exam.service.UserServiceImpl required a bean 
of type 'com.exam.repo.UserRepository' that could not be found.

The injection point has the following annotations:

@org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.exam.repo.UserRepository' in 
your configuration.`

Getting this error in my console while i am trying to run the application . I have defined the User Repository with @Repository notation. Here is the project structure :

(https://i.stack.imgur.com/j5Lqg.png)

If anyone please can help me in this.

I am running it on SpringToolSuite and Using MySQL database. Performed various troubleshooting steps there.

1

There are 1 best solutions below

0
grekier On

The error comes from a wrong scanning configuration. By default, Spring Boot scans all packages below the main class. Since you have the main class in a "sub"-package, it will not scan all the others.

There are 2 ways to fix it:

  1. The easy one is to move the main class one package up and remove the @ComponentScan annotation
  2. Alternative, you will need to add all your packages you want scanned to the @ComponentScan like:
@ComponentScan({"com.exam.service","com.exam.repo","com.exam.models"})