| 在spring 的以前版本中,只是支持singleton,prototype两种类型, 在2.0中作了很大的改进,增加了RequestScope,和SessionScope两种范围。当然也支持自定义Scope 下面简单介绍一下,spring2.0是如何支持自定义Scope的。 Scope接口,需要实现的接口,主要的方法:
下面简单定义一个Scope对象: Scope scope = new Scope() { private int index; private List objects = new LinkedList(); { objects.add(new TestBean()); objects.add(new TestBean()); } public String getConversationId() { return null; } public Object get(String name, ObjectFactory objectFactory) { if (index >= objects.size()) { index = 0; } return objects.get(index++); } public Object remove(String name) { throw new UnsupportedOperationException(); } public void registerDestructionCallback(String name, Runnable callback) { } }; 如何使用让此scope生效,有两种方法: 第一编程实现: ConfigurableBeanFactory 定义了关于Scope的一些方法: void registerScope(String scopeName, Scope scope); String[] getRegisteredScopeNames(); Scope getRegisteredScope(String scopeName); 可以使用registerScope方法来注册相应的scope applicationContext.getBeanFactory().registerScope("myScope", scope); 另外一种实现 xml 配置(建议使用) 通过CustomScopeConfigurer 来注册相应的Scope,由于CustomScopeConfigurer 实现了BeanFactoryPostProcessor,对于ApplcationContext,自动会实现相应的配置 <bean id="myScope" class="MyScope"/> <bean id="customerScope" class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="myScope"> <bean class="myScope"/> </entry> </map> </property> </bean> <bean id="usesScope" class="org.springframework.beans.TestBean" scope="myScope"/> 当然也可以编程实现 Map scopes = new HashMap(); scopes.put(this, new NoOpScope()); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); |
spring2.0 自定义Scope
最新推荐文章于 2024-12-13 15:08:28 发布
本文介绍Spring 2.0中新引入的RequestScope和SessionScope,并详细讲解如何通过编程方式及XML配置实现自定义Scope。了解如何注册Scope并应用于Bean的生命周期管理。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Stable-Diffusion-3.5
图片生成
Stable-Diffusion
Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率
2949

被折叠的 条评论
为什么被折叠?



