I need a little help.
I'm doing some tests with Spring Boot and I have my services + some processes that I want to run in thread. Each thread will run its own service.
When I do service dependency injection for my main class and step into the thread constructor, everything works the way I want.
However, I do not like the way it is implemented and I believe there is a more beautiful way of doing this, ie the thread itself injects the service it will use.
How are you today?
Main Class:
@Autowired
ITesteService service;
public static void main(String[] args) {
SpringApplication.run(BaseUnicaApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new TesteProcessor(service));
}
Thread
public class TesteProcessor implements Runnable {
private ITesteService service;
public TesteProcessor() {
this.service = service;
}
public void run() {
service.save
I have tried to make the dependency injection direct in the service, but NullPointer occurs when I use the service.
public class TesteProcessor implements Runnable {
@Autowired
private ITesteService service;
public TesteProcessor() {
}
public void run() {
service.save
}
What would be the best way to do this?
Thank you