Skip to content

ThreadLocalAccessor implementation for RequestAttributes and LocaleContext #32112

Closed as not planned
@ttddyy

Description

@ttddyy

In Spring MVC, objects like RequestAttributes and LocaleContext are stored in threadlocal(RequestContextHolder, LocaleContextHolder).

To support propagation between threadlocal and reactor operation chain, e.g. using WebClient in MVC, I have custom ThreadLocalAccessor(from micrometer context-propagation) implementations for them.
However, it would be more useful if Spring Framework provided them.

Sample Implementations:

public class LocaleContextThreadLocalAccessor implements ThreadLocalAccessor<LocaleContext> {

	static final String KEY = "my-locale-context";

	@Override
	public Object key() {
		return KEY;
	}

	@Override
	public LocaleContext getValue() {
		return LocaleContextHolder.getLocaleContext();
	}

	@Override
	public void setValue(LocaleContext value) {
		LocaleContextHolder.setLocaleContext(value);
	}

	@Override
	public void setValue() {
		LocaleContextHolder.resetLocaleContext();
	}

}
public class RequestAttributesThreadLocalAccessor implements ThreadLocalAccessor<RequestAttributes> {

	static final String KEY = "my-request-attributes";

	@Override
	public Object key() {
		return KEY;
	}

	@Override
	public RequestAttributes getValue() {
		return RequestContextHolder.getRequestAttributes();
	}

	@Override
	public void setValue(RequestAttributes value) {
		RequestContextHolder.setRequestAttributes(value);
	}

	@Override
	public void setValue() {
		RequestContextHolder.resetRequestAttributes();
	}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by anothertype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions