如何确保 Provider<MyFacade> returns 在新的异步线程中有一个新的 MyFacade?
How can I make sure Provider<MyFacade> returns a new MyFacade in a new async thread?
我正在尝试让我的 myFacadeProvider.get()
return 成为一个特定的话题 MyFacade
。只要我在正常的 WebApplication RequestContext 中,它就可以工作,因为它 return 在整个应用程序中都是相同的 MyFacade
。但是,如果我创建一个新线程并执行 myFacadeProvider.get()
,它将 return 与 WebApplication RequestContext 中引用的相同 MyFacade
而不是该特定线程的新线程。我怎样才能使 myFacadeProvider.get()
return 成为特定的线程 MyFacade
无论我在哪个线程?
一些示例代码:
@Component
@Scope(value = WebapplicationContext.SCOPE_REQUEST)
public class MyFacade {
@Setter
private String work = "the main work";
public String doWork() {
return work;
}
}
@Component
public class QueryES {
@Autowired
private Provider<MyFacade> myFacadeProvider;
public void doThings() {
// This call gets a MyFacade that is thread safe in this scoped context and works as expected
MyFacade myFacade = myFacadeProvider.get();
myFacade.doWork(); // returns `the main work`
myFacade.setWork("the secondary work");
myFacade.doWork(); // returns `the secondary work`
// When I spin up a new thread and do the same call it will give me back the same myFacade
// that was returned previously instead of a new thread specific myFacade
Future<String> future = getFacadeAsyncDoWork(); // returns `the secondary work` instead of `the main work`
}
public ListenableFuture<String> getFacadeAsyncDoWork() {
return new AsyncResult<String>(doQueryWork());
}
public String doQueryWork() {
// Returns same myFacade as above, I want this to return a thread specific provider
MyFacade asyncMyFacade= myFacadeProvider.get();
return asyncMyFacade.doWork(); // returns `the secondary work` instead of `the main work`
}
}
您可以使用 CustomScopeConfigurer
来注册自定义范围。 Spring 包含 SimpleThreadScope
实现但默认不注册。
示例:
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.SimpleThreadScope;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public CustomScopeConfigurer customScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("thread", new SimpleThreadScope());
return configurer;
}
}
@Component
@Scope(value = "thread")
public class MyFacade {
private final Thread currentThread = Thread.currentThread();
public String getThreadName() {
return currentThread.getName();
}
}
我正在尝试让我的 myFacadeProvider.get()
return 成为一个特定的话题 MyFacade
。只要我在正常的 WebApplication RequestContext 中,它就可以工作,因为它 return 在整个应用程序中都是相同的 MyFacade
。但是,如果我创建一个新线程并执行 myFacadeProvider.get()
,它将 return 与 WebApplication RequestContext 中引用的相同 MyFacade
而不是该特定线程的新线程。我怎样才能使 myFacadeProvider.get()
return 成为特定的线程 MyFacade
无论我在哪个线程?
一些示例代码:
@Component
@Scope(value = WebapplicationContext.SCOPE_REQUEST)
public class MyFacade {
@Setter
private String work = "the main work";
public String doWork() {
return work;
}
}
@Component
public class QueryES {
@Autowired
private Provider<MyFacade> myFacadeProvider;
public void doThings() {
// This call gets a MyFacade that is thread safe in this scoped context and works as expected
MyFacade myFacade = myFacadeProvider.get();
myFacade.doWork(); // returns `the main work`
myFacade.setWork("the secondary work");
myFacade.doWork(); // returns `the secondary work`
// When I spin up a new thread and do the same call it will give me back the same myFacade
// that was returned previously instead of a new thread specific myFacade
Future<String> future = getFacadeAsyncDoWork(); // returns `the secondary work` instead of `the main work`
}
public ListenableFuture<String> getFacadeAsyncDoWork() {
return new AsyncResult<String>(doQueryWork());
}
public String doQueryWork() {
// Returns same myFacade as above, I want this to return a thread specific provider
MyFacade asyncMyFacade= myFacadeProvider.get();
return asyncMyFacade.doWork(); // returns `the secondary work` instead of `the main work`
}
}
您可以使用 CustomScopeConfigurer
来注册自定义范围。 Spring 包含 SimpleThreadScope
实现但默认不注册。
示例:
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.SimpleThreadScope;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public CustomScopeConfigurer customScopeConfigurer() {
CustomScopeConfigurer configurer = new CustomScopeConfigurer();
configurer.addScope("thread", new SimpleThreadScope());
return configurer;
}
}
@Component
@Scope(value = "thread")
public class MyFacade {
private final Thread currentThread = Thread.currentThread();
public String getThreadName() {
return currentThread.getName();
}
}