Spring: Null with @Inject

My Spring got rusty; and I’ve made a common mistake.  Surprisingly it was hard to find a solution to it; so I am providing one here in hope that it helps others.


I defined a class; that I wanted to be managed by spring.

public class AIConductorWorkflowStarter {

    AmazonSimpleWorkflow workflow;

Then I wanted to test it; so wrote the following in the JUnit test class:

public void startWorkflowSuccessfullyTest() throws Exception {

AIConductorWorkflowStarter starter = new AIConductorWorkflowStarter();          starter.startWorkflow(String.valueOf(System.currentTimeMillis()), "this is a descripton of a job");

Naturally, this wasn’t the right way of doing it so I got the null exception (nullpointerexception).


The reason for the exception is quite straightforward: “Spring cannot autowire / inject beans it doesn’t control (i.e. created with new)” [ref].

The fix is simple.

  • Add to AppConfig.java bean of your class; in my case:
    public AIConductorWorkflowStarter getAIConductorWorkflowStarter(){
        return new AIConductorWorkflowStarter();
  • Inject your bean into the Test class (or the class where you plan to use it):


public class AIConductorWorkflowStarterTest {

    AIConductorWorkflowStarter starter;




Bread Crumbs:

Searched for spring bean 10:20am
java – What in the world are Spring beans?… – stackoverflow.com 10:19am
2.2. @Bean – Spring – spring.io 10:20am
Searched for spring bean mkyong 10:20am
Spring Tutorial – Mkyong.com – mkyong.com 10:20am
Spring Auto-Wiring Beans with @Autowired… – mkyong.com – Viewed 6 times 10:20am
Searched for spring4 autowire inject 10:14am
java – What is the difference between… – stackoverflow.com 10:15am
3.9 Annotation-based container configuration… – spring.io 10:15am
Spring Beans Auto-Wiring – Tutorials Point – tutorialspoint.com 10:15am
Spring 3 and JSR-330 @Inject and @Named… – mkyong.com 10:15am
Searched for spring 4 autowire inject 10:14am
java – @Autowire not preperly injected… – stackoverflow.com 10:14am
java – Spring Autowired usage – Stack Overflow – stackoverflow.com 10:14am
4.11 Annotation-based configuration – Spring – spring.io 10:14am
Difference Between @Resource, @Autowired… – javabeat.net 10:14am
Flowstopper: Dependency Injection: @Resource,… – flowstopper.org 10:14am
Searched for spring appconfig 10:07am
Spring 3 JavaConfig example – Mkyong.com – mkyong.com – Viewed 8 times 9:56am
Searched for spring 4 10:05am
Announcing Spring Framework 4.0 GA Release – spring.io – Viewed 2 times 10:05am
Searched for spring 4 appconfig 10:05am
Searched for spring specify appconfig class 9:57am
java – Spring 3.1: How do I inject a… – stackoverflow.com – Viewed 2 times 9:57am
Configuration Simplifications in Spring 3.0 – spring.io 9:57am
Spring Java Based Configuration – Tutorials… – tutorialspoint.com
Searched for spring “@Inject” AppConfig null 9:52am
Configuration – Spring – spring.io 9:53am
spring – How to @Inject A Bean into a… – stackoverflow.com 9:53am
spring 3.1 configuration: environment… – stackoverflow.com 9:53am
Searched for spring does not get injected null 9:50am
java – Spring Dependency Injection Autowiring… – stackoverflow.com 9:51am
java – Spring Injected Bean property… – stackoverflow.com 9:51am
dependency injection – Spring DI – Autowired… – stackoverflow.com 9:51am
dependency injection – Spring: Value… – stackoverflow.com 9:51am


About Neil Rubens

see http://ActiveIntelligence.org
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *