1

I have an Spring Rest application, in which there's a requirement to get some access token in a service(Annotated with @Service) class. The token generation is in a separate class(Annotated with @Component) and the requirement given is to get a unique token for each new request. And I'll be using the generated token two times within the same request in the service class. Now the questions are as follows.

I can inject the tokenGenerator class using @Autowired. And get the token, store it in the private instance variable inside the class and use it wherever I wanted inside the service class. Is it the correct approach to store a value that should live as long as the request is alive. When I tested this approach I find the same access token is printed across the methods in service class for a single request. what would go wrong here?

Another approach I tried was using WebApplicationContext.SCOPE_REQUEST on the TokenGenerator class and use the Provider in my service class to get the class instance and then access token from it. This also works the same way as the previous approach. If so why do I even need to use the approach for request scoped values?

Approach 1: Here I have used the dependency injection to get the access token.

@Component
public class TokenGenerator
{
 public string getToken();//okhttp Rest API Call
}

@Service
public class ServiceClass{
    @Autowired 
    TokenGenetor token;

    private String tokenValue;//**setTokenValue as setter**

    public void method1()
    {
     setTokenValue(token.getToken());
    }
    public method2(){print(tokenValue)}// prints 1234abcd
    public method3(){print(tokenValue)}// prints 1234abcd
}

Approach 2: Here I have used the RequestScope to get the access token. and Used the Provider to get the instance in the service class.

@Component
@scope(WebApplicationContext=scope_request)
public class TokenGenerator
{
    public string getToken();//okhttp Rest API Call
}

@Service
public class ServiceClass{
    @Autowired 
    private Provider<TokenGenetor> token;

    private String tokenValue;//setTokenValue as setter

    public void method1()
    {
     // Not Setting anything
    }
    public method2(){print(token.get().getToken();)}// prints 1234abcd
    public method3(){print(token.get().getToken();)}// prints 1234abcd
}
Rajesh2389
  • 349
  • 2
  • 15
  • Does `TokenGenerator` always generate a new token whenever `getToken()` is called ? If yes , then approach 2 seems does not make sense as it generate the same token in method2() and method3() ?? – Ken Chan Feb 14 '20 at 19:17
  • Method 2 is thread safe and Method 1 is not since @Service makes ServiceClass a singleton that is shared among threads. – LucasP Feb 14 '20 at 21:48
  • @Ken, whenever you call the getToken method directly without Request scope, you'll get a new token. Otherwise not. – Rajesh2389 Feb 15 '20 at 07:57
  • @Lucas, I understand that the Service Class is Singleton. So are you saying for two different requests, the token will be same instead of different ones. Can you elaborate a bit. Also it'll be nice if you could provide any documents available online for this matter. Thanks. – Rajesh2389 Feb 15 '20 at 08:02
  • It really depends on the implementation on `TokenGenerator` , so would you please also show the code of `TokenGenerator` , I am interested in how the token is generated. – Ken Chan Feb 15 '20 at 09:34
  • It's a REST API Call made to third party application to get the town. I do not generate the token myself, but I get it through the Rest API. In the same public method mentioned above. – Rajesh2389 Feb 16 '20 at 06:23

0 Answers0