Results 1 to 4 of 4

Thread: @Autowired and repositories

  1. #1
    Join Date
    Apr 2011
    Posts
    15

    Default @Autowired and repositories

    I have created a repository CarRepository that extends JpaRepository<Car, Long>. I also have a test where I declare a member variable of type CarRepository and tag it as @Autowired. The test runs fine.

    Now I want to use this repository from another module (not a test). In the class where I will load Car objects I declare a CarRepository variable and also have a setter method for it; my goal is to inject this dependency. In my application-context.xml I define a bean of class CarRepository; everything seems ready to rock. But when I run this code, I get the following exception:

    Could not instantiate bean class [CarRepository]: Specified class is an interface
    Of course, one of the great things about Spring Data is the ability to define repositories as interfaces and not having to write a single line of code in an implementing class. What is the obvious thing I am missing?

  2. #2
    Join Date
    Apr 2011
    Posts
    107

    Default

    You cannot define a bean with an interface...

    data-jpa will create the implementation and will declare the implementation in spring repository.

    I don't know the norm used, but try to inject your beans with 'ref="CarRepositoryImpl"' without declaring CarRepositoryImpl bean. (or set log of your test to debug to view which object are added in the repository)

  3. #3
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    493

    Default

    The Spring Data Repository beans are registered under the simple repository interface's name (as stated in the reference documentation [1]). So you should be able to simply reference a
    Code:
    carRepository
    .

    [1] http://static.springsource.org/sprin...e/html/#d0e351

  4. #4
    Join Date
    Apr 2011
    Posts
    15

    Default

    Thanks for all the hints. That last one by Oliver was the one that solved it for me. I also printed the Spring Data reference and am reading it right now.

    I am loving Spring Data (JPA) / Hades. I heard about it for the first time at TSS-JS in Prague in 2009, but only now I am seeing its full potential.

    Thanks again and best regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •