• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

@Autowired not working in Spring Framework

 
Ranch Hand
Posts: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first problem continues even if I use @Component
 
Alejandro Barrero
Ranch Hand
Posts: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem continues even after using @Repository
 
Alejandro Barrero
Ranch Hand
Posts: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is not with the static variable, because

Doesn't work either.
 
Marshal
Posts: 22444
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 501
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 556
10
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried removing the static on the fields?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic