简体   繁体   English

无法在 Spring Boot 应用程序的 Main 方法中使用 @Autowired JPA Repository

[英]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.

相关问题 Spring Boot应用程序中的JUnit测试中没有自动装配JPA存储库的合格Bean - No qualifying bean for autowired JPA repository in JUnit test in Spring Boot application Spring Boot @Autowired Jpa存储库返回Null - Spring Boot @Autowired Jpa repository returns Null Spring @Autowired无法连线Jpa存放区 - Spring @Autowired can not wire Jpa repository 如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 spring 服务中 - How to implement Generic JPA Repository in Spring Boot - Which can be autowired into spring services for any entity/class type Spring数据jpa存储库注入失败,在Spring启动时使用@Autowired - spring data jpa repository inject fails using @Autowired in spring boot 为什么我的Spring Boot自动连接的JPA存储库未通过JUnit测试? - Why is my Spring Boot autowired JPA Repository failing JUnit test? 在 Spring Boot 存储库中使用自动装配 bean 的自定义删除方法 - custom delete method using autowired bean in spring boot repository Spring Boot @Autowired无法初始化类 - Spring boot @Autowired can't initialise class 此时如何在Query上使用? -JPA存储库春季启动 - How to use at Query at this? - JPA repository spring boot 我可以在 Spring boot 自定义验证器中自动连接一个存储库吗 - Can I Autowired one Repository inside Spring boot custom validator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM