Change the java properties file without restarting the web service reads the value of recent changes

1. write the configuration file is written in the properties, but sometimes you need to change the value in project development, but find that you need to restart the service to take effect

     /**
     * Initial configuration file
     */
    public void init(){
                try{
            InputStream is =     Config.class.getResourceAsStream("/res/config.properties");
            properties = new Properties();
            properties.load(is);
            
        }catch (Exception e){
            throw new RuntimeException("Failed to get properties!");
        }
    }

This method will be loaded later found config.properties file is loaded into memory, the next time you need to read to obtain direct information from the file in memory instead of reading again! So it is necessary to change the input file acquisition mode stream 

     String path = Config.class.getClassLoader().getResource("/res/config.properties").getPath();  
	InputStream is = new FileInputStream(path);  
	properties.load(is);  

  


 

Guess you like

Origin www.cnblogs.com/jdy1022/p/11076346.html