[英]Loading a jar's context.xml from classpath
我正在嘗試將jar內的應用程序上下文作為插件加載。 我用它來加載上下文:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**my-context.xml");
當我通過pom.xml加載jar時,它可以正常工作。
然后,我使用eclipse而不是maven將其直接添加到類路徑中,以避免每次都編譯(最終目標是tomcat中的共享lib文件夾,也無法正常工作)。 現在spring無法找到它並返回默認上下文(無例外)
我使用以下命令檢查它是否正確插入到類路徑中:
InputStream in1 = this.getClass().getClassLoader().getResourceAsStream("my-context.xml");
有用。
我檢查了日志。 使用pom.xml,spring可以在jar中正確搜索
Searching directory [...target\classes\META-INF\maven\x.y.z] for files matching pattern [...\x.y.z/target/classes/**/my-context.xml]
Searching directory [...ehealth.poc.module1] for files matching pattern [D:/JRB/Projects/Vivates/workspaces/default/extcom/ehealth.poc.module1/target/classes/**/ecm-context.xml]
...
Resolved location pattern [classpath*:**/my-context.xml] to resources [file [...\target\classes\my-context.xml]]
Loading XML bean definitions from file [...\target\classes\my-context.xml]
...
在第二種情況下,關於我的jar的日志中沒有任何內容。
為什么當我使用maven或直接使用classpath時spring沒有相同的行為? 除了在類路徑中簡單添加依賴項位置之外,我還想做其他事情嗎?
最后,我們在eclipse上找到了解決方案。
問題來自**中的**
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**my-context.xml");
看來**不會掃描.jar文件。 設置直接路徑有效:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:my-context.xml");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.