@Scope多例

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

posted @ 2021-06-14 22:32  宋不争  阅读(131)  评论(0编辑  收藏  举报