I'm trying to use Spring and Jhipster to access some Database. I want to use a service, using injection.
When using a rest Controller, I have no problem, but when I'm trying to use an injected service in a pojo, this service is null.
@RestController
public class TheRestController {
@Autowired
protected TheService theService;
@RequestMapping("/test")
public void test() {
if (theService == null) {
System.out.println("rest : null");
}
{
System.out.println("Rest : service is not null");
}
System.out.println("Call Pojo ");
Pojo pojo = new Pojo();
pojo.process();
}
}
The service is not null and could be used to acces to the database. But when I try to use the Pojo, in the rest controller or directly in the main(), the Service is null.
My Pojo is exactly the same as the Rest controller :
public class Pojo {
@Autowired
protected TheService theService; // this one is allways null.
public void process(){
if (theService == null){
System.out.println( "POJO : null" );
}else{
System.out.println("POJO : service is not null");
}
}
}
When sending a request, I have the output :
Rest : service is not null
Call Pojo
POJO : null
I tried with @Autowired, @Resource and @Inject, same result.
how can I access to theService in a Pojo (without allways passing it as an a parameter) ?