Spring项目的类中获取ApplicationContext实例的方式

时间:2021-06-03 14:07:05   收藏:0   阅读:11

获取ApplicationContext实例的方式:

1.实现ApplicationContextAware接口

新增自定义类,并实现ApplicationContextAware接口,Spring加载该类时会自动调用setApplicationContext方法。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationUtil implements ApplicationContextAware{
    
    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // TODO Auto-generated method stub
        
        this.applicationContext = applicationContext;
        
    }

}

 

原文:https://www.cnblogs.com/labing/p/14844616.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!