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 文件内容:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
            factory-bean="clientFactory" factory-method="create" />
            class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
       
       
   


读取包内与包外的属性文件
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();
                }
            }
        }

你可能感兴趣的:(Jar访问包内与包外配置文件的方法与说明)