在静态方法中调用Spring注入的Bean的方法
@Component
public class FileUtil {
@Autowired
FileConfig fileConfig;
@Autowired
private static FileConfig staticFileConfig;
@PostConstruct
public void init(){
staticFileConfig = fileConfig;
}
public static void test(){
String path = staticFileConfig.getPath();//getPath()是FileConfig中的方法
}
}
-
@PostConstruct 该注解被用来修饰一个非静态的 void() 方法。
-
被 @PostConstruct 修饰的方法会在服务器加载 Servlet 的时候运行,并且只会被服务器执行一次。
-
@PostConstruct 在构造函数之后执行,init() 方法之前执行。
-
该注解的方法在整个Bean初始化中的执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)