开发者

Is it ok to manipulate static attributes on runtime?

开发者 https://www.devze.com 2023-01-30 22:38 出处:网络
I am providing Settings of my JAVA project in a Prefs.java class with static attributes and static methods. But the token for OAuth2 needs to be assigned on runtime. Is this a good way to go... ?

I am providing Settings of my JAVA project in a Prefs.java class with static attributes and static methods. But the token for OAuth2 needs to be assigned on runtime. Is this a good way to go... ?

public class Prefs {


  //known before runtime
  public static final String SERVER_BASE_URL ="http://api.mycompany.com/";

  //needs to be set on startup through the setter method
  private static String token;
开发者_运维百科

  public static String getToken() {
    return token;
  }

  public static void setToken( String token ) {
    Prefs.token = token;
  }

  public static String getXyEndpointUrl() {
    return SERVER_BASE_URL + "/xy";

  }
}


I would advice against such design. This type of static variables no better than global variables. This page gives a few reasons why you should avoid them. Here are a few of them.

  • Non-locality
  • No Access Control or Constraint Checking
  • Implicit coupling
  • Concurrency issues
  • Testing and Confinement

But the token for OAuth2 needs to be assigned on runtime. Is this a good way to go... ?

Here it really seems to me like you would want to pass such token to the constructor of the Prefs object.


Static variables are object-oriented substitutes for global variables in C. Try to avoid them whenever possible.

Many times you only need one object, in your case it's the Prefs object.

public class Prefs {

  //known before runtime
  public final String SERVER_BASE_URL ="http://api.mycompany.com/";

  //needs to be set on startup through the setter method
  private String token;


  public String getToken() {
    return token;
  }

  public void setToken( String token ) {
    Prefs.token = token;
  }

  public String getXyEndpointUrl() {
    return SERVER_BASE_URL + "/xy";
  }

}

public class Program {

  protected Prefs prefs;

  protected Other prefsAware;

  public Program() {
    prefs = new Prefs();
    prefsAware = new Other(prefs);
  }

  // or even (if you don't like constructor mediated passing of prefs)
  public Prefs getPrefs() {
    return prefs;
  }

}


typically global configs are read from properties files at runtime. You can have varying config files for the development, QA, production environments.

The only thing you need to at least be aware of is that in a web application, if you set static variables from a web request, you could munge the data if you do not synchronize the setter. If you are only reading then you should be fine.

An alternative to what you are doing is why not inject the OAuth key into a service that handles the authentication concerns of your system? You could do what you need to do with a static resource, but in this case, but you should be aware that you don't really need a static variable to hold the key.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号