java - Spring:为什么用@PostConstruct 注释的方法不能是静态的?

标签 java spring spring-boot postconstruct

我正在阅读本网站上有关 @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/

相关文章:

java - 出于 Character.isWhitespace 的目的,Java 是否将 'normal' 空间视为空白?

java - 将 CSV 文件读入不同类型的数组,然后读入 Arraylist

spring - 如果出现异常,如何覆盖 Spring Batch CompositeItemWriter 管理委托(delegate)编写者的事务?

java - Springboot 复选框值到 Controller

mysql - 我无法用小写执行 Sql UpperCase

java - 使用 Spring,如何手动触发 AuthenticationSuccessHandler

java - TextView setContentDescription 不起作用

java - 我无法使用Spring MVC 3访问jsp页面

database - JDBCTemplate with TransactionTemplate and Connection Pool,使用哪个数据源

Spring Boot 数据 JPA : Hibernate Session issue