Announcement Announcement Module
Collapse
No announcement yet.
Not finding Spring Data Repository Implementation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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: com.test.fitness.jpa.repo.WeightRepository com.test.fitness.client.FitnessController.weightRe po; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [com.test.fitness.jpa.repo.WeightRepository] is defined: expected single matching bean but found 2: [weightRepository, weightRepositoryImpl]

    I have an interface WeightRepository defined in com.test.fitness.jpa.repo as:
    Code:
    public interface WeightRepository
     extends JpaRepository<Weight, Integer>
    { 
        List<Weight> findByWeightUser( User weightUser );
    }
    and a class which implements the interface in com.diodesign.fitness.jpa.repo.impl:
    Code:
    @Repository
    @Transactional( readOnly = true )
    public class WeightRepositoryImpl
     implements WeightRepository
    {
      ... other stubbed methods ...
      @Override
      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="com.diodesign.fitness.jpa.repo.impl.WeightR epositoryImpl">
    </bean>
    <jpa:repositories base-package="com.diodesign.fitness.jpa.repo" />

    In the controller I have the following:
    Code:
    @Controller
    @SessionAttributes
    public class FitnessController
    { 
      @Autowired
      WeightRepository weightRepo;
      ... methods defined ...
    }
    I tried implementing this based upon the docs and the spring blog http://blog.springsource.com/2011/02...ring-data-jpa/ but I am doing something incorrectly. Does anyone have any suggestions?

    Thanks

  • #2
    Actually finding 2 implementations

    Code:
    No unique bean of type [com.test.fitness.jpa.repo.WeightRepository] 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 http://static.springsource.org/sprin...eference/html/. 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!


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

    Comment


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

      Comment

      Working...
      X