springboot prototype设置多例不起做用的解决办法

大多数人会直接这样写:java

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public TestBean getTestBean() {

    return new TestBean();
}

ConfigurableBeanFactory.SCOPE_PROTOTYPE的值就是prototypeweb

可是发现Autowire的时候,每个请求用的仍是同一个单例对象,这是由于没设置多例的代理模式的问题,改为以下配置就能够了:svg

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public TestBean getTestBean() {
    return new TestBean();
}