Announcement Announcement Module
Collapse
No announcement yet.
@Autowired and repositories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    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)

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment

        Working...
        X