Spring源碼解析——ApplicationContext介紹

相比於BeanFactory接口,我們更多時候使用ApplicationContext接口來加載Bean,兩個接口都是來加載Bean的,相比之下ApplicationContext提供了更多擴展功能。使用ApplicationContext方式加載XML

ApplicationContext ctx = new ClassPathXmlApplicationContext("app.xml");

以ClassPathXmlApplicationContext.java爲切入點

//其中configLocations爲數組形式的xml形式的配置文件路徑
    public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
        this(configLocations, true, null);
    }

    public ClassPathXmlApplicationContext(
            String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
            throws BeansException {

        super(parent);
      //todo 設置配置文件路徑
        setConfigLocations(configLocations);
        if (refresh) {
          //加載刷新配置,可能是xml文件也可能是其他屬性文件
            refresh();
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章