Java gets the method of the value in the properties file __properties read

Source: Internet
Author: User

First, the normal way to get the profile value

Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;
Import java.util.Properties;
Import Java.util.PropertyResourceBundle;

Import Java.util.ResourceBundle; public class Test {//1. Using the Load () method of the Java.util.Properties class//Example: public static string Getproperties_1 (string url) throws IOException {INP

		Utstream in = new Bufferedinputstream (new FileInputStream (URL));

		Properties P = new properties ();

		P.load (in);

	Return P.getproperty ("Jdbc.type"); }//2. Using the Getbundle () method of the Java.util.ResourceBundle class//example: public static void getproperties_2 (String URL) {ResourceBundle RB
		= Resourcebundle.getbundle (URL);
		enumeration<string> keys = Rb.getkeys ();
		while (Keys.hasmoreelements ()) {System.out.println (rb.getstring (Keys.nextelement ())); }//3. Constructor//Example using the Java.util.PropertyResourceBundle class: public static void Getproperties_3 (String url) throws Ioexception {InputStream in = new Bufferedinputstream (new FileInputStream (URL));
		ResourceBundle RB = new propertyResourceBundle (in);

		enumeration<string> keys = Rb.getkeys ();
		while (Keys.hasmoreelements ()) {System.out.println (rb.getstring (Keys.nextelement ())); }//4. getResourceAsStream () method with class variable//Example: public static string Getproperties_4 (string url) throws IOException {Inpu

		Tstream in = Test.class.getResourceAsStream (URL);

		Properties P = new properties ();
		
		P.load (in);

	Return P.getproperty ("Jdbc.url"); }//5. The getResourceAsStream () method//example of the Java.lang.ClassLoader obtained using Class.getclassloader (): public static String Getproperties_

		5 (String URL) throws IOException {InputStream in = Test.class.getClassLoader (). getResourceAsStream (URL);

		Properties P = new properties ();

		P.load (in);

	Return P.getproperty ("Jdbc.url"); }//6. Using the Getsystemresourceasstream () static method//Example of the Java.lang.ClassLoader class: public static String Getproperties_6 (STring URL) throws IOException {InputStream in = Classloader.getsystemresourceasstream (URL);

		Properties P = new properties ();

		P.load (in);

	Return P.getproperty ("Jdbc.url"); The Javax.servlet.ServletContext getResourceAsStream () method///Example://public static propertie can be used in the//supplement//Servlet

	s getproperties_8 (String URL) {//InputStream in = Context.getresourceasstream (URL);

	Properties P = new properties ();

	P.load (in); public static void Main (string[] args) throws IOException {//need to place files under the project root directory System.out.println (getproperties_
		1 ("settings.properties"));
		System.out.println ("----------");
		The file needs to be placed below the class file sibling directory getproperties_2 ("Settings");
		System.out.println ("----------");
		Getproperties_3 ("Settings.properties");
		System.out.println ("----------");
		System.out.println (Getproperties_4 ("settings.properties"));
		System.out.println ("----------");
		System.out.println (Getproperties_5 ("settings.properties")); System.out.println ("----------");
	System.out.println (Getproperties_6 ("settings.properties")); }

}
Second, Spring's Way of @Value annotations

The configuration required for the Spring XML file

	<!--Load Application attribute instances can be  referenced by @Value ("#{app_prop[' Jdbc.driver ']}") String jdbcdriver-->
    <util:properties Id= "App_prop" location= "Classpath:settings.properties" local-override= "true"/>
	
Java code:
Package cn.yufu.system.modules.oa.merchant.web;

Import Cn.yufu.system.common.web.BaseController;
Import Cn.yufu.system.modules.oa.merchant.service.ActBusApprovalService;

@Controller
@RequestMapping (value = "${adminpath}/merchant/actbusapproval") Public
class Actbusapprovalcontroller extends Basecontroller {

	@Autowired
	private Actbusapprovalservice Actbusapprovalservice;
	App_prop  is the same as the ID of the configuration file
	@Value ("#{app_prop[' Fileuploadpath ']}")
	private String Uploadfilepath;
	



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.