Spring SimpleThreadScope 未在@Components 上正确自动装配
Spring SimpleThreadScope not properly autowired on @Components
我有一个 Spring 引导应用程序,我需要线程绑定的 bean。我想要需要使用 Spring 的 SimpleThreadScope 的解决方案。我尝试将它自动装配到 @Components 但根据我打印的日志,看起来 Spring 不会为每个生成的线程创建一个新的 bean。我如何正确地自动装配/配置 bean?
这是我的控制器
@RestController
public class Controller {
@Autowired
private DummyClass dummyClass;
@Autowired
private DummyService1 svc;
@PostMapping
public Result myPostMethod (@RequestBody Request request) {
LOGGER.info("myPostMethod " + Thread.currentThread().getName() + " " + Integer.toHexString(this.dummyClass.hashCode()));
this.svc.doSomething();
return new Result();
}
}
我的示例服务
@Service
public class DummyService1 {
@Autowired
private DummyClass dummyClass;
@Autowired
private DummyService2 service;
public void doSomething () {
LOGGER.info("doSomething " + Thread.currentThread().getName() + " " + Integer.toHexString(this.dummyClass.hashCode()));
this.service.doSomething2();
}
}
@Service
public class DummyService2 {
@Autowired
private DummyClass dummyClass;
public void doSomething2 () {
LOGGER.info("doSomething2 " + Thread.currentThread().getName() + " " + Integer.toHexString(this.dummyClass.hashCode()));
}
}
我的配置
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Bean
@Scope(value = "thread", proxyMode = ScopedProxyMode.TARGET_CLASS)
public DummyClass dummyClass () {
DummyClass ctx = new DummyClass();
return ctx;
}
@Bean
public static BeanFactoryPostProcessor beanFactoryPostProcessor () {
return new CustomScopeRegisteringBeanFactoryPostProcessor();
}
}
public class CustomScopeRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("thread", new SimpleThreadScope());
}
}
执行2次后的实际输出
myPostMethod http-nio-8080-exec-4 81823b32
doSomething http-nio-8080-exec-4 81823b32
doSomething2 http-nio-8080-exec-4 81823b32
myPostMethod http-nio-8080-exec-8 81823b32
doSomething http-nio-8080-exec-8 81823b32
doSomething2 http-nio-8080-exec-8 81823b32
2 次执行后的预期输出
myPostMethod http-nio-8080-exec-4 81823b32
doSomething http-nio-8080-exec-4 81823b32
doSomething2 http-nio-8080-exec-4 81823b32
myPostMethod http-nio-8080-exec-8 9a5170d
doSomething http-nio-8080-exec-8 9a5170d
doSomething2 http-nio-8080-exec-8 9a5170d
我注意到如果我在 DummyClass 中执行一个方法(set 或 get),每个线程都会创建一个新实例。我的问题是,新创建的对象没有被注入组件(DummyService1 和 DummyService2)
WebApplicationContext.SCOPE_REQUEST
是始终为每个请求提供不同 bean 的 Web 感知范围的一部分
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
我有一个 Spring 引导应用程序,我需要线程绑定的 bean。我想要需要使用 Spring 的 SimpleThreadScope 的解决方案。我尝试将它自动装配到 @Components 但根据我打印的日志,看起来 Spring 不会为每个生成的线程创建一个新的 bean。我如何正确地自动装配/配置 bean?
这是我的控制器
@RestController
public class Controller {
@Autowired
private DummyClass dummyClass;
@Autowired
private DummyService1 svc;
@PostMapping
public Result myPostMethod (@RequestBody Request request) {
LOGGER.info("myPostMethod " + Thread.currentThread().getName() + " " + Integer.toHexString(this.dummyClass.hashCode()));
this.svc.doSomething();
return new Result();
}
}
我的示例服务
@Service
public class DummyService1 {
@Autowired
private DummyClass dummyClass;
@Autowired
private DummyService2 service;
public void doSomething () {
LOGGER.info("doSomething " + Thread.currentThread().getName() + " " + Integer.toHexString(this.dummyClass.hashCode()));
this.service.doSomething2();
}
}
@Service
public class DummyService2 {
@Autowired
private DummyClass dummyClass;
public void doSomething2 () {
LOGGER.info("doSomething2 " + Thread.currentThread().getName() + " " + Integer.toHexString(this.dummyClass.hashCode()));
}
}
我的配置
@Configuration
public class MyConfig implements WebMvcConfigurer {
@Bean
@Scope(value = "thread", proxyMode = ScopedProxyMode.TARGET_CLASS)
public DummyClass dummyClass () {
DummyClass ctx = new DummyClass();
return ctx;
}
@Bean
public static BeanFactoryPostProcessor beanFactoryPostProcessor () {
return new CustomScopeRegisteringBeanFactoryPostProcessor();
}
}
public class CustomScopeRegisteringBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("thread", new SimpleThreadScope());
}
}
执行2次后的实际输出
myPostMethod http-nio-8080-exec-4 81823b32
doSomething http-nio-8080-exec-4 81823b32
doSomething2 http-nio-8080-exec-4 81823b32
myPostMethod http-nio-8080-exec-8 81823b32
doSomething http-nio-8080-exec-8 81823b32
doSomething2 http-nio-8080-exec-8 81823b32
2 次执行后的预期输出
myPostMethod http-nio-8080-exec-4 81823b32
doSomething http-nio-8080-exec-4 81823b32
doSomething2 http-nio-8080-exec-4 81823b32
myPostMethod http-nio-8080-exec-8 9a5170d
doSomething http-nio-8080-exec-8 9a5170d
doSomething2 http-nio-8080-exec-8 9a5170d
我注意到如果我在 DummyClass 中执行一个方法(set 或 get),每个线程都会创建一个新实例。我的问题是,新创建的对象没有被注入组件(DummyService1 和 DummyService2)
WebApplicationContext.SCOPE_REQUEST
是始终为每个请求提供不同 bean 的 Web 感知范围的一部分
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)