Search code examples
javaoracle-databasemodel-view-controllerdao

Java mvc write to db from thread


I'm using DAO implementation to write to DB. In service code I get accesses to DAO by using this code:

@Autowired
    protected myDAO myDao; 

Then for example to insert new row to DB I'm using:

myDao.insertFunction();

How can I insert new row in Thread class?

implements Runnable

Thanks


Solution

  • If you are starting a new thread by hand, then you won't have spring beans autowiring available, since that thread is not managed by Spring.

    You can get the MyDAO bean from the thread by using ApplicationContext.getBean(Class clazz) method.