大多数人会直接这样写:

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

return new TestBean();
}

ConfigurableBeanFactory.SCOPE_PROTOTYPE的值就是prototype

但是发现Autowire的时候,每一个请求用的还是同一个单例对象,这是因为没设置多例的代理模式的问题,改成如下配置就可以了:

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