Service instance null with @Autowired

0

Hello everyone. I'm doing a webservice REST SpringBoot application with JAVA. I need a method to run from time to time. For this, I'm using Quartz (org.quartz). The class method is executed at the time I want, normally. However, this method instantiates a controller to execute a search method on the database, and this search method is not working.

The instantiated controller class uses a Service interface with the @Autowired annotation. When I try to execute some method of the controller that uses this interface, it gives an error saying that it is null.

I have already tried to search the database directly for the class executed periodically, but it accuses the EntityManager is null.

StackTrace:

2016-01-21 12:59:28.596 ERROR 11284 --- [eduler_Worker-1] org.quartz.core.JobRunShell              : Job mainGroup.mainJob threw an unhandled Exception: 

java.lang.NullPointerException: null
at    br.com.conexaonfe.canais.app.controller.ControladorController.findByAppNServer(ControladorController.java:73) ~[classes/:na]
at br.com.conexaonfe.canais.app.controller.ControladorJob.execute(ControladorJob.java:39) ~[classes/:na]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.2.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.2.jar:na]

2016-01-21 12:59:28.596 ERROR 11284 --- [eduler_Worker-1] org.quartz.core.ErrorLogger              : Job (mainGroup.mainJob threw an exception.

ControllerJob (class that is run periodically by Quartz: link

ControllerController (controller class I spoke to): link

ControllerService (interface service used by the controller): link

ControllerServiceImpl (implementation of interface above): link

ControllerRepository (repository interface): link

ControllerRepositoryJpa (implementation of interface above): link

Controller: link

Main class of the application (the one that starts the quartz): link

Thank you in advance!

EDITED

Personally, I managed to find the solution by searching a lot. Here's the solution: link .

    
asked by anonymous 21.01.2016 / 16:26

1 answer

0

I researched a lot and was able to find the solution and the reason for the problem.

  • The reason:

      

    It is not possible to manually instantiate beans, as this is totally outside the IoC concept.

  • The solution:

      

    link

Link example:

B bean = new B();
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
factory.autowireBean( bean );
factory.initializeBean( bean, "bean" );
    
22.01.2016 / 12:34