Unable to Autowire

@org.springframework.beans.factory.annotation.autowired(required=true) in spring boot
autowired object is null in junit
spring autowire interface multiple implementations
spring boot autowired null
spring boot autowire not working
how to autowire interface in spring boot
autowired @qualifier
spring autowire by name

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

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
}

Java: How to fix Spring @Autowired annotation not working issues , When a constructor of a class is called, the @Autowired instance Spring context will not be known to it and thus most likely this will also fail. Unable to autowire MetricRegistry #120. Closed dustinbarnes opened this issue Jun 4, 2015 · 2 comments Closed Unable to autowire MetricRegistry #120.

You create yourself an instance, its ServiceHelper does not get autowired by Spring:

Maker maker = new Maker();

You can access bean via ApplicationContext:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext cts = SpringApplication.run(DemoApplication.class, args);
        Maker maker = ctx.getBean(Maker.class);
        maker.printName();
    }
}

Two reasons why your Spring @Autowired component is null, Two reasons why your Spring @Autowired component is null A common error people hit is when they autowire a class and when they try to call a Unable to process parts as no multi-part configuration has been provided  Spring Boot integeration test, but unable to @Autowired MockMvc. P.S Tested with Spring Boot 2.1.2.RELEASE

When you use new Maker() you are not using the bean Spring created for you and as a result the the object you have is uninitialized as dependencies are not injected.

You need to get the bean which the Spring framework created, like I did below as directly you cannot autowire static fields:

@SpringBootApplication
public class DemoApplication {

    private static Maker maker;

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

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

Autowiring In Spring, If this fails, it tries to autowire by using byType . 1. Autowiring 'no': This is a default autowiring mode. It means no autowiring. <bean id  I'm surprised it works at all. Your code, for example, declares the class SampleController. Your Spring xml file does not have any reference to SampleController but does have references to a missing AppController class.

4.2. Referencing beans across @Configuration classes, Direct bean references with @Autowired In addition to being able to reference any particular bean definition as seen above, one @Configuration class may  Apart from the autowiring modes provided in bean configuration file, autowiring can be specified in bean classes also using @Autowired annotation. To use @Autowired annotation in bean classes, you must first enable the annotation in spring application using below configuration. 2.1. Enable annotation config. applicationContext.xml.

Spring Bean Autowiring - @Autowired, In Spring framework, declaring bean dependencies in configuration files is a good practice to follow, so the Spring container is able to autowire relationships  Starting with Spring 2.5, the framework introduced a new style of Dependency Injection driven by @Autowired Annotations. This annotation allows Spring to resolve and inject collaborating beans into your bean. In this tutorial, we will look at how to enable autowiring, various ways to wire in beans, making beans optional, resolving bean

Getting error "Injection of autowired dependencies failed" in Liferay , I have created a Spring MVC project using plugin sdk in liferay 7. In this I want to access ControllerBean class by @Autowired Annotation in my  unable to autowire sessionfactory bean in spring Tag: java , spring , hibernate , spring-mvc , spring-security I have searched for various solutions but nothing helping me.

Comments
  • You cannot autowire static fields.
  • @DarrenForsythe I have commented out static field.
  • @kang check my answer for a work around on injecting dependencies in a static field.
  • This certainly works. Though I thought static Auto wiring is not possible based on above comment yet this seems to prove it is possible.
  • There are still more ways also @kang
  • The autowired on the static field is pointless. What is happening here is you are autowiring the second Maker, the instance field, and setting it after the bean as been init'd.
  • updated another approach, i'm just providing approaches for static beans @DarrenForsythe
  • Whats the solution here to get the auto wire working?
  • It means, ServiceHelper will not be auto-wired in Maker because Maker is not added to Spring application context. Maker is created using new keyword. Spring has no knowledge of it.
  • @Hima I get that. I am asking how to resolve this if I want to call Maker in the main method? Unable to Autowire static, unable to use 'new', whats the solution here?
  • you can check my answer there are couple of ways to @Autowire static beans @kang