Jar访问包内与包外配置文件的方法与说明
访问Jar包外的配置文件 ApplicationContext context = new FileSystemXmlApplicationContext( new String{"client-beans.xml"}); //说明:指定为与Jar所在目录中的 client-beans.xml文件 //需要说明的是在运行或生成(打包)时不会将 client-beans.xml 复制到指定的目录,需要手工复制
client = (HelloWord) context.getBean("client");
访问Jar包内的配置文件 ApplicationContext context = new ClassPathXmlApplicationContext( new String{"client-beans.xml"}); //说明:指定为Jar包中根目录下的 client-beans.xml文件 client = (HelloWord) context.getBean("client"); 相关ClassPathXmlApplicationContext与 FileSystemXmlApplicationContext请访问 http://hi.baidu.com/mark58/blog/item/64691708113b9f9d0a7b82cb.html client-beans.xml 文件内容:
读取包内与包外的属性文件 String path = "a.properties"; config = new Properties(); FileInputStream fis = null; boolean result = false; try { //下面一行是读取包内对应的文件 config.load(this.getClass().getClassLoader().getResourceAsStream(path)); result = true; } finally { if (fis != null) { try { fis.close(); } catch (Exception e) { e.printStackTrace(); } } }