• 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

 
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
 
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor 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: 94
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
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor 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: 94
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: 94
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: 94
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: 94
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
Marshal
Posts: 22450
121
Eclipse IDE Spring VI Editor 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic