可以在运行时操作静态属性吗?

发布于 2024-10-08 14:51:01 字数 546 浏览 10 评论 0原文

我在具有静态属性和静态方法的 Prefs.java 类中提供 JAVA 项目的设置。但 OAuth2 的令牌需要在运行时分配。这是一个好方法吗...?

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 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";

  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一腔孤↑勇 2024-10-15 14:51:01

我建议不要这样的设计。这种类型的静态变量并不比全局变量好。 此页面给出了一些您应该避免使用它们的原因。以下是其中的一些。

  • 非局部性
  • 无访问控制或约束检查
  • 隐式耦合
  • 并发问题
  • 测试和限制

但是 OAuth2 的令牌需要在运行时分配。这是一个好方法吗...?

在我看来,您确实希望将此类令牌传递给 Prefs 对象的构造函数。

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.

孤芳又自赏 2024-10-15 14:51:01

静态变量是 C 语言中全局变量的面向对象替代品。尽可能避免使用它们。

很多时候您只需要一个对象,在您的情况下它是 Prefs 对象。

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;
  }

}

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;
  }

}
蓝梦月影 2024-10-15 14:51:01

通常全局配置是在运行时从属性文件中读取的。您可以为开发、QA、生产环境使用不同的配置文件。

您至少需要注意的唯一一件事是,在 Web 应用程序中,如果您通过 Web 请求设置静态变量,并且不同步设置器,则可能会修改数据。如果您只是阅读,那么应该没问题。

您正在做的事情的另一种选择是为什么不将 OAuth 密钥注入到处理系统身份验证问题的服务中?您可以使用静态资源执行您需要执行的操作,但在这种情况下,您应该意识到您实际上并不需要静态变量来保存密钥。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文