from:http://andyzhu.blog.51cto.com/4386758/775836/
ImportJava.net.URL; ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTestmain { Public Static voidMain (string[] args) {// ########################################################################################################### # //1: Use the GetResource () method of Class class//finds a resource in the current package (specifies a relative path, and all else returns NULL.) ) URL FILEPATHURL1 = Testmain.class. GetResource ("Beans_samelocation.xml"); //Search for resources at the root (requires file delimiter "/", all other returns NULL.) ) URL filePathUrl2 = Testmain.class. GetResource ("/beans.xml"); //find resources within different packages (Specify relative path (requires file delimiter "/"), and all others return NULL. ) URL filePathUrl3 = Testmain.class. GetResource ("/test/spring/beanpost/file/beans_difflocation.xml"); // ########################################################################################################### # //2: The GetResource () method of the ClassLoader class using Class class//finds resources within the same package and always returns NULL. //URL filePathUrl3 =//TestMain.class.getClassLoader (). GetResource ("Beans_samelocation.xml"); //finds a resource at the root, specifies a relative path, and all else returns NULL. URL filePathUrl4 = Testmain.class. getClassLoader (). GetResource ("Beans.xml"); //look for resources within different packages, specify relative paths, and all others return NULL. URL filePathUrl5 = Testmain.class. getClassLoader (). GetResource ("Test/spring/beanpost/file/beans_difflocation.xml"); // ########################################################################################################### # //3: Use the Getsystemresource () method of the ClassLoader class//finds a resource within the specified package, specifies a relative path, and returns null for all others. URL filePathUrl6 = Classloader.getsystemresource ("Test/spring/beanpost/beans_samelocation.xml"); //Ibid .URL filePathUrl7 = Classloader.getsystemclassloader (). GetResource ("Test/spring/beanpost/beans_samelocation.xml"); //in the root search, specify a relative path, and all others return NULL. URL FilePathUrl8 = Classloader.getsystemresource ("Beans.xml"); //Ibid .URL filePathUrl9 = Classloader.getsystemclassloader (). GetResource ("Beans.xml"); // ########################################################################################################### # //4: Load the resource with thread (recommended this method)//finds resources within the specified package (relative path), and returns null for all others. FilePathUrl6 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Test/spring/beanpost/beans_ Samelocation.xml "); //in the root search, (relative path), all other returns NULL. FILEPATHURL7 = Thread.CurrentThread (). Getcontextclassloader (). GetResource ("Beans.xml"); //find resources within different packages (relative paths), others return 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=NewClasspathxmlapplicationcontext ("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=NewClasspathxmlapplicationcontext ("Beans.xml"); Animal Animal1= (Animal) ac1.getbean ("Animal"); System.out.println (Animal1.speak ()); } }
Java loading resource files in several ways