美文网首页
spring源码解读-XmlBeanFactory的解析

spring源码解读-XmlBeanFactory的解析

作者: 小陈阿飞 | 来源:发表于2018-11-23 16:14 被阅读0次

    解读源码首先要明白用法,根据实际的用法针对的去看源码

    先来个栗子:

    XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));
    Students bean = (Students) bf.getBean("student");
    System.out.println(bean.getB());
    

    beans.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:dubbo="http://code.alibabatech.com/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
        <dubbo:custom id="testCustom" name="this is a test custom tag" />
    
        <bean id="s1" class="xmlbeanfactory.S1" />
    
        <bean id="student" class="xmlbeanfactory.Students" name="st3,st4">
            <constructor-arg value="3" type="java.lang.String"/>
            <constructor-arg value="2" type="java.lang.String"></constructor-arg>
            <constructor-arg value="1" type="int"></constructor-arg>
            <constructor-arg ref="s1"/>
        </bean>
    
        <bean id="student1" class="xmlbeanfactory.Students" name="st1,st2">
            <property name="a" value="1"></property>
            <property name="b" value="2"></property>
            <property name="cs">
                    <list><value>1</value><value>2</value></list>
            </property>
            <property name="s1" ref="s1"></property>
        </bean>
    </beans>
    

    spring.handlers文件增加:

    http\://code.alibabatech.com/schema/dubbo=xmlbeanfactory.TestNamespaceHandler
    

    spring.schemas文件增加:

    http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=org/springframework/beans/factory/xml/dubbo.xsd
    

    增加dubbo.xsd文件

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <xsd:schema xmlns="http://code.alibabatech.com/schema/dubbo"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://code.alibabatech.com/schema/dubbo">
    
        <xsd:element name="custom" type="customType">
        </xsd:element>
        <xsd:complexType name="customType">
            <xsd:attribute name="id" type="xsd:ID">
            </xsd:attribute>
            <xsd:attribute name="name" type="xsd:string">
            </xsd:attribute>
        </xsd:complexType>
    
    </xsd:schema>
    

    推荐两篇不错的解析文:
    http://www.cnblogs.com/ToBeAProgrammer/p/5230065.html
    力推:https://www.cnblogs.com/wade-luffy/p/6065888.html bean注册
    力推:https://www.cnblogs.com/wade-luffy/p/6069245.html bean加载

    相关文章

      网友评论

          本文标题:spring源码解读-XmlBeanFactory的解析

          本文链接:https://www.haomeiwen.com/subject/xiijqqtx.html