• 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

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • 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: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • 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: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies.

 
Rob Spoor
Sheriff
Posts: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alejandro Barrero wrote:



Then you have to Autowire it and use it.
 
Alejandro Barrero
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • 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: 443
  • Mark post as helpful
  • send pies
  • 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: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • 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: 443
  • Mark post as helpful
  • send pies
  • 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: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • 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: 443
  • Mark post as helpful
  • send pies
  • 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: 85
2
Python Spring Java
  • Mark post as helpful
  • send pies
  • 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: 21759
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!