I am having troubles with my Spring Boot initialization. I have this structure in a simple Spring Boot project.
com.project.name
|----App.java (Annoted with @SpringBootApplication and Autowire MyCustomService)
|----com.project.name.service
|----MyCustomService.java (Annoted with @Service)
I tried setting scanBasePackages
property in the SpringBootApplication Annotation but does not work. Anyway I have an @Bean
annotated and I see that Spring Boot inject it correctly in the app because I can see the log when I run the application like this:
2019-03-09 15:23:47.917 INFO 21764 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'jobLauncherTaskExecutor'
...
2019-03-09 15:23:51.775 INFO 21764 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'jobLauncherTaskExecutor'
A basic scheme of my AppClass.java
@SpringBootApplication(
exclude = { DataSourceAutoConfiguration.class }
//,scanBasePackages = {"com.project.name.service"}
)
public class App{
private static Logger logger = LoggerFactory.getLogger(App.class);
@Autowired
private static MyCustomService myCustomService;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
...
myCustomService.anyMethod();//NullPointerException
}
}
@Bean
public ThreadPoolTaskExecutor jobLauncherTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(25);
return executor;
}
I guess that I am missing something but I am reading some guides and does not find anything about this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…