0

I'm currently using Spring for a project, and I'm confused how Spring can actually instantiate an annotated interface such as a JpaRepository.

As far as I know, an interface cannot be instantiated without an implementation. Also it was in my understanding, that any annotations in interfaces are not inherit to the implementing class (Which should render them useless?)

So how is Spring able to actually get a working bean out of only a annotated interface as below?

@Repository
public interface ExampleRepository extends PagingAndSortingRepository<ExampleEntity, Long>{

}
Herr Derb
  • 4,977
  • 5
  • 34
  • 62
  • 2
    Spring Data generates JDK proxies at runtime. – chrylis -cautiouslyoptimistic- Sep 04 '17 at 06:38
  • As per their homepage *As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.* see [home page](http://projects.spring.io/spring-data-jpa/) – Scary Wombat Sep 04 '17 at 06:39
  • @chrylis I'm not really familiar with the proxy concept. As I understand, spring detects the `Component` annotation and creates a default bean implementing the defined repository interface? – Herr Derb Sep 04 '17 at 06:44
  • As mentioned by @chrylis, spring generates JDK proxies at runtime. A good way to "see that" is to run your application in debug mode and put breakpoints on the interface methods. You'll be able to see the classes generated. – Gautam Sep 04 '17 at 06:46
  • @HerrDerb Read some tutorials on the JDK proxy feature. It's a core part of a lot of frameworks that provide magic, including Spring, JPA providers, REST clients, and more. – chrylis -cautiouslyoptimistic- Sep 04 '17 at 06:58

0 Answers0