SpringBootApplication不会自动连线我的服务 [英] SpringBootApplication does not Autowire my Services

查看:103
本文介绍了SpringBootApplication不会自动连线我的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在进行Spring Boot初始化时遇到麻烦.我在一个简单的Spring Boot项目中就有这个结构.

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)

我尝试在SpringBootApplication注释中设置scanBasePackages属性,但是不起作用.无论如何,我都带有@Bean注释,并且我看到Spring Boot正确地将其注入了应用程序,因为这样运行应用程序时,我可以看到日志:

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'

我的AppClass.java的基本方案

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.

推荐答案

Spring无法@Autowire静态字段,请使用ApplicationContext来获取bean

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();
    }
}

或者您可以使用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();
    }     
}

这篇关于SpringBootApplication不会自动连线我的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆