[英]Can't use @Autowired JPA Repository in Main method of Spring Boot application
I've autowired a JPA repository that adds dummy data in my H2 database before the application starts.我已经自动装配了一个 JPA 存储库,它在应用程序启动之前在我的 H2 数据库中添加了虚拟数据。 But is there a reason as to why I can't use it in the main () method but am able to use it in the runner() method?
但是,为什么我不能在 main () 方法中使用它,但可以在 runner() 方法中使用它,有什么原因吗?
@SpringBootApplication
public class FullstackApplication {
@Autowired
private CarRepository carRepository;
private static final Logger logger = LoggerFactory.getLogger(FullstackApplication.class);
public static void main(String[] args) {
carRepository. // Here I get a compilation error: Cannot make a static reference to a non-static field
SpringApplication.run(FullstackApplication.class, args);
}
@Bean
CommandLineRunner runner(){
return args -> {
// Save demo data to database
carRepository.save(new Car("Ford", "Mustang", "Red",
"ADF-1121", 2017, 59000));
carRepository.save(new Car("Nissan", "Leaf", "White",
"SSJ-3002", 2014, 29000));
carRepository.save(new Car("Toyota", "Prius", "Silver",
"KKO-0212", 2018, 39000));
};
}
}
You are accessing a non static field directly from static method which is not permitted in java
您正在直接从 java 中不允许的静态方法访问非静态字段
Also you cannot make static field @Autowired
你也不能制作静态字段
@Autowired
so if you do this所以如果你这样做
@Autowired
private static CarRepository carRepository;
it won't throw any error but it will be ignored.它不会抛出任何错误,但会被忽略。
Main method is marked static, which means, everything that is used there should be either static as well , or be manually instantiated. Main 方法被标记为静态,这意味着,在那里使用的所有方法也应该是静态的,或者手动实例化。
You do not instantiate CarRepository manually in static body of main method, you are relying on Spring to instantiate it somewhere during its startup phase , which will happen after "carRepository. //...." this line.您没有在 main 方法的静态主体中手动实例化 CarRepository,您依赖 Spring 在启动阶段的某个地方实例化它,这将在“carRepository. //....”这一行之后发生。
That is why , you cannot use carRepository in this exact place, because it is not static by itself and in was not manually instantiated.这就是为什么你不能在这个确切的地方使用 carRepository,因为它本身不是静态的,并且没有手动实例化。
In CommandRunner though , at the time return is being called , instance of CarRepository is already created by Spring and autowired to field, because Spring startup already has finished , and it can be easily used.但是在CommandRunner中,在调用return的时候,CarRepository的实例已经被Spring创建并自动绑定到field,因为Spring启动已经完成,可以很方便的使用。
Well main method is marked as static and you cannot access non static members from a static method.好吧,主要方法被标记为静态,您不能从静态方法访问非静态成员。
To just solve that you have to mark the carRepository static.为了解决这个问题,您必须将 carRepository 标记为静态。 But since static field cannot be autowired it will ignore it and you will not get the object.
但是由于静态字段不能自动装配,它会忽略它并且你不会得到对象。
It will work in command runner because at that time the sprint start up is already finished and beans has been instantiated.它将在命令运行器中工作,因为那时 sprint 启动已经完成并且 bean 已经被实例化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.