Issue
I have config class SpecialEventsConfig
.
I am trying to initialise a request Scoped bean of type String.
@Bean("requestTime")
public String getRequestTime() {
return String.valueOf(System.nanoTime());
}
This works, but it will initialise a singleton. I want to use this to initialise a String for the request scope.
@Bean("requestTime")
@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS)// Tried NO and INTERFACES as well
public String getRequestTime() {
return String.valueOf(System.nanoTime());
}
This could solve my problem, but unfortunately it doesn't work.
Is there a way to achieve such behaviour?
Solution
I think you could try this workaround.
@Bean("requestTime")
@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public Supplier<String> getRequestTime() {
long time = System.nanoTime();
return () -> String.valueOf(time);
}
Answered By - Semyon Kirekov