19

When I run TaskJob I am getting null pointer exception because Spring doesn't autowiring serviceJob service. Is new thread causing this problem because Spring autowired mysqlService without any problem?

public class TaskJob implements Runnable {
    @Autowired
    private ServiceJob serviceJob;

    String name;
    String source;

    public TaskJob(String name, String source) {
        this.name = name;
        this.source = source;
    }

    public void run() {
        serviceJob.run();
    }
}

@Service
public class ServiceJob extends BaseJob{

    @Autowired
    private MysqlService mysqlService;

    public void run(){
    ....
    }
}

@Service
public class MysqlService {
...
}

My applicationContext.xml;

<context:component-scan base-package="cm.*" /> 

And my classes are;

cm.tasks.jobs.TaskJob
cm.jobs.ServiceJob
cm.services.MysqlService;

EDIT: TaskJob instanciated with;

TaskJob taskJob = new TaskJob(name, source);
Thread taskThread = new Thread(taskJob);
taskThread.start();
hellzone
  • 5,393
  • 25
  • 82
  • 148
  • Can you provide us some [MCVE](http://stackoverflow.com/help/mcve), because you're not giving us enough intel to help you – DamCx Feb 28 '17 at 07:14
  • 1
    How is `TaskJob` instanciated? – Jens Feb 28 '17 at 07:19
  • @Jens I edited my answer. – hellzone Feb 28 '17 at 07:24
  • 1
    Autowiring happens only if Spring itself instantiates the class. You have to define a bean and then ask Spring (from a Spring-aware class) to make one for you. You cannot use `new` and expect Spring to autowire anything. I'm sure this is a dup but I cannot find it right now. – Jim Garrison Feb 28 '17 at 07:25

3 Answers3

34

Spring only autowires components it creates. You are calling new TaskJob(), Spring doesn't know about this object so no autowiring will take place.

As a workaround you can call the application context directly. Firstly get a handle on the application context. This can be done by adding @Autowire for the application context itself.

@Autowired
private ApplicationContext applicationContext;

When you create TaskJob, ask the app context to do your auto-wiring.

TaskJob taskJob = new TaskJob(name, source);
applicationContext.getAutowireCapableBeanFactory().autowireBean(taskJob);

Additionally if you have any @PostConstruct annotated methods you need triggering you can call initializeBean()

applicationContext.getAutowireCapableBeanFactory().initializeBean(taskJob, null);
Adam
  • 35,919
  • 9
  • 100
  • 137
  • I don't get why It doesn't autowire. I mean yes I am calling new TaskJob() but not new ServiceJob(). It is a little confusing. Thanks for your answer. – hellzone Feb 28 '17 at 07:31
  • 1
    When you call new TaskJob() you're asking the JVM to create a new object of type TaskJob. Spring is simply not involved. If you look closely you're probably not calling new ServiceJob()... You're more likely to be using a ServiceJob which is created via the Spring package scan... – Adam Feb 28 '17 at 07:33
  • @Adam really helpful – Ganesh Gudghe Mar 30 '22 at 05:01
2

Your TaskJob is instantiated wihh "new" operator which means the object created is not a spring bean. So you will have to write code to create object for the property (ServiceJob) with the new operator.

While using Spring spring framework Service objects are not created like this. Kindly use getBean method of Applicationcontext. Please see here

subir
  • 310
  • 4
  • 13
-4

Please Try this type

<context:component-scan base-package="cm.*,cm.tasks.jobs" /> 
Rob
  • 26,989
  • 16
  • 82
  • 98
BalaMurugan.N
  • 120
  • 1
  • 1
  • 10
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/15365060) – viral Feb 28 '17 at 09:05
  • @iOSAppDev Why doesn't it? – Bhargav Rao Mar 29 '17 at 08:51
  • @BhargavRao Well, it doesn't solve the problem in question. Hence, it doesn't. – viral Mar 30 '17 at 06:23
  • 2
    Then @iOSAppDev [You are doing it wrong](https://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue). Please note that you should NOT be deleting wrong answers in the LQPQ but only non answers. – Bhargav Rao Mar 30 '17 at 07:01