最代码官方的gravatar头像
最代码官方 2021-01-31 22:04:47

Spring自定义ThreadLocal Scope实现类:ThreadScope

项目描述

在上一份代码Spring Bean的Scopes范围Singleton和Prototype说明的实例中,除了几个spring内置的scope外,还可以通过实现Scope接口实现自定义范围Scope的实现类,这样可以按照自己的需求来对bean的生命周期进行管理

运行环境

jdk8+IntelliJ IDEA+maven

项目技术(必填)

spring @Scope

依赖包文件(可选)

链接:https://pan.baidu.com/s/1kuognwJcLChm7PM65gEcHw
提取码:5cmd

是否原创(转载必填原文地址)

转载https://memorynotfound.com/spring-custom-scope-creating-and-implementing-threadscope/

项目截图(必填)

Spring自定义ThreadLocal Scope实现类:ThreadScope

运行截图(必填)

Spring自定义ThreadLocal Scope实现类:ThreadScope

前提时需要在spring中初始化自定义的Scope实现类

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="thread" class="com.memorynotfound.spring.core.scope.ThreadScope"/>

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="thread" value-ref="thread"/>
            </map>
        </property>
    </bean>

    <bean class="com.memorynotfound.spring.core.scope.Register" scope="thread"/>

</beans>

注意事项(可选)

运行Run即可


打赏

文件名:spring-custom-scope-thread-scope-example.zip,文件大小:5.896K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友