1

I reconfigured my DAOs to a more convenient way (by using JpaRepository) instead of doing all that boilerplate code manually. But now everytime I start the Spring Application it gives me the following error:

APPLICATION FAILED TO START  
Description:  
Field userRepository in DAO.UserDAOService required a bean of type 'DAO.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 'DAO.UserRepository' in your configuration.
Process finished with exit code 1
    
Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
NaN
  • 153
  • 1
  • 5
  • 14

5 Answers5

4

SOLUTION: Just create sub-packages in the same package where you have your Spring appliciation located.

EXAMPLE OF SOLUTION CAN BE FOUND HERE: 'Field required a bean of type that could not be found.' error spring restful API using mongodb

NaN
  • 153
  • 1
  • 5
  • 14
1

You've forgot to put an annotation on your repository class. That's why Spring cannot find that bean.

Try adding @Repository on top of your class definition.

Emre Acar
  • 920
  • 9
  • 24
1

Add @Repository annotation then bean will created and autowired in service.

import org.springframework.stereotype.Repository;

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

And don't need to create bean in service

@Bean
public void setUserRepository(UserRepository userRepository)
{
    this.userRepository = userRepository;
}
Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • I did that and I still get this error: *************************** APPLICATION FAILED TO START *************************** Description: Field userRepository in DAO.UserDAOService required a bean of type 'DAO.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 'DAO.UserRepository' in your configuration. Process finished with exit code 1 – NaN Mar 08 '19 at 13:41
  • Removed service @Bean creation in controller also ? – Eklavya Mar 08 '19 at 14:03
  • Yes and still it gives me the same error. It either doesn't find it or there are multiple Bean creations but I already removed the annotations with the corresponding set Methods in the userDAOService and UserController class. – NaN Mar 08 '19 at 14:15
  • Are you added org.springframework.boot spring-boot-starter-data-jpa as dependency ? – Eklavya Mar 08 '19 at 14:33
  • 1
    Yes. Just wanted to let you know that I solved the problem. SPring couldn't find the repository because it was in another package. I put the repository as sub-package but some how spring couldn't find service. So I solved it by making sub-packages for models,service,repository and controller and that how Spring could find the repository. But thanks for sticking with me :) . – NaN Mar 08 '19 at 15:02
  • You are welcome :) But With Bean creation and without Repository annotation may be it won't work – Eklavya Mar 08 '19 at 20:17
1
  1. Make sure that you have your repository class in a sub-package of the ApplicationConfiguration class.

  2. Annotate the repository class with @Repository.

SQB
  • 3,926
  • 2
  • 28
  • 49
Mrixs
  • 11
  • 2
0

In addition to the previous answers, the IDE can often suggests you the wrong import for the annotation of the Bean class, for example for a @Service annoted bean, be sure that you import:

import org.springframework.stereotype.Service;

and not something like:

import org.jvnet.hk2.annotations.Service
Mickaël B.
  • 325
  • 4
  • 14