我正在阅读本网站上有关 @PostConstruct
的文档:https://www.baeldung.com/spring-postconstruct-predestroy
是这样写的:
The method annotated with @PostConstruct can have any access level but it can't be static.
谁能告诉我为什么用这个注解注解的方法不能是静态的?
最佳答案
好吧,方法的名称已经说明了它的作用。
PostConstruct,这个方法会在构造器之后调用。它不能是静态的,因为静态方法不能访问非静态变量、方法等。
如果你需要静态的东西只运行一次,你可以使用静态 block 。
关于java - Spring:为什么用@PostConstruct 注释的方法不能是静态的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64698406/