spring源码解读-XmlBeanFactory的解析

2018-11-23  本文已影响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加载

上一篇 下一篇

猜你喜欢

热点阅读