spring2自定義標籤
頭很大啊.現在需要擴充套件mule的標籤.
只得先從spring的標籤擴充套件開始, 並筆記之:
spring2以上版本支援自定義標籤.
作為一般的應用程式開發人員,擴充套件spring的標籤意義不大。
但如果你是一個平臺開發人員,則很有必要擁有一套自己的標籤, 提供給應用開發人員使用.
由於mule的標籤是基於spring的xml schema擴充套件的, 為了弄出mule的自定義標籤,讓我們從一個簡單的spring標籤製作開始吧。
我需要做的標籤如下:
<co:log id="myLog" isPrintTime="true" company="cb" />
這個標籤功能簡單, 僅僅列印一下log.它和如下spring bean功能一樣:
<bean id="log" class="com.log.LogBean">
<constructor-arg value="true"></constructor-arg>
<property name="company" value="cb"></property>
</bean>
標籤的schema如下:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.springframework.org/schema/myns" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.mycompany.com/schema/myns" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans"/> <xsd:element name="log"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="isPrintTime" type="xsd:boolean" use="required"/> <xsd:attribute name="company" type="xsd:string"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
log標籤背後的bean如下:
public class LogBean
{
private boolean isPrintTiem = false;
private String company = "";
public LogBean(boolean isPrintTime)
{
this.isPrintTiem = isPrintTime;
}
public void print(String log)
{
if(this.isPrintTiem)
System.out.println(this.company + new Date() + ":" + log);
else
System.out.println(this.company + ":" + log);
}
public String getCompany()
{
return company;
}
public void setCompany(String company)
{
this.company = company;
}
public boolean isPrintTiem()
{
return isPrintTiem;
}
}
實現一個標籤需要擴充套件一個class:
NamespaceHandlerSupport
並實現一個interface:
BeanDefinitionParser
public class LogNamespaceHandler extends NamespaceHandlerSupport
{
public void init()
{
registerBeanDefinitionParser("log",
new LogBeanDefinitionParser());
}
}
LogNamespaceHandler在spring的Ioc容器載入配置檔案時,遇到log節點便會呼叫LogBeanDefinitionParser,完成bean的初始化,並註冊到spring context中.
public class LogBeanDefinitionParser implements
BeanDefinitionParser {
public BeanDefinition parse(Element element, ParserContext parserContext) {
// create a RootBeanDefinition that will serve as configuration
// holder for the 'pattern' attribute and the 'lenient' attribute
RootBeanDefinition beanDef = new RootBeanDefinition();
beanDef.setBeanClass(LogBean.class);
// never null since the schema requires it
String pattern = element.getAttribute("isPrintTime");
beanDef.getConstructorArgumentValues().addGenericArgumentValue(pattern);
String company = element.getAttribute("company");
if(StringUtils.hasText(company))
{
beanDef.getPropertyValues().addPropertyValue("company",company);
}
// retrieve the ID attribute that will serve as the bean identifier in
// the context
String id = element.getAttribute("id");
// create a bean definition holder to be able to register the
// bean definition with the bean definition registry
// (obtained through the ParserContext)
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDef, id);
// register the BeanDefinitionHolder (which contains the bean
// definition)
// with the BeanDefinitionRegistry
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext
.getRegistry());
return beanDef;
}
}
最後在META_INF中, 加入兩個配置檔案:
spring.handlers和spring.schemas
spring.handlers內容如下:
http\://www.mycompany.com/schema/myns=com.log.LogNamespaceHandler
spring.schemas內容如下:
http\://www.mycompany.com/schema/myns/myns.xsd=com/log/logns.xsd
最後測試一下:
新建一個applicationContext.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:co="http://www.mycompany.com/schema/myns" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns.xsd "> <co:log id="myLog" isPrintTime="true" company="cb" /> <bean id="log" class="com.log.LogBean"> <constructor-arg value="true"></constructor-arg> <property name="company" value="cb"></property> </bean> </beans>
testcase:
public class LogTagTest extends AbstractDependencyInjectionSpringContextTests
{
protected String[] getConfigLocations()
{
return new String[] {"classpath*:applicationContext*.xml"};
}
public void testLogBean()
{
LogBean logBean = (LogBean)this.applicationContext.getBean("myLog");
assertTrue(StringUtils.hasText(logBean.getCompany()));
assertTrue(logBean.isPrintTiem());
logBean.print("is fun!");
}
}
綠色在期待中出現.
為了讓mule使用這個bean, 只需mule的配置檔案匯入上面的spring的Application.xml
<spring:beans> <spring:import resource="applicationContext.xml" /> </spring:beans>
然後定義一個mule:
<model name="LogModel"> <service name="databaseCommonUMO"> <!-- any number of endpoints can be added to an inbound router --> <inbound> <vm:inbound-endpoint path="logQueue" /> </inbound> <component> <method-entry-point-resolver> <include-entry-point method="print" /> </method-entry-point-resolver> <spring-object bean="iplatformLog"></spring-object> </component> </service> </model> </mule>
寫一個mule的測試:
public class MySpringTagMuleClientTest
{
public static void main(String[] args) throws MuleException
{
// create mule
MuleContext muleContext;
String config = "mule-myspringtag-config.xml";
muleContext = new DefaultMuleContextFactory().createMuleContext(config);
muleContext.start();
// creat mule client
MuleClient client = new MuleClient();
MuleMessage response = client.send("vm://logQueue", "hello, I'm log content.", null);
}
}
控制檯列印出如下訊息:
cyberThu Mar 19 16:59:26 CST 2009:hello, I'm log content.
相關文章
- 自定義標籤【迭代標籤】薦
- 自定義標籤FlowTagLayout
- ThinkPHP自定義標籤PHP
- java自定義標籤Java
- spring 自定義標籤Spring
- 建立自定義標籤庫
- jQuery Mobile 自定義標籤jQuery
- ViewPager之標籤的自定義Viewpager
- 關於自定義標籤庫
- 有關自定義標籤庫
- 深入淺出自定義標籤(三)操作標籤體薦
- 08.Django自定義模板,自定義標籤和自定義過濾器Django過濾器
- JSP 自定義標籤介紹JS
- 【14】vue.js — 自定義標籤Vue.js
- 自定義分頁標籤詳解
- 自定義標籤出現問題
- JSP自定義標籤之三:為標籤新增屬性JS
- jQuery自定義標籤程式碼例項jQuery
- JSP第六篇【自定義標籤之傳統標籤】JS
- Eclipse預設標籤TODO,XXX,FIXME和自定義標籤Eclipse
- JSP自定義標籤就是如此簡單JS
- 擴充spring元件之自定義標籤Spring元件
- 記錄一種自定義標籤的用法
- jsp如何自定義tag的標籤庫?JS
- JSP自定義標籤系列---rtexprvalue屬性JS
- 自定義html標籤和表單屬性HTML
- 自定義Watir可識別標籤[Z重要!!!]
- Django自定義模板標籤與過濾器Django過濾器
- 聊聊自定義SPI如何使用自定義標籤注入到spring容器中Spring
- Android自定義控制元件之自定義ViewGroup實現標籤雲Android控制元件View
- 實戰練習之Jsp自定義標籤JS
- jsp自定義標籤系列之---用法實力JS
- 求助:如何中斷jsf中多個自定義標籤中的其他標籤JS
- jsp的三種自定義標籤 寫法示例JS
- 急急急急!Struts自定義標籤html:text 問題HTML
- Django5關於 自定義過濾器和標籤Django過濾器
- spring原始碼深度解析— IOC 之 自定義標籤解析Spring原始碼
- 【Django】編寫自定義模板標籤tags 和 過濾器Django過濾器