This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

@Autowired not working

 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a simple test class in Spring

But I am getting userService null in line
List<User> userList = test.userService.getAllUsers();
I also have
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I'm missing something, you never specified how the UserService should be made available for autowiring. Unless it extends (Crud)Repository or there is an @Component implementation or a @Bean provider, this interface is no different from any other interface.

Also, autowiring only works if you let Spring create the instances. The way you're using Test, it's a simple POJO where nothing will get autowired.
 
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UserService is an interface. Try to implement it with a Class and using "Service" annotation and override the method you are calling. Or else You should extend CRUD/PagingAndSorting Repository.
Service Annotation
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies.

 
Rob Spoor
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Also, autowiring only works if you let Spring create the instances. The way you're using Test, it's a simple POJO where nothing will get autowired.


I'm quoting myself because this is the reason nothing gets autowired.
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:



Then you have to Autowire it and use it.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob; that shows my ignorance; could you please tell me how to correct the problem.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the responses from all of you. Please tell me how and where do i use "@Service("userService")".
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:I appreciate the responses from all of you. Please tell me how and where do i use "@Service("userService")".


 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Khalid, but I still have problems; I used '@Service("userService")' in class UserServiceImpl, but in class Test with 'private static UserService userService;' I am still getting userService null.
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:Thanks Khalid, but I still have problems; I used '@Service("userService")' in class UserServiceImpl, but in class Test with 'private static UserService userService;' I am still getting userService null.



Directly invoke the method with userService object instead of creating new Test object.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just did with

Still doesn't work. I also tried with

but repository is null in getAllUsers() of UserServiceImpl
 
M Khalid
Ranch Hand
Posts: 100
2
Python Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:I just did with

Still doesn't work. I also tried with

but repository is null in getAllUsers() of UserServiceImpl



Because there is no data source.
You should refer to a tutorial first and then try implementing. I would suggest to go JournalDev or Bealdege tutorial sites
 
Rob Spoor
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This problem is more or less repeated/continued at https://coderanch.com/t/698537/frameworks/Autowired-working-Spring-Framework#3278140.
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic