3

I have a problem in spring boot concerning repositories. I have a service :

@Service("userService")
public class UserServiceImpl implements UserService {

  @Autowired
  private UserRepository userRepository;

  @Autowired
  private RoleRepository roleRepository;
}

and the repos here :

@Repository("userRepository")
public interface UserRepository extends CrudRepository<User, Long> {
     User findByEmail(String email);
}

When I run the app I get this message :

Description:

Field userRepository in com.projectWS.service.UserServiceImpl required a 
bean of type 'com.projectWS.repo.UserRepository' that could not be found.

Action:

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

Please help me I'm so desperate... this is my main class:

@SpringBootApplication
@Configuration
@EnableWebMvc
public class Main {

   public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
   }
}

Another problem is , @EnableJpaRepositories doesn't get recognized !!

  • What do you mean "doesn't get recognized"? Sounds like maybe you didn't include a dependency on Spring Data JPA. (And prefer constructor injection to field injection whenever practical.) – chrylis -cautiouslyoptimistic- Dec 08 '16 at 21:17
  • I tried every possible dependency in my pom.xml... I don't understand the thing between paranths. Please help me I'ma newbie at this spring thingy – Naoufal EL BANTLI Dec 08 '16 at 21:19

3 Answers3

4

most likely your Main class is defined within a package and your other packages are not getting scanned.

Try annotation your Main class with:

@ComponentScan("com.projectWS")

judging by your error message and assuming that the top of your package level starts at com.projectWS

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
0

I am not a Spring expert but I suspect that this can be because of the case of the name of these classes. Just for peace of mind test this please:

@Service("userService")
public class UserServiceImpl implements UserService {

  @Qualifier("userRepository")
  @Autowired
  private UserRepository userRepository;

  @Qualifier("roleRepository")
  @Autowired
  private RoleRepository roleRepository;
}
Henrique Vieira
  • 151
  • 1
  • 4
0

It seems like you have not added Spring data for JPA, add the following to your pom.xml

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