Days----How Java and Spring read properties files

Source: Internet
Author: User

One: Java read (only read the properties file under the class file)

private static String Smsurl;//url
Static {   
	        Properties prop = new properties ();  
	        URL url= SendSMS.class.getResource ("/sms.properties");
	        try {   
	            prop.load (Url.openstream ());   
	            Smsurl = Prop.getproperty ("Smsurl"). Trim (); 
	        catch (Exception e) {   
	            e.printstacktrace ();   
	        }   
	    }

Two: Spring read

1. Read the files under the class file

static {    
		Resource Resource = new Classpathresource ("/<span style=" font-family:arial, Helvetica, Sans-serif; " >sms.properties</span><span style= "font-family:arial, Helvetica, Sans-serif;" > ");</span>
		try {
			Properties prop = propertiesloaderutils.loadproperties (Resource);
			Evaluateurl=prop.getproperty ("<span style=" font-family:arial, Helvetica, Sans-serif; " >smsurl</span><span style= "font-family:arial, Helvetica, Sans-serif;" > "). Trim ();</span>
		} catch (IOException e) {
			//TODO auto-generated catch block
			e.printstacktrace ();
		}
    }

2. Read the files under the Web path

<pre name= "code" class= "Java" >static {
Resource Resource = new Classpathresource (".. /config/config.properties ");
		try {
			Properties prop = propertiesloaderutils.loadproperties (Resource);
			Evaluateurl=prop.getproperty ("Jysystem.evaluateurl"). Trim ();
		catch (IOException e) {
			//TODO auto-generated catch block
			e.printstacktrace ();
		}
}

Tomcat Down Path:





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.