Need some help with Spring autowiring, and scopes.
Here is the basic app structure:
I have an CustomHttpClient, annotated as @Component, and also pulling some config-related properties from application.properties file (via @Value annotation).
CustomHttpClient is used by several services in my application. Whenever I'm using the CustomHttpClient, I autowire an instance of that via:
@Autowired private CustomHttpClient httpClient;
I use interceptor to modify some of the variables inside CustomHttpClient, like so:
public class MyInterceptor extends HandlerInterceptorAdapter { @Autowired CustomHttpClient httpClient; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { httpClient.setSomeProperty(newValue); ...
Now, here is the problem. If I have everything set up as described above, then whenever I change any setting of the CustomHttpClient via interceptor, that new value is persisted for all other clients, as long as VM is running. So when I run httpClient.setSomeProperty() - that setting is now permanently saved. Even if I connect to the application from another client.
Basically what I need to have are two things:
- Still being able to override default settings of the CustomHttpClient via interceptor (request interceptor, configured via ).
- Make sure a new instance of CustomHttpClient is created for every request (after the interceptor does its' magic).
I tried changing the scope of CustomHttpClient to @Scope("prototype"), but that way I can no longer change settings of CustomHttpClient with an interceptor.