• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

@Autowired not working in Spring Framework

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have asked this question before about code from an Internet tutorial, but I was only told to look at other tutorials. Now I am trying to create an application following example code in the book "Spring Data" by Petri Kainulainen. I have a service RepositoryContactService

My ApplicationContext class sets the package of the service for scanning

I am running class Test

The problem is that the line "service.save(contact);" is giving me service null.
If you believe that you can tell me how to solve this problem, please let me know. If not please don't tell me anything; I am sorry to say this, but it happen this morning.
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another problem; I changed Test

and changed ApplicationContext

Added "com.packtpub.springdata.jpa.repository" for ContactRepository (assumming it is needed

but now repository is null in

@Override
public void save(Contact updated) {
repository.save(updated);
}


in RepositoryContactService. I really don't know what I am doing wrong.
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first problem continues even if I use @Component
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem continues even after using @Repository
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem in the second case is that I instantiated the class, but even if I use @Configurable the problem continues.
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is not with the static variable, because

Doesn't work either.
 
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the same issue as your other thread: https://coderanch.com/t/698501/frameworks/Autowired-working. The issue remains the same: Spring isn't doing anything if you don't tell Spring to do something. Just annotating fields doesn't mean Spring will start doing anything, you need to make sure that Spring starts processing the annotations. Whenever you use "new" you are bypassing Spring, and nothing gets autowired. Autowiring static fields will not work either way, because that's just not how Spring autowiring works. You can only autowire instance constructors / fields / methods.
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Spring problem is the same; but one issue came from an Internet tutorial and the other one from a book. I appreciate the time you spend sending me messages, but if you cannot tell me something specific to address the problem, please don't waste your time.
 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried removing the static on the fields?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!