• @ComponentScan的scopeResolver属性详解


    @ComponentScan的scopeResolver属性详解

    一、源码说明

    	/**
    	 * The {@link ScopeMetadataResolver} to be used for resolving the scope of detected components.
    	 */
    	Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;
    

    根据注释,我们知道这是一个用来解析bean的scope的属性,

    通俗一点来说,Spring中提供了@Scope注解,用来定义bean的单例还是多例,默认是单例。

    Spring官方文档对于这个属性有一段说明:

    Like the pre-built annotations for those scopes, you may also compose your own scoping annotations using Spring’s meta-annotation approach: e.g. a custom annotation meta-annotated with @Scope("prototype"), possibly also declaring a custom scoped-proxy mode.
    

    就是也许我们有自定义的Scope,这时我们就需要在scopeResolver属性上指明我们自定义的解析类。

    二、举例

    To provide a custom strategy for scope resolution rather than relying on the annotation-based approach, implement the ScopeMetadataResolver interface, and be sure to include a default no-arg constructor. Then, provide the fully-qualified class name when configuring the scanner:

    1. 自定义一个scope注解
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyScope {
    	String value() default ConfigurableBeanFactory.SCOPE_SINGLETON;
    	ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
    }
    

    可以对比下Spring自带的@scope注解

    1. 把自定义的MyScope注解加在我们的组件上
    @Component
    //注意这里也指定的是SCOPE_PROTOTYPE
    @MyScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class CustomScopeAnnotationBean {
    }
    
    
    1. 那思考我们在创建bean的过程中如何解析我们的MyScope注解呢?

    通过scopeResolver属性

    @Configuration
    @ComponentScan(basePackages = "example.scannable_scoped", scopeResolver = MyScopeMetadataResolver.class)
    class ComponentScanWithScopeResolver {
    }
    
    1. 自定义解析类
    class MyScopeMetadataResolver extends AnnotationScopeMetadataResolver {
    
    	MyScopeMetadataResolver() {
    		this.scopeAnnotationType = MyScope.class;
    	}
    }
    
    

    这里只是将父类中的Scope.class换换成了MyScope.class

    1. 测试类
    	@Test
    	public void withScopeResolver() {
    		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithScopeResolver.class);
    		// custom scope annotation makes the bean prototype scoped. subsequent calls
    		// to getBean should return distinct instances.
    		assertThat(ctx.getBean(CustomScopeAnnotationBean.class)).isNotSameAs(ctx.getBean(CustomScopeAnnotationBean.class));
    	}
    

    单测通过,

    看到我们的注解已经生效了

  • 相关阅读:
    Linux编译工具:gcc入门
    Socket编程实践(3) 多连接服务器实现与简单P2P聊天程序例程
    Socket编程实践(2) Socket API 与 简单例程
    Socket编程实践(1) 基本概念
    数据结构图文解析之:二分查找及与其相关的几个问题解析
    数据结构图文解析之:直接插入排序及其优化(二分插入排序)解析及C++实现
    [CG编程] 基本光照模型的实现与拓展以及常见光照模型解析
    郑重声明!本博客内容皆为原创且首发于博客园
    【原创】面试官:谈谈你对mysql联合索引的认识?
    【原创】为什么Mongodb索引用B树,而Mysql用B+树?
  • 原文地址:https://www.cnblogs.com/heliusKing/p/13874172.html
Copyright © 2020-2023  润新知