Announcement Announcement Module
No announcement yet.
Not finding Spring Data Repository Implementation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not finding Spring Data Repository Implementation

    I am trying to enable the spring data jpa features and I am getting the following error:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'fitnessController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: po; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [] is defined: expected single matching bean but found 2: [weightRepository, weightRepositoryImpl]

    I have an interface WeightRepository defined in as:
    public interface WeightRepository
     extends JpaRepository<Weight, Integer>
        List<Weight> findByWeightUser( User weightUser );
    and a class which implements the interface in
    @Transactional( readOnly = true )
    public class WeightRepositoryImpl
     implements WeightRepository
      ... other stubbed methods ...
      public List<Weight> findByWeightUser(User weightUser)
        return ( this.findByWeightUser( weightUser ) );
    I have set to scan for repo data with with the configration:
    <bean name="weightRepository" class=" epositoryImpl">
    <jpa:repositories base-package="" />

    In the controller I have the following:
    public class FitnessController
      WeightRepository weightRepo;
      ... methods defined ...
    I tried implementing this based upon the docs and the spring blog but I am doing something incorrectly. Does anyone have any suggestions?


  • #2
    Actually finding 2 implementations

    No unique bean of type [] is defined: expected single matching bean but found 2: [weightRepository, weightRepositoryImpl]
    Spring scans your base package and it creates proxy instances for those interfaces extending Repository.

    So Spring is instantiating your WeightRepository with a default bean id=weightRepositoryImpl and you are instantiating WeightRepositoryImpl with a bean id=weightRepository.

    You have two instances of a WeightRepository implementation, one created by you and one by Spring. Because @Autowired injects by type Spring is finding 2 candidates to inject.

    You only need to write an implementation of WeightRepository if you want to add custom functionality. If you don't need custom functionality you do NOT need to declared your weightRepository bean in your configuration, neither you need to provide an implementation of WeightRepository.

    Spring will create a proxy implementation of your interface WeightRepository and make it available implicitly even if no bean is declared in the context.

    You can read about it in the documentation The behavior explained is mentioned in several parts of the documentation. If you need to add custom behavior by implementing your own WeightRepository take a look at 1.4. Custom implementations section.

    Hope it helps!

    Last edited by nicolas.loriente; Jun 9th, 2011, 03:59 PM.


    • #3
      Thanks for the clarrification. That helped greatly.