• 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>

  • 相关阅读:
    用户、角色和权限关系
    IDEA debug启动的时候需要等半个小时甚至更长时间
    快速启动服务
    leetcode — rotate-image
    leetcode — permutations-ii
    leetcode — jump-game-ii
    leetcode — jump-game
    leetcode — trapping-rain-water
    leetcode — first-missing-positive
    leetcode — combination-sum-ii
  • 原文地址:https://www.cnblogs.com/springmorning/p/10460094.html
Copyright © 2020-2023  润新知