JAVA本地程序使用FileSystemXmlApplicationContext讀取application-datasorce.xml進行數據庫操作


使用絕對路徑讀取,就絕對不會出錯

程序代碼如下,這個類是BookDaoTest

 1        String appContextPath = BookDaoTest.class.getClassLoader().getResource("").getPath() + "../";
 2             File file = new File(appContextPath);
 3             String abPath = null;
 4             try {
 5                 abPath = file.getCanonicalPath();
 6             } catch (IOException e) {
 7                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 8             }
 9             String[] ctxConfig = {"file:" + abPath + "/applicationContext-beans.xml", "file:" + abPath + "/applicationContext-datasource.xml", "file:" + abPath + "/applicationContext-security.xml"};
10             ApplicationContext context = new FileSystemXmlApplicationContext(ctxConfig);
11 
12             BookDao bookDao = (BookDao) context.getBean("bookDao");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM