头很大啊.现在需要扩展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.