target = null ;
return "success" ;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
}
@Override
public Object resolveContextualObject(String key) {
return null;
}
@Override
public String getConversationId() {
return null;
}
}
注册Scope
@Component
public class CustomScopeRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("custom", new CustomScope()) ;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
}
}
使用Scope
@Component
@Scope("custom")
public class ApplyScopeBean {
}
示例
@RestController
@RequestMapping("/refresh")
@Scope(value = https://www.isolves.com/it/cxkf/jiagou/2023-09-05/ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RefreshController implements ApplicationContextAware{
@Resource
private ApplyScopeBean scopeBean ;
@Resource
private CustomScope customScope ;
@GetMapping("/custom")
public String custom() {
return scopeBean.getCustom() ;
}
@GetMapping("/remove")
public Object remove() {
return customScope.remove("applyScopeBean") ;
}
}
【Spring中Bean的作用域Scope你知道多少?如何自定义作用域?】这里将Controller设置为多例,以便查看效果 。交替执行上面的接口,只要删除了就会创建新的实例 。
3 多例注入如果一个Bean 设置了@Scope(value =https://www.isolves.com/it/cxkf/jiagou/2023-09-05/
ConfigurableBeanFactory.SCOPE_PROTOTYPE) 当这个Bean需要在一个单例Bean中被注入时,需要如下配置才可
@Component
@Scope(value = https://www.isolves.com/it/cxkf/jiagou/2023-09-05/ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ApplyScopeBean {
}
这样才能正确地注入Bean,否则因为本身使用者是单例的,属性只会被初始化一次 。也可以在每次使用前调用BeanFactory#getBean() 。
完毕!!!
推荐阅读
- 如何在 SQL Server 中备份和恢复数据库
- 盘点蔬果中的“护肝大将”
- 欧洲发展中国家有哪些 发展中国家有哪些
- 面试中的2个雷区:避免触碰这些小事
- 五大必备面试技巧:在职场竞争中脱颖而出
- 四季茶花在北方家中适合养殖吗视频 四季茶花在北方家中适合养殖吗
- 男生中文名字简短好听 男生好听罕见的中文名字
- 一年中有7个大月,5个小月对吗 一年中有几个大月几个小月
- 福禄寿喜:文物中的中国吉祥文化
- 梦见肺有什么征兆