0

I'm having some issues with Autowiring in Spring Boot. Some of these issues are related to IntelliJ https://youtrack.jetbrains.com/issue/IDEA-139669. But I noticed something now.

In one class I'm trying to Autowire UserDetailsService like this.

@Autowired
UserDetailsService userDetailsService;

public void someMethod() {
userDetailsService.getUserDetails();
}

If the getUserDetials method is static in UserDetailsService like below. Then the Autowiring works.

@Service
public class UserDetailsService {
    public static void getUserDetails() {
    System.out.println("Output from UserDetailsService");
}

But if the getUserDetials method is non-static in UserDetailsService like below. Then I get the following error java.lang.NullPointerException: null for userDetailsService.getUserDetails() when I try to use the Autowiring.

@Service
public class UserDetailsService {
    public void getUserDetails() {
    System.out.println("Output from UserDetailsService");
}

Is it expected behaviour that Autowiring is dependent on the method being static or is it just an issue with the IntelliJ, or am I missing something else here?

g3blv
  • 3,877
  • 7
  • 38
  • 51
  • What do you mean returns null? Nothing is returning anything. Clarify your question as it makes no sense. – Alan Hay Apr 25 '17 at 18:41
  • @AlanHay I've updated the question now. – g3blv Apr 25 '17 at 19:11
  • 6
    Static / non-static has nothing to do with autowiring as it just not happening regardless: http://stackoverflow.com/questions/24800309/can-we-call-static-method-with-null-object-in-java-if-yes-how So the issue is in your config.UserDetailsService is probable not being picked up as a Sping Bean and so nothing is autowired. – Alan Hay Apr 25 '17 at 19:27
  • @AlanHay Now I get it. Since it is `static` there is no need for a `new` instance of the class and therefore I don't need a new instance of the class to use it's methods? Therefore I could remove the `@Autowired` annotation and it will still work? – g3blv Apr 26 '17 at 08:34

0 Answers0