Spring扩展(2)-Spring Scope总结

2018-02-08  本文已影响176人  Terminalist

0.先醒醒脑

WechatIMG2220.jpeg

1.关于scope

2.自定义scope

要实现自己的scope,可以参照RequestScope和SessionScope,首先必须实现Scope接口或者继承AbstractRequestAttributesScope,接口定义中的4个方法并非都是必须的,但get和remove方法必须实现;

1.有了Scope的实现类之后,我们需要把这个Scope注册到容器中,才能供相应的bean定义使用。使用编码注册的方式是通过ConfigurableBeanFactory#registerScope注册自定义scope;

  Scope customScope = new CustomScope(); 
  beanFactory.registerScope("custom",customScope);

正常情况下都是按如下方式注册:

    ClassPathXmlApplicationContext context = ...;
    context.getBeanFactory().registerScope("custom", new CustomScope());

2.使用xml的方式注入:

<bean id="custom" class="CustomScope"/>
<bean id="customerScope" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="custom">
                <bean class="custom"/>
            </entry>
        </map>
    </property>
</bean>
<bean id="usesScope" class="org.springframework.beans.TestBean" scope="custom"/>

原因是这样:
CustomScopeConfigurer 实现了BeanFactoryPostProcessor,BeanClassLoaderAware等接口,SpringIoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它。而CustomScopeConfigurer中实现的postProcessBeanFactory方法;

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (this.scopes != null) {
            Iterator var2 = this.scopes.entrySet().iterator();

            while(var2.hasNext()) {
                Entry<String, Object> entry = (Entry)var2.next();
                String scopeKey = (String)entry.getKey();
                Object value = entry.getValue();
                if (value instanceof Scope) {
                    beanFactory.registerScope(scopeKey, (Scope)value);
                } else {
                    Class scopeClass;
                    if (value instanceof Class) {
                        scopeClass = (Class)value;
                        Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
                        beanFactory.registerScope(scopeKey, (Scope)BeanUtils.instantiateClass(scopeClass));
                    } else {
                        if (!(value instanceof String)) {
                            throw new IllegalArgumentException("Mapped value [" + value + "] for scope key [" + scopeKey + "] is not an instance of required type [" + Scope.class.getName() + "] or a corresponding Class or String value indicating a Scope implementation");
                        }

                        scopeClass = ClassUtils.resolveClassName((String)value, this.beanClassLoader);
                        Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class");
                        beanFactory.registerScope(scopeKey, (Scope)BeanUtils.instantiateClass(scopeClass));
                    }
                }
            }
        }

    }

它同样会去扫描加载的配置文件中的scope,并进行注册;

补充:关于BeanFactoryPostProcessor的内容,Spring IoC容器允许同时可以定义多个BeanFactoryPostProcessor,通过实现order接口来确定各个BeanFactoryPostProcessor执行顺序。注册一个BeanFactoryPostProcessor实例需要定义一个Java类来实现BeanFactoryPostProcessor接口,并重写该接口的postProcessorBeanFactory方法。通过beanFactory可以获取bean的定义信息,并可以修改bean的定义信息。

public interface BeanFactoryPostProcessor {
    void postProcessBeanFactory(ConfigurableListableBeanFactory var1) throws BeansException;
}

在Spring中内置了一些BeanFactoryPostProcessor实现类,可遵循如下关系延伸阅读;


1071792-20170320144908611-422885334.jpg

关于在spring-boot中自定义scope其实也类似,改天再将案例贴出!

上一篇下一篇

猜你喜欢

热点阅读