zoukankan      html  css  js  c++  java
  • Spring框架参考手册(4.2.6版本)翻译——第三部分 核心技术 6.10.7 为自动检测组件提供作用域

    6.10.7 为自动检测组件提供作用域

    总地来说,如同Spring管理的组件,自动检测组件的默认和最常见的作用域就是单例。但是,有时您需要其他作用域Spring 2.5提供了新的@Scope 只需在注中提供作用域的名称:

    @Scope("prototype")
    @Repository
    public class MovieFinderImpl implements MovieFinder {
        // ...
    }

    要为作用域解析提供自定义策略而不是依赖基于注方法可以实现ScopeMetadataResolver接口,并确保包含默认的无参数构造函数。然后,在配置扫描程序时提供完全限定的类名:

    @Configuration
    @ComponentScan(basePackages = "org.example", scopeResolver = MyScopeResolver.class)
    public class AppConfig {
        ...
    }
    <beans>
        <context:component-scan base-package="org.example"
                scope-resolver="org.example.MyScopeResolver" />
    </beans>

    使用某些非单例作用域时,可能需要为作用域对象生成代理。原因作为依赖项的作用域bean”一节描述。为此,component-scan元素上提供了scoped-proxy属性。三个可能的值是:nointerfacestargetClass。 例如,以下配置将生成标准JDK动态代理:

    @Configuration
    @ComponentScan(basePackages = "org.example", scopedProxy = ScopedProxyMode.INTERFACES)
    public class AppConfig {
        ...
    }
    <beans>
        <context:component-scan base-package="org.example"
            scoped-proxy="interfaces" />
    </beans>

  • 相关阅读:
    P4141 消失之物(退背包模板)
    P5829 【模板】失配树
    P4827 [国家集训队] Crash 的文明世界
    P4074 [WC2013]糖果公园
    P3242 [HNOI2015]接水果
    P2371 [国家集训队]墨墨的等式
    P4819 [中山市选]杀人游戏
    P5331 [SNOI2019]通信
    BZOJ1082 [SCOI2005]栅栏
    poj1475 Pushing Boxes[双重BFS(毒瘤搜索题)]
  • 原文地址:https://www.cnblogs.com/springmorning/p/10460094.html
Copyright © 2011-2022 走看看