1.环境
目前开发的项目使用的spring boot(2.1.4.RELEASE)+ssm
2. 需求
现在有一个数据处理任务的接口,在spring boot项目启动后,可以手动的去启动任务,但是这样比较麻烦,每次项目重新运行都要去通过swagger调用数据处理的接口.所以 现在想在项目启动后就是调用该方法
3. 解决思路
当然spring给我们提供了方法:在SpringBoot中,有两种接口方式实现启动执行,分别是ApplicationRunner和CommandLineRunner,除了可接受参数不同,其他的基本一样.网上有很多例子.就不多说了,作者选用的是实现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方法.没有出现空指针异常.