'Spring failed to get bean from work thread

I have a ThreadPoolTaskExecutor bean. This bean is created during the program run;

I have a myWorker derived from runnable and inside the override run() method, it calls:

public void run() {
    MyEntity myEntity = m_applicationContext.getBean(MyEntity.class);
    ...
}

My program call the run() method from the executor like this:

MyWorker myWorker = new MyWorker();

ThreadPoolTaskExecutor threadPoolTaskExecutor = m_applicationContext.getBean(ThreadPoolTaskExecutor.class);
    

threadPoolTaskExecutor.execute(myWorker);

Then, from some machine, everything works fine. But from other machines, the MyEntity myEntity = m_applicationContext.getBean(MyEntity.class); failed, seems indicating the MyEntity bean creation failed.

How can that be?

Tried calling MyEntity myEntity = m_applicationContext.getBean(MyEntity.class); before excuting the work thread, then everything works fine.

Can anyone explain this? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source