• Post Reply Bookmark Topic Watch Topic
  • New Topic

@Autowired not working for a repository  RSS feed

 
Alejandro Barrero
Ranch Hand
Posts: 376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a class I have

When running I get  bankRepository null.
I have

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16044
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because your class JpaRepository1 is not a Spring bean, and you are instantiating the class yourself using new JpaRepository1() in line 6 instead of letting Spring manage it.

Spring autowiring only works in Spring beans. Spring cannot autowire fields in objects that are not Spring beans.
 
Alejandro Barrero
Ranch Hand
Posts: 376
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. I tried to use your ideas, but it still doesn't work (well I am just learning).
This is what I did




I also tried to inject BankService in JpaRepository1 but it didn't work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!