简体   繁体   English

java 库中的 Static 字段

[英]Static field in the java library

I'm using java SDK library of one payment system.我正在使用一个支付系统的 java SDK 库。

In that library you have to set up API key which they provided to you before in the static field:在该库中,您必须设置他们之前在 static 字段中提供给您的 API 密钥:

public static volatile String apiKey;

Each HTTP request which was made by this library involve this apiKey in headers.此库发出的每个 HTTP 请求都在标头中包含此 apiKey。

My task - make aggregator of users of that payment system.我的任务 - 使该支付系统的用户聚合。 Each user has his own API Key.每个用户都有自己的 API 密钥。

When the number of users was 2, I've just made bad thing:当用户数为 2 时,我刚刚做了一件坏事:

public synchronized void pay() {
     Library.apiKey = securityService.currentUser().key();
     Library.pay(...)
     ...
}

It works but everyone understands that it is a bad bad bad practice.它有效,但每个人都明白这是一种不好的坏习惯。 Now I have to support more than 100 users.现在我必须支持 100 多个用户。 My decision will be in 100 times slower not to mention another problems.我的决定将慢 100 倍,更不用说其他问题了。

I want to continue to use this library.我想继续使用这个库。 But in multithread way.但是以多线程方式。 Of course, if I make a request by myself I will do smth like this:当然,如果我自己提出请求,我会这样做:

public void pay() {
     HttpHeader headers;
     headers.put("api_key", securityService.user().key());
     httpClient.post(headers...)
}

I understand that this library just doesn't fit to my task.我知道这个库不适合我的任务。 But I need that library, it does a lot of work inside themself.但我需要那个库,它在自己内部做了很多工作。

If there is no possibility I have to fork it and change to fit my demands.如果没有可能,我必须分叉并更改以适应我的需求。

I attach the link to source code of the library.我附上了图书馆源代码的链接。 https://github.com/stripe/stripe-java/tree/master/src/main/java/com/stripe https://github.com/stripe/stripe-java/tree/master/src/main/java/com/stripe

All of the request methods accept an optional RequestOptions object.所有请求方法都接受可选的RequestOptions object。 In the RequestOptions you can add a secret API key to each method.RequestOptions ,您可以为每个方法添加一个秘密 API 密钥。

RequestOptions requestOptions = RequestOptions.builder()
    .setApiKey("sk_test_...")
    .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM