Spring 自定义标签

自定义标签,用来实现定义Bean的作用范围线程独立,每个线程产生自己的实例对象

对象类


public class Model {
}

标签处理类


public class ThreadScopeHandler extends NamespaceHandlerSupport {

    public void init() {
        registerBeanDefinitionParser("thread", new ThreadScopeBeanDefinitionParser());
    }

}

BeanDefinitionParser


public class ThreadScopeBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    protected Class getBeanClass(Element element) {
        return CustomScopeConfigurer.class;
    }

    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        Map<String, Object> scopes = new HashMap();
        if (!scopes.containsKey("thread")) {
            SimpleThreadScope simpleThreadScope = new SimpleThreadScope();
            scopes.put("thread", simpleThreadScope);
            bean.addPropertyValue("scopes", scopes);
        }
    }
}

spring.handlers


http\://www.zheng.com/schema/scope=org.dataguru.ThreadScopeHandler

spring.schemas


http\://www.zheng.com/schema/scope.xsd=scope.xsd

scope.xsd


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.zheng.com/schema/scope" targetNamespace="http://www.zheng.com/schema/scope"
            elementFormDefault="qualified"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="thread">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>


mybeans.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:scope="http://www.zheng.com/schema/scope"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.zheng.com/schema/scope http://www.zheng.com/schema/scope.xsd">
    <bean id="cc" class="org.dataguru.Model" scope="thread"/>
    <!--必须要有一个id,否则会报Configuration problem: Id is required for element 'thread' when used as a top-level tag的错误..-->
    <scope:thread id="threadscope"/>
</beans>

测试类


public class ThreadScopeTest {

    public static void main(String[] args) throws InterruptedException {
        final ApplicationContext ac = new ClassPathXmlApplicationContext("mybeans.xml");  
        System.out.println("主线程对象比较是否相同:"+(ac.getBean("cc") == ac.getBean("cc")));
        new Thread(new Runnable() {

            @Override
            public void run() {
                Model model1 = (Model) ac.getBean("cc");
                System.out.println("线程一:model1.hash=" + System.identityHashCode(model1));
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                Model model2 = (Model) ac.getBean("cc");
                System.out.println("线程二:model2.hash=" + System.identityHashCode(model2));
            }
        }).start();

    }
}


执行结果:



可以看到同在一个主线程的两个对象是相等的,在不同线程产生的对象是不相等的。


项目完整代码:

https://github.com/difffate/SpringProjects
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值