Search code examples
javaspringspring-bootspring-annotations

Spring SimpleThreadScope not properly autowired on @Components


I have a Spring Boot application and I need to have beans that are thread-bound. I want the solution where I need to use Spring's SimpleThreadScope. I tried autowiring it to @Components but based on the logs I print, it looks like that Spring does not create a new bean for each spawned thread. How do I properly autowire/ configure the bean?

Here is my Controller

@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();
  }
}

My sample services

@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()));
  }
}

My configuration

@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());
  }
}

Actual output after 2 executions

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

Expected output after 2 executions

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

I noticed that if I execute a method (set or get) in the DummyClass, a new instance will be created per thread. My problem is, that newly created object is not being injected in the components (DummyService1 and DummyService2)


Solution

  • WebApplicationContext.SCOPE_REQUEST which is part of Web Aware Scopes that always provide different bean for every request

    @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)