1. xml文件配置示例
pigeon常用的配置方式是基于Spring Schema方式进行配置,pigeon自定义了一个xml命名空间解析器,用来解析以pigeon:xxx标志的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:pigeon="http://code.dianping.com/schema/pigeon"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.dianping.com/schema/pigeon http://code.dianping.com/schema/pigeon/pigeon-service-2.0.xsd"
default-autowire="byName">
<!--定义独立的线程池 -->
<pigeon:pool id="poolEcho" corePoolSize="5" maxPoolSize="20"
workQueueSize="50"/>
<!-- 以服务方视角,定义一个pigeon服务,供客户端调用-->
<pigeon:service
url="com.dianping.pigeon.demo.EchoService"
interface="com.dianping.pigeon.demo.EchoService"
useSharedPool="false"
ref="defaultEchoServiceImpl">
<pigeon:method name="echo" pool="poolEcho"/>
</pigeon:service>
<!-- 定义一个pigeon服务器 -->
<pigeon:server id="testServer" autoSelectPort="true" corePoolSize="150" httpPort="4080" maxPoolSize="300"
port="4040" suffix="test" workQueueSize="300"/>
<!-- 扫描pigeon相关的注解属性,默认只扫描com.dianping包,如果非此包下的服务需要自定义package属性,多个package以逗号,分隔-->
<pigeon:annotation id="testAnnotation" package="com.dianping"/>
<!-- 以调用方视角,定义一个pigeon服务引用,通过RPC调用远程服务方服务-->
<pigeon:reference id="echoServiceWithCallback"
interface="com.dianping.pigeon.demo.EchoService"
url="com.dianping.pigeon.demo.EchoService"
timeout="1000" protocol="default" serialize="hessian" callType="callback"
callback="echoServiceCallback"/>
</beans>
2. spring源码解析自定义标签流程
下面通过分析源码看pigeon是如何解析这些标签的。
基于以下测试代码在Spring容器中运行:
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/schema/provider.xml");
System.in.read();
}
分析Spring源码,在初始化容器解析xml,会经过以下路径:
- 调用ClassPathXmlApplicationContext构造函数
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {
configLocation}, true, null);
}
//紧接着调用
ublic ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
super(parent);
// 保存配置文件路径
setConfigLocations(configLocations);
if