• Java加载资源文件几种方法


    from: http://andyzhu.blog.51cto.com/4386758/775836/

    import java.net.URL; 
     
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
     
    public class TestMain { 
        public static void main(String[] args) { 
     
            // ############################################################################################################ 
            // 1:使用本类的Class类的getResource()方法 
            // 在当前包寻找资源(指定相对路径,其他均返回null。) 
            URL filePathUrl1 = TestMain.class.getResource("beans_sameLocation.xml"); 
     
            // 在根寻找资源(需要文件分隔符"/",其他均返回null。) 
            URL filePathUrl2 = TestMain.class.getResource("/beans.xml"); 
     
            // 在不同包内寻找资源(指定相对路径(需要文件分隔符"/"),其他均返回null。) 
            URL filePathUrl3 = TestMain.class.getResource("/test/spring/beanpost/file/beans_diffLocation.xml"); 
     
            // ############################################################################################################ 
            // 2:使用本类的Class类的ClassLoader类的getResource()方法 
            // 在相同包内寻找资源,总是返回null。 
            // URL filePathUrl3 = 
            // TestMain.class.getClassLoader().getResource("beans_sameLocation.xml"); 
     
            // 在根寻找资源,指定相对路径,其他均返回null。 
            URL filePathUrl4 = TestMain.class.getClassLoader().getResource("beans.xml"); 
     
            // 在不同包内寻找资源,指定相对路径,其他均返回null。 
            URL filePathUrl5 = TestMain.class.getClassLoader().getResource("test/spring/beanpost/file/beans_diffLocation.xml"); 
     
            // ############################################################################################################ 
            // 3:使用ClassLoader类的getSystemResource()方法 
            // 在指定包内寻找资源,指定相对路径,其他均返回null。 
            URL filePathUrl6 = ClassLoader.getSystemResource("test/spring/beanpost/beans_sameLocation.xml"); 
            // 同上 
            URL filePathUrl7 = ClassLoader.getSystemClassLoader().getResource("test/spring/beanpost/beans_sameLocation.xml"); 
     
            // 在根寻找,指定相对路径,其他均返回null。 
            URL filePathUrl8 = ClassLoader.getSystemResource("beans.xml"); 
            // 同上 
            URL filePathUrl9 = ClassLoader.getSystemClassLoader().getResource("beans.xml"); 
     
            // ############################################################################################################ 
            // 4:使用Thread加载资源(推荐此方法) 
            // 在指定包内寻找资源,(相对路径),其他均返回null。 
            filePathUrl6 = Thread.currentThread().getContextClassLoader().getResource("test/spring/beanpost/beans_sameLocation.xml"); 
     
            // 在根寻找,(相对路径),其他均返回null。 
            filePathUrl7 = Thread.currentThread().getContextClassLoader().getResource("beans.xml"); 
     
            // 在不同包内寻找资源,(相对路径),其他均返回null。 
            filePathUrl8 = Thread.currentThread().getContextClassLoader().getResource("test/spring/beanpost/file/beans_diffLocation.xml"); 
     
            // ############################################################################################################ 
     
            System.out.println(filePathUrl1.getFile()); 
            System.out.println(filePathUrl2.getFile()); 
            System.out.println(filePathUrl3.getFile()); 
            System.out.println(filePathUrl4.getFile()); 
            System.out.println(filePathUrl5.getFile()); 
            System.out.println(filePathUrl6.getFile()); 
            System.out.println(filePathUrl7.getFile()); 
            System.out.println(filePathUrl8.getFile()); 
            System.out.println(filePathUrl9.getFile()); 
            System.out.println("----------------------------------------------------------------------------------------"); 
            System.getProperties().list(System.out); 
            System.out.println("----------------------------------------------------------------------------------------"); 
     
            ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); 
            Animal animal = (Animal) ac.getBean("animal"); 
            System.out.println(animal.speak()); 
            animal.setAge(88); 
     
            Animal animal0 = (Animal) ac.getBean("animal"); 
            System.out.println(animal0.speak()); 
     
            ApplicationContext ac1 = new ClassPathXmlApplicationContext("beans.xml"); 
            Animal animal1 = (Animal) ac1.getBean("animal"); 
            System.out.println(animal1.speak()); 
        } 
    } 
  • 相关阅读:
    POJ 3693 Maximum repetition substring (寻找重复次数最多的连续子串)
    URAL-1297 Palindrome (最长回文子串)
    SPOJ
    POJ Musical Theme (求最长不重叠子串)
    Leangoo看板Jenkins配置指南
    Leangoo(领歌)企业版发布,助力企业规模化敏捷
    推荐Scum敏捷开发的几款工具
    如何使用Leangoo进行简单的BUG管理
    轻量级的项目管理工具-Leangoo
    Leangoo敏捷项目协作工具到底好在哪里?
  • 原文地址:https://www.cnblogs.com/liqipeng/p/6076180.html
Copyright © 2020-2023  润新知