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;