栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

在静态方法中调用Spring注入的类的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在静态方法中调用Spring注入的类的方法

在静态方法中调用Spring注入的类的方法

@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(注释的方法)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/705047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号