Мне нужно сделать асинхронный вызов службы, который внедряется Spring с помощью аннотации autowire.Я делаю что-то вроде этого
@Component
Public class nameListener implements ApplicationListener<ContextRefreshEvent>{
@Autowire
protected ServiceName serviceName;
@Override
@Transactional
public onApplicationEvent(ContextRefreshEvent event){
log(asyncMethod.get().longValue());
}
@Async
public CompletableFuture<Long> asyncMethod(){
CompletableFuture<Long> result = CompletableFuture.supplyAsync(()-> serviceName.methodName());
return result;
}
Настройка serviceBean:
@Configuration
public class serviceClassConfiguration{
@Autowired
protected serviceFactory;
@Bean
public ServiceType serviceName();
//this only creates the type with the attributes it need
return serviceFactory.createServiceName();
}
Ошибка, которую я получаю, говорит следующее:
org.springframework.beans.factory.BeanCreationException;
Scope session is not active for the current thread;
consider defining a scoped proxy for this bean if you intend
to refer to it from a singleton;
nested exception is 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
Если я понимаюпроблема правильно Я думаю, что Spring внедряет прокси, когда выполняется внедрение зависимостей, поэтому bean-компонент serviceName недоступен в области asyncCall, это правильно?
Я пытался создать новый класс только для asyncMethod с использованием прототипаобласть видимости, так что ее можно создавать в любое время, когда вызывается и добавляется как bean-компонент на класс прослушивания, но это также не работает.
Какие обходные пути можно использовать для управления этой ситуацией?
С наилучшими пожеланиями