spring boot 項目在啓動時調用接口

1.環境

目前開發的項目使用的spring boot(2.1.4.RELEASE)+ssm

2. 需求

現在有一個數據處理任務的接口,在spring boot項目啓動後,可以手動的去啓動任務,但是這樣比較麻煩,每次項目重新運行都要去通過swagger調用數據處理的接口.所以 現在想在項目啓動後就是調用該方法

3. 解決思路

當然spring給我們提供了方法:在SpringBoot中,有兩種接口方式實現啓動執行,分別是ApplicationRunnerCommandLineRunner,除了可接受參數不同,其他的基本一樣.網上有很多例子.就不多說了,作者選用的是實現CommandLineRunner接口,重寫了run 方法.

4. 例子

  • controller
@EnableConfigurationProperties({Person.class})
@RestController
public class HelloController {

    @Autowired
    Person person;


    @GetMapping("/getValue")
    public String getValue(){
        String name = person.getName();
        int age = person.getAge();
        return "name="+name+","+"age="+age;
    }
 }

這裏我自定義一個Person類,並且定義了一個person.properties配置文件,讀取其中的值,配置如下:

  • person
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@EnableConfigurationProperties({Person.class})
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • person.properties
person.name=zhangsan
person.age=20

注意:這裏涉及到幾個註解:

@EnableConfigurationProperties({Person.class})
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})

大家有問題可以學習下

  • 自定義類實現CommandLineRunner
@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        HelloController helloController = new HelloController();
        helloController.getValue();
    }
}

5 問題

這時候以爲可以完美解決,但是出現了錯誤:

2019-09-26 18:45:48.825 ERROR 4724 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at com.example.springboot_train.SpringbootTrainApplication.main(SpringbootTrainApplication.java:15) [classes/:na]
Caused by: java.lang.NullPointerException: null
	at com.example.springboot_train.controller.HelloController.getValue(HelloController.java:50) ~[classes/:na]
	at com.example.springboot_train.runner.MyCommandLineRunner.run(MyCommandLineRunner.java:12) ~[classes/:na]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	... 5 common frames omitted

2019-09-26 18:45:48.828  INFO 4724 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

竟然出現了空指針異常

6 解決思路

我再run方法中new 了一個controller 層中的類:
HelloController helloController = new HelloController();
而這個類依賴其他類並且使用了@Autowired注入的Spring Bean.那這就不奇怪了,那這個錯誤空指針就不爲過了,因爲使用的是new而不是spring幫我們自動注入的,那肯定會出現這個錯誤,因此查看了Springboot中new出來的實例和@Autowired注入的區別:那肯定要換成@Autowired註解的方式,交給spring容器去管理.
其實從這個錯誤中更加認識到什麼是spring容器了.

7 解決方案

import com.example.springboot_train.controller.HelloController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {
    
    @Autowired
    HelloController helloController;
    @Override
    public void run(String... args) throws Exception {
        //HelloController helloController = new HelloController();
        helloController.getValue();
    }
}

問題解決,項目啓動時調用了getValue方法.沒有出現空指針異常.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章