使用绝对路径读取,就绝对不会出错

程序代码如下,这个类是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");
posted on 2012-08-18 00:21  ZimZz  阅读(3508)  评论(0编辑  收藏  举报