最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中ApplicationContextAware的使用方法詳解

 更新時間:2023年03月22日 08:47:30   作者:霞光里  
ApplicationContextAware?通過它Spring容器會自動把上下文環(huán)境對象調(diào)用ApplicationContextAware接口中的setApplicationContext方法,這篇文章主要介紹了Spring中ApplicationContextAware的作用,需要的朋友可以參考下

ApplicationContextAware 通過它Spring容器會自動把上下文環(huán)境對象調(diào)用ApplicationContextAware接口中的setApplicationContext方法。

我們在ApplicationContextAware的實現(xiàn)類中,就可以通過這個上下文環(huán)境對象得到Spring容器中的Bean。

  看到—Aware就知道是干什么的了,就是屬性注入的,但是這個ApplicationContextAware的不同地方在于,實現(xiàn)了這個接口的bean,當spring容器初始化的時候,會自動的將ApplicationContext注入進來: 
使用方法如下:

1.實現(xiàn)ApplicationContextAware接口:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.co.ayz.rpc.registry.ServiceRegistry;

public class RpcServer implements ApplicationContextAware{

    private ApplicationContext context;

        @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        // TODO Auto-generated method stub
        context = applicationContext;       
    }   
     //獲得applicationContext
    public static ApplicationContext getApplicationContext() {
        //assertContextInjected();
        return context;
    }    
    public static void clearHolder(){
        context=null;
    }
    //獲取Bean
    public static <T> T getBean(Class<T> requiredType){
        //assertContextInjected();
        return (T) getApplicationContext().getBean(requiredType);
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name){
        assertContextInjected();
        return (T) getApplicationContext().getBean(name);
    }    
    //判斷application是否為空
    public static void assertContextInjected(){
        Validate.isTrue(context==null, "application未注入 ,請在springContext.xml中注入SpringHolder!");
    }
}

因為我們在做開發(fā)的時候,并不是說在每一個地方都能將屬性注入到我們想要的地方去的,比如在Utils使用到dao,我們就不能直接注入了,這個時候就是我們需要封裝springContext的時候了,而ApplicationContextAware就起了關(guān)鍵性的作用。

自己寫的demo:

/**  
* @Title: SpringJobBeanFactory.java
* @Package com.founder.mrp.job
* @Description: TODO
* @version V1.0  
*/

package com.founder.mrp.job;

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

@Component
public class SpringJobBeanFactory implements ApplicationContextAware {

    
    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringJobBeanFactory.applicationContext=applicationContext;
        
    }
     public static ApplicationContext getApplicationContext() {
            return applicationContext;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException {
            if (applicationContext == null){
                return null;
            }
            return (T)applicationContext.getBean(name);
      }
}


使用:
TypeSetErpService typeSetErpServ = SpringJobBeanFactory.getBean("typeSetErpServiceImpl");

到此這篇關(guān)于Spring中ApplicationContextAware的作用的文章就介紹到這了,更多相關(guān)Spring中ApplicationContextAware作用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

南召县| 武山县| 弋阳县| 阳西县| 襄城县| 综艺| 蕉岭县| 泽州县| 光山县| 侯马市| 建始县| 漳州市| 色达县| 灯塔市| 宁津县| 武功县| 太和县| 慈利县| 冕宁县| 泽普县| 清徐县| 延津县| 湘乡市| 宣汉县| 纳雍县| 石阡县| 日土县| 彰化县| 阿勒泰市| 佳木斯市| 兰西县| 衢州市| 舟山市| 蛟河市| 郓城县| 大丰市| 金华市| 如皋市| 阜平县| 奎屯市| 肇庆市|