Spring Data JPA - injection fails - BeanCreationException: Could not autowire field

前端 未结 1 423
陌清茗
陌清茗 2020-12-31 03:31

i followed the tutorial posted here to get a basis application to work with Spring Data JPA. Now, how i understood, using the configuration



        
相关标签:
1条回答
  • 2020-12-31 04:04

    The problem is very probably in some of the configuration you haven't shown. It would also be good if you posted the error you're getting. It might be something different than what you think it is.

    One thing I notice about your config is that you're using context:annotation-config instead of context:component-scan. The latter will auto-detect and create beans based on the @Component family of annotations. The former doesn't do that.

    Other than that, everything you posted looks like it should work, though there are several odd things, which I'll come to in a moment. I copied all your posted code into a sample project and filled in a few details, like a maven pom, a persistence.xml, and the missing pieces of the applicationContext.xml. I also added a "create" method to the service so it would actually do something. With those in place and a main class to drive it all, it's a runnable example. You can browse the code on github, or you can clone and run it with:

    git clone git://github.com/zzantozz/testbed tmp
    cd tmp/stackoverflow/10539417-basic-spring-data-jpa
    mvn -q compile exec:java -Dexec.mainClass=rds.testbed.springDataJpa.SpringDataJp
    

    Now for the oddities that I noticed. From the top:

    • With the code as given, there's no need for the PersistenceAnnotationBeanPostProcessor that you've added to the applicationContext.xml. It's not doing anything for you. Of course, there may well be other code that needs it that you haven't shown.
    • The @Repository annotation on your CustomerService is supposed to be used on DAO classes, or classes that interact with a database. The appropriate annotation for a service is @Service.
    • The @Resource annotation on your ICustomerRepository is mostly used for marking fields and methods for autowiring. I'm not sure what made you think to put it on your repository interface, but it's not doing anything there.
    • Your repository shouldn't be @Transactional. That belongs at your service, and you've already got it there, so that's fine. Note that it still works with the @Transactional on the repository because it just joins the existing transaction started by the service.
    • It's worth noting again that you're not using component scanning, even though you have a @Component-related annotation (the @Repository on your service). That might be causing you some problems. Instead of turning on component scanning, I manually created the service bean using XML in the sample project.

    So... if this hasn't explained something to you, if you give me a specific error, I can probably explain why you're getting it and tell you what to do to make it right.

    0 讨论(0)
提交回复
热议问题