Can’t use @Autowired JPA Repository in Main method of Spring Boot application

huangapple go评论86阅读模式
英文:

Can't use @Autowired JPA Repository in Main method of Spring Boot application

问题

我已经为你翻译好了内容,如下:

@SpringBootApplication
public class FullstackApplication {

	@Autowired
	private CarRepository carRepository;

	private static final Logger logger = LoggerFactory.getLogger(FullstackApplication.class);

	public static void main(String[] args) {
		carRepository. // 这里会出现编译错误:无法对非静态字段进行静态引用
		SpringApplication.run(FullstackApplication.class, args);
	}

	@Bean
	CommandLineRunner runner(){
		return args -> {
			// 向数据库保存演示数据
			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));
		};
	}
}
英文:

I've autowired a JPA repository that adds dummy data in my H2 database before the application starts.
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?

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

答案1

得分: 2

你正在从静态方法直接访问非静态字段,在Java中是不允许的。

另外,你不能将静态字段设为 @Autowired

因此,如果你这样做:

@Autowired
private static CarRepository carRepository;

不会抛出任何错误,但它将被忽略。

英文:

> You are accessing a non static field directly from static method
> which is not permitted in java

Also you cannot make static field @Autowired

so if you do this

@Autowired
  private static CarRepository carRepository;

it won't throw any error but it will be ignored.

答案2

得分: 2

主方法被标记为静态,这意味着在那里使用的一切要么也应该是静态的,要么要手动实例化。

你不能在主方法的静态块中手动实例化CarRepository,你依赖于Spring在启动阶段的某个地方实例化它,这将发生在"carRepository. //...."这一行之后。

这就是为什么你不能在这个确切的地方使用carRepository,因为它本身不是静态的,也没有被手动实例化。

但是在CommandRunner中,当调用return时,CarRepository的实例已经被Spring创建并自动连接到字段,因为Spring的启动已经完成,可以轻松使用它。

英文:

Main method is marked static, which means, everything that is used there should be either static as well , or be manually instantiated.

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.

That is why , you cannot use carRepository in this exact place, because it is not static by itself and in was not manually instantiated.

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.

答案3

得分: 1

主方法被标记为静态,因此您无法从静态方法访问非静态成员。

为了解决这个问题,您需要将carRepository标记为静态。但是由于静态字段不能被自动装配,它将被忽略,您将无法获得该对象。

它会在命令行运行器中工作,因为在那时,Spring的启动已经完成,bean已经被实例化。

英文:

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. 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.

huangapple
  • 本文由 发表于 2020年8月24日 18:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63559453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定