spring 自定義標籤實現
阿新 • • 發佈:2018-12-16
Spring 自定義標籤實現
一、自定義Spring標籤簡介
Spring官方文件 42.1中,介紹瞭如何自定義Spring標籤,步驟如下: 1、編寫一個XML Schema描述您的自定義元素 2、編寫自定義名稱空間處理程式實現,實現NamespackHandler介面 3、編寫一個或多個自定義的Bean定義解析器,實現BeanDefinitionParser介面 4、註冊到Spring容器
二、自定義標籤實現
1、編寫yf.xsd,名稱根據自己的喜好來寫即可 complexContent 該複雜型別只包含元素或不包含任何元素內容(空) attribute 該複雜型別包含指定的屬性。 annotation和documentation,結合起來,對定義的內容,做一定的文字說明
<xsd:annotation>
<xsd:documentation>
<![CDATA[ Namespace support for the yf services provided by yf framework. ]]></xsd:documentation>
</xsd:annotation>
1)定義一個type
<!-- xsd:complexType 定義一個type,及其屬性attribute --> <xsd:complexType name="applicationType"> <!-- 定義屬性id, id值唯一 --> <xsd:attribute name="id" type="xsd:ID"> <xsd:annotation> <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> </xsd:complexType>
2)定義標籤,並指定其引用哪個type,擷取部分示例如下
<!-- 定義標籤application,並指明其對應的type,示例 <yf:application></yf:application> --> <xsd:element name="application" type="applicationType"> <xsd:annotation> <xsd:documentation><![CDATA[ The application config ]]></xsd:documentation> </xsd:annotation> </xsd:element>
3)編寫標籤對應的實體類。(一個標籤對應一個實體類) 兩個標籤:applaction、registry,實體類對應Application、Registry
2、編寫標籤解析類(因為名稱空間中需要用到此類)
ApplicationBeanDefinitionParser.java RegistryBeanDefinitionParser.java
3、編寫名稱空間處理類
public class YfNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
/**
* registerBeanDefinitionDecorator(String elementName, BeanDefinitionDecorator dec)
* 通過指定的BeandefiniParser解析xsd中定義的element元素
*/
registerBeanDefinitionParser("application", new ApplicationBeanDefinitionParser(Application.class, true));
registerBeanDefinitionParser("registry", new RegistryBeanDefinitionParser());
}
}
4、編寫spring.handlers和spring.schemas串聯起所有部件,二者都存放在META-INF資料夾下
spring.handlers指定NamespaceHandler,內容如下:
http\://yf.oschina.net/schema/yf= com.yf.springcustomtag.springsupport.YfNamespaceHandler
spring.schemas解析XML檔案時將xsd檔案重定向到本地檔案,避免從網上下載xsd檔案(通過實現org.xml.sax.EntityResolver介面來實現該功能),內容如下:
http\://yf.oschina.net/schema/yf.xsd=META-INF/yf.xsd
5、測試
1)編寫spring配置applicationContext.xml,注意Beans中引入自定義標籤的xmlns資訊
xmlns:yf="http://yf.oschina.net/schema/yf"
http://yf.oschina.net/schema/yf http://yf.oschina.net/schema/yf.xsd
完整內容如下:
<?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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:yf="http://yf.oschina.net/schema/yf"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://yf.oschina.net/schema/yf http://yf.oschina.net/schema/yf.xsd">
<!-- 上述Beans中須引用自定義標籤的xsd資訊 -->
<yf:application id="applicationTest" name="yfNameTest" version="1.0" />
<yf:registry id="yfRegistry" address="127.0.0.1" port="8080" />
</beans>
2)編寫測試類
public class YfTagTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Application application1 = (Application) context.getBean("applicationTest");
System.out.println(application1.toString());
Registry registry1 = (Registry) context.getBean("yfRegistry");
System.out.println(registry1.toString());
}
}
3)執行測試類,結果如下,表明自定義標籤測試成功。
23:34:29.734 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
23:34:29.743 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
23:34:29.752 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'applicationTest'
Application{id='applicationTest', name='yfNameTest', version='1.0'}
23:34:29.753 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'yfRegistry'
Registry{id='yfRegistry', address='127.0.0.1', port=8080}
Process finished with exit code 0