Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

java - SpringBootApplication does not Autowire my Services

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Spring cannot @Autowire static fields, use ApplicationContext to get the bean

@SpringBootApplication(
    exclude = { DataSourceAutoConfiguration.class }
    //,scanBasePackages  = {"com.project.name.service"}
)

public class App{

    private static Logger logger = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(App.class, args);

        MyCustomService myCustomService = (MyCustomService)context.getBean("myCustomService");
        ...

        myCustomService.anyMethod();
    }
}

Or you can use CommandLineRunner

 @SpringBootApplication(
    exclude = { DataSourceAutoConfiguration.class }
    //,scanBasePackages  = {"com.project.name.service"}
)

public class App implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(App.class);

    @Autowired
    private MyCustomService myCustomService;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    public void run(String... args){
        myCustomService.anyMethod();
    }     
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...