Unable to Autowire

kang :

Using Spring Initializer to create a simple Spring boot. I am only choosing DevTools under the options available.

After creating the project, without making any changes to it, able to run the program fine.

Now when I try to do some Autowiring in the project, it simply doesn't work. I don't get it. Been looking all over previous questions here which has resolutions for it but none works plus there is nothing complex about what I am doing in my case as follows. Please advice what I am missing.

@SpringBootApplication
public class DemoApplication {

    //  @Autowired
    //  private static Maker maker; // Stopped using this cos I wanted to check if the autowiring is not working in this class only or anywhere. Turns out it is anywhere. 

        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
            Maker maker = new Maker();
            maker.printName(); // Fails cos of ServiceHelper Autowiring 
        }
    }


@Service
public class Maker {

    @Autowired
    private ServiceHelper serviceHelper;

    public void printName(){
        System.out.println("This is from the method body itself.");
        System.out.println("Auto wiring works cos I got this -> " + serviceHelper.help());
    }
}

@Component
public class ServiceHelper {
    public String help(){
        return "...FROM HELPER...";
    }
}

StackTrace

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: java.lang.NullPointerException at com.example.demo.services.Maker.printName(Maker.java:15) at com.example.demo.DemoApplication.main(DemoApplication.java:17) ... 5 more

Deadpool :

If you create any bean using new keyword that bean will not added to Spring Application Context, and this is one way to @Autowire static beans

@SpringBootApplication
public class DemoApplication {

    @Autowired
    private static Maker maker; 

    @Autowired
    private Maker tMaker;

    @PostConstruct
    public void init() {
    DemoApplication.maker = tMaker;
}

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        maker.printName();  
    }
}

or you can do Autowire using Constructor the instance of Maker is injected as an argument to the constructor when DemoApplication is created

@Autowired
public DemoApplication(Maker maker) {
    DemoApplication.maker = maker;
    }

or you can use @Autowired on setter method, the setter method is called with the instance of Maker when DemoApplication is created

@Autowired
public void setMaker(Maker maker) {
        DemoApplication.maker = maker
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104569&siteId=1