Spring 讀取配置文件 及路徑問題

Spring容器最基本的接口就是BeanFactory. BeanFactory負責配置、創建、管理Bean,它有一個子接口ApplicationContext,也稱為Spring上下文。Spring容器負責管理Bean與Bean之間的信賴關系。

BeanFactory有很多實現類,通常使用org.springframework.beans.factory.xml.XmlBeanFactory類。但對于大部分J2EE應用而言,推薦使用ApplicationContext. ApplicationContext是BeanFactory的子接口,其常用實現類是org.springframework.context.support.FileSystemXmlApplicationContext和org.springframework.context.support.ClassXmlAplicationContext。

Springr的配置信息通常采用XML配置文件來設置,因此,創建BeanFactory實例時,應該提供XML配置文件作為參數。

下面詳細介紹ApplicationContext的實際運用:


一:ClassPathXmlApplicationContext

1.沒有前綴:默認為項目的classpath下相對路徑

ApplicationContext appCt = new ClassPathXmlApplicationContext("app.spring.xml");

2.前綴classpath:表示的是項目的classpath下相對路徑

ApplicationContext appCt = new ClassPathXmlApplicationContext("classpath:app.spring.xml");

3.使用前綴file 表示的是文件的絕對路徑

ApplicationContext appCt = new ClassPathXmlApplicationContext("file:D:/app.spring.xml");

4.可以同時加載多個文件

String[] xmlCfg = new String[] { "classpath:base.spring.xml","app.spring.xml"};

ApplicationContext appCt = new ClassPathXmlApplicationContext(xmlCfg);

5.使用通配符加載所有符合要求的文件

ApplicationContext appCt = new ClassPathXmlApplicationContext("*.spring.xml");

二:FileSystemXmlApplicationContext

1.默認為項目工作路徑 即項目的根目錄

ApplicationContext appCt2 = new FileSystemXmlApplicationContext("src/main/resources/app.spring.xml");

2.前綴classpath:表示的是項目的classpath下相對路徑

ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:app.spring.xml");

3.使用前綴file 表示的是文件的絕對路徑

ApplicationContext appCt2 = new FileSystemXmlApplicationContext("file:D:/app.spring.xml");

ApplicationContext appCt2 = new FileSystemXmlApplicationContext("D:/app.spring.xml");

4.可以同時加載多個文件

String[] xmlCfg = new String[] { "src/main/resources/base.spring.xml","classpath:app.spring.xml"};

ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);

5.使用通配符加載所有符合要求的文件

ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:*.spring.xml");




importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importorg.springframework.context.support.FileSystemXmlApplicationContext;

importaoplog.LogAfterAdvice;

importaoplog.LogBeforeAdvice;

/**

*?@author?Michael

*

*/

publicclassTestApplicationContext?{

/**

*?@param?args

*/

publicstaticvoidmain(String[]?args)?{

/**

*?ClassPathXmlApplicationContext

*/

//?沒有前綴:默認為項目的classpath下相對路徑

ApplicationContext?appCt?=newClassPathXmlApplicationContext(

"app.spring.xml");

//?前綴classpath:表示的是項目的classpath下相對路徑

//?ApplicationContext?appCt?=?new?ClassPathXmlApplicationContext(

//?"classpath:app.spring.xml");

//?使用前綴file?表示的是文件的絕對路徑

//?ApplicationContext?appCt?=?new?ClassPathXmlApplicationContext(

//?"file:D:/app.spring.xml");

LogBeforeAdvice?logBefore?=?(LogBeforeAdvice)?appCt

.getBean("logBefore");

System.out.println("ClassPathXmlApplicationContext?test:"

+?logBefore.getClass());

//?利用通配符文件加載

ApplicationContext?appCtXx?=newClassPathXmlApplicationContext(

"*.spring.xml");

//?多文件加載

String[]?xmlCfg?=newString[]?{"classpath:base.spring.xml",

"myapp.spring.xml"};

ApplicationContext?appCtMore?=newClassPathXmlApplicationContext(

xmlCfg);

/*

*?FileSystemXmlApplicationContext

*/

//?默認為項目工作路徑?即項目的根目錄

ApplicationContext?appCt2?=newFileSystemXmlApplicationContext(

"src/main/resources/app.spring.xml");

//?前綴classpath:表示的是項目的classpath下相對路徑

//?ApplicationContext?appCt2?=?new?FileSystemXmlApplicationContext(

//?"classpath:app.spring.xml");

//?使用前綴file?表示的是文件的絕對路徑

//?ApplicationContext?appCt2?=?new?FileSystemXmlApplicationContext(

//?"file:D:/app.spring.xml");

LogAfterAdvice?logAfter?=?(LogAfterAdvice)?appCt2.getBean("logAfter");

System.out.println("FileSystemXmlApplicationContext?test:"

+?logAfter.getClass());

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容