Java can';无法从异步方法访问对象
我对侦听器执行的异步方法有问题。 首先,我请求使用某个端点,没关系,我看到当前用户的bean被创建了。然后它进入activemq内部,并在一段时间内将侦听器的结果返回给另一个方法。在那里我想看到我的当前用户对象,但我有NPEJava can';无法从异步方法访问对象,java,spring,asynchronous,Java,Spring,Asynchronous,我对侦听器执行的异步方法有问题。 首先,我请求使用某个端点,没关系,我看到当前用户的bean被创建了。然后它进入activemq内部,并在一段时间内将侦听器的结果返回给另一个方法。在那里我想看到我的当前用户对象,但我有NPE @RequiredArgsConstructor @Component @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) @Slf4j
@RequiredArgsConstructor
@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Slf4j
public class CurrentUserImpl implements CurrentUser {
private final HttpServletRequest request;
private Account currentAccount;
private final AccountClient accountClient;
@PostConstruct
private void init() {
String username = request.getHeader("Username");
if (username != null) {
currentAccount = accountClient.getByUsername(new String(Base64.getDecoder().decode(username)));
} else {
log.info("'username' header haven't been found in the request");
}
}
@Override
public Optional<Account> getAccount() {
return Optional.ofNullable(currentAccount);
}
}
你能给出完整的堆栈跟踪吗?
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.