System.setProperty is safe in java?

前端 未结 4 849
囚心锁ツ
囚心锁ツ 2020-12-20 06:11

In java to pass the values between some classes we can use System.setProperty. But using System.getProperties() we can get all the system propertie

4条回答
  •  余生分开走
    2020-12-20 06:53

    As per the documentation

    In general, be careful not to overwrite system properties.

    The setProperties method changes the set of system properties for the current running application. These changes are not persistent. That is, changing the system properties within an application will not affect future invocations of the Java interpreter for this or any other application. The runtime system re-initializes the system properties each time its starts up. If changes to system properties are to be persistent, then the application must write the values to some file before exiting and read them in again upon startup.

    your concern is correct that some third party libraries might overwrite the properties that your app is using. Its always a good practice to use some naming convention to distinguish keys defined in your property file.

    A very simple simulation of the problem

    public class TestApp {
        public static void main(String args[]) throws InterruptedException {
            TestApp app = new TestApp();
            app.new ThirdPartyLib("thirdParty").start();
            while (true) {
                Thread.currentThread().sleep(500);
                System.setProperty("test", "orignalProperty");
                System.out
                        .format("Thread Name  '%s' setting the property with value '%s' \n ",
                                Thread.currentThread().getName(),
                                System.getProperty("test"));
            }
        }
    
        class ThirdPartyLib extends Thread {
            public ThirdPartyLib(String threadName) {
                super(threadName);
            }
    
            @Override
            public void run() {
                super.run();
                while (true) {
                    Thread.currentThread();
                    try {
                        Thread.sleep(400);
                        System.setProperty("test", "modifiedProperty");
                        System.out
                                .format("Thread Name  '%s' setting the property with value '%s' \n ",
                                        Thread.currentThread().getName(),
                                        System.getProperty("test"));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    will result in output below - which might not be the intended one and I am sure difficult to debug also

     Thread Name  'thirdParty' setting the property with value 'modifiedProperty' 
     Thread Name  'main' setting the property with value 'orignalProperty' 
     Thread Name  'thirdParty' setting the property with value 'modifiedProperty' 
     Thread Name  'main' setting the property with value 'orignalProperty' 
     Thread Name  'thirdParty' setting the property with value 'modifiedProperty' 
     Thread Name  'thirdParty' setting the property with value 'modifiedProperty' 
     Thread Name  'main' setting the property with value 'orignalProperty' 
    

提交回复
热议问题