@Scope
注解 用于管理 Bean 为单例还是原型(多例)模式
Spring 默认为单例模式,现在切换为多例模式后不生效。
示例
问题代码 一
1 2 3 4 5
| @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Service public class IscpDatabaseManageServiceImpl implements IIscpDatabaseManageService, IscpDatabaseManageApi { }
|
解决方案
传递 proxyMode
参数为 ScopedProxyMode.TARGET_CLASS
1 2 3 4 5 6
| @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) @Service public class IscpDatabaseManageServiceImpl implements IIscpDatabaseManageService, IscpDatabaseManageApi { }
|
问题代码 二
1 2 3 4
| @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) public interface IIscpDatabaseManageService { }
|
解决方案
接口/服务类不是一个 Bean/Component ,故不生效。需要注解到服务实现类上
1 2
| public interface IIscpDatabaseManageService { }
|
1 2 3 4 5 6
| @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS) @Service public class IscpDatabaseManageServiceImpl implements IIscpDatabaseManageService, IscpDatabaseManageApi { }
|