gogoWebsite

Get the value value in the properties file

Updated to 19 days ago
package ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import ;
 import .;
 import ;
 import ;
 import ;
 import ;
 import ;

 public class PropertyUtil extends PropertyPlaceholderConfigurer{

 private final static Logger logger = ();
 private static Map<String, String> mapProperties;

        //This method is loaded only once, and then the values ​​in the properties file will be stored in mapProperties as key-value.
       private static void processProperties(Properties props) throws BeansException {
                 if (mapProperties == null) {
                           mapProperties = new HashMap<String, String>();
                  }

                  Iterator<Entry<Object, Object>> iterator = ().iterator();
                  while(()) {
                           <Object, Object> entry = (<Object, Object>) ();
                          String strKey = (String) ();
                          String strValue = (String)();
                           try {
                                strValue=new String(("ISO-8859-1"), "GBK");
                           } catch (UnsupportedEncodingException e) {
                          // TODO Auto-generated catch block
                                  ();
                          }
                              (strKey, strValue);
                }
     }
             public static String getContextProperty(String name) {
                         if (mapProperties == null || () == 0) {
                                   // Where () is the folder name of the upper folder where the properties configuration file is located, that is, "config", () gets the project root path,
                                   //strConfPath is the folder path
                                   String strConfPath = () + ;
                                   // ("---------->path=" + () );
                                  // Where () is the properties file suffix, that is, "properties", get the file name
                                  List<String> lsFilePath = (strConfPath, , false);

                                  if (lsFilePath == null || () == 0) {
                                                return "";
                                  }

                                 for (String strPath : lsFilePath) {
                                               ClassPathResource resource = new ClassPathResource( + "/" + strPath);
                                               Properties props = null;
                                               try {
                                                        props = (resource);//Read information in properties file
                                               } catch (IOException e) {
                                                       // TODO Auto-generated catch block
                                                       //();
                                                      ("getContextProperty exception:" + ());
                                               }
                                                processesProperties(props);//Put it into mapProperties.
                                    }
                       }
                    return (String) (name); then take out the value value in properties according to the passed key
      }
 //Click the() method directly when using it.  The key in the properties file needs to be passed into the method.
 //You need to replace, with the real value, and then you can use it directly.
       public static void main(String[] args) {
                   ("----------->" + getContextProperty());
       }
 }