close
1.ApplicationContext的Initialize
目前知道有classpath的ClassPathXmlApplicationContext與File System的FileSystemXmlApplicationContext,可是很奇怪的是裡面的定位表示法可以互相使用,而且效果接近。
可以new ClassPathXmlApplicationContext("file:bin/resource/spring/**/*.xml") // 在Class Path使用File System
亦可new FileSystemXmlApplicationContext("classpath*:resource/spring/**/*.xml") // 在File System使用Class Path
上述/**/表示該目錄及其以下的子目錄都要拉進Context配置。所以衍生有趣的第二個筆記。此外還可以混搭如下:
new ClassPathXmlApplicationContext("file:bin/resource/spring/**/*.xml", "classpath*:resource/spring/**/*.xml");
new ClassPathXmlApplicationContext("file:bin/resource/spring/**/*.xml", "classpath*:resource/spring/**/*.xml");
2.Develop和Release的配置目錄差異
一般Eclipse會把編譯好的class連同配置檔一起output到bin目錄下,所以在Eclipse下運行測試時必須考慮bin/目錄。但釋出時,卻可能按實際需要改配置檔,所以配置檔不能包在jar裡,是故會是一個output.jar加一個配置目錄,如resources目錄等。定位表示法通常是常數,但有種方式不必寫死,就是使用static {}語法:
public static final String DEFAULT_CONTEXT; // 宣告,但必須在static { }對該常數初始化,否則編譯錯誤。
static {
String classPath = ShareObject.class.getProtectionDomain().getCodeSource().getLocation().getPath(); // 抓到目前類別定位路徑
String binPath = ""; // for Release 表示要用java -jar output.jar。
if (classPath.endsWith("bin/")) binPath = "bin/"; // for Develop 表示是在Eclipse下運行。
DEFAULT_CONTEXT = "file:" + binPath + "resources/spring/**/*.xml";
}
String classPath = ShareObject.class.getProtectionDomain().getCodeSource().getLocation().getPath(); // 抓到目前類別定位路徑
String binPath = ""; // for Release 表示要用java -jar output.jar。
if (classPath.endsWith("bin/")) binPath = "bin/"; // for Develop 表示是在Eclipse下運行。
DEFAULT_CONTEXT = "file:" + binPath + "resources/spring/**/*.xml";
}
3.ApplicationContext使用通配符的限制:
第一點所述 "classpath*:resource/spring/**/*.xml",不包括包在jar檔的xml,只能用全名方可生效。也就是目前在Spring 2.0.6無法適用類似Ant的通配符語法。
第一點所述 "classpath*:resource/spring/**/*.xml",不包括包在jar檔的xml,只能用全名方可生效。也就是目前在Spring 2.0.6無法適用類似Ant的通配符語法。
全站熱搜