pangxia75 发表于 2015-11-13 14:44:20

apache ftpServer源码解读与收获(三)

三.代码解读

上节说道服务器的配置,那么代码解读部分,我们就从服务器怎么加载这些配置开始。
一.加载配置
系统在后期版本中加入了对Spring配置的支持,我们是以1.0.6版本包作为解读对象的。我们做系统,基本上都离不开可配置化支持,简单的做法可以直接基于Spring的标准Bean来配置,但配置较为复杂或者需要更多丰富控制的时候,会显得非常笨拙。一般的做法会用原生态的方式去解析定义好的xml文件,然后转化为配置对象。这种方式需要特殊解析比较繁琐,如果我们用的是spring框架,多出来的配置文件让产品部署又显得很麻烦。Spring提供了可扩展Schema的支持,这是一个不错的折中方案,完成一个自定义配置一般需要以下步骤:

· 设计配置属性和JavaBean

· 编写XSD文件,描述你的配置xml的格式。

· 编写NamespaceHandler和BeanDefinitionParser完成解析工作

· 编写spring.handlers和spring.schemas串联起所有部件。(handlers文件用来注册所有的配置文件解析,schemas文件用来指向具体的XSD文件位置)

· 在Bean文件中应用
我们首先来看一下spring应用中application.xml文件的配置。


写道
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;

xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:jee=&quot;http://www.springframework.org/schema/jee&quot;

xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;

xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;

xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd&quot;

>

<description>Spring公共配置 </description>

<!-- 定义受环境影响易变的变量 -->

<bean

class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;>

   <property

      name=&quot;systemPropertiesModeName&quot;

       value=&quot;SYSTEM_PROPERTIES_MODE_OVERRIDE&quot; />

   <property name=&quot;ignoreResourceNotFound&quot; value=&quot;true&quot; />

   <property name=&quot;locations&quot;>

      <list>

         <value>classpath*:*.properties</value>

       </list>

   </property>

</bean>



<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->

<context:component-scan base-package=&quot;com.vingsoft.dataExchange&quot; />

....省略......

<!-- 使用annotation定义事务 -->



<tx:annotation-driven transaction-manager=&quot;transactionManager&quot;

proxy-target-class=&quot;true&quot; />

</beans>




大家有没有注意到红色部分的配置,spring就是通过扩展schema来兼容个性化的配置的。我们以其中tx标签为例。xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot; 配置文件头定义了一个绑定前缀为tx的命名空间。 xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。tx的模式文档为http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 。
我们再来看看spring.handlers和spring.schemas文件(在jar包的META-INFO下)
spring.handlers 文件:

写道
http\://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler

Spring.schemas 文件:

写道
http\://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd

这里大家有了大致的了解。下面我们针对ftpServer的配置文件。就很简单了。


写道
<server xmlns=&quot;http://mina.apache.org/ftpserver/spring/v1&quot;

            xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;

             xsi:schemaLocation=&quot;

http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd&quot;

id=&quot;myServer&quot;>

         <listeners>

               <nio-listener name=&quot;default&quot; port=&quot;21&quot;>

                      <ssl>

                            <keystore file=&quot;ftpserver.jks&quot; password=&quot;password&quot; />

                     </ssl>

                </nio-listener>

         </listeners>

          <file-user-manager file=&quot;users.properties&quot; />

</server>

配置的模型文件位置是http://mina.apache.org/ftpserver/ftpserver-1.0.xsd。我们根据spring.schemas文件。就能发现XSD文件是对应于org/apache/ftpserver/config/spring/ftpserver-1.0.xsd文件,这里定义了该配置的格式。具体可以打开该文件查看。再去查看spring.handles文件,可以看到配置的解析方法:org.apache.ftpserver.config.spring.FtpServerNamespaceHandler


registerBeanDefinitionParser(&quot;server&quot;,newServerBeanDefinitionParser());
registerBeanDefinitionParser(&quot;nio-listener&quot;,
new ListenerBeanDefinitionParser());
registerBeanDefinitionParser(&quot;file-user-manager&quot;,
new UserManagerBeanDefinitionParser());
registerBeanDefinitionParser(&quot;db-user-manager&quot;,
new UserManagerBeanDefinitionParser());
registerBeanDefinitionParser(&quot;native-filesystem&quot;,
new FileSystemBeanDefinitionParser());
registerBeanDefinitionParser(&quot;commands&quot;,
new CommandFactoryBeanDefinitionParser());


每一个具体的解析都继承了AbstractSingleBeanDefinitionParser类。复写了doParse方法。
截取了FtpServerFactory 解析的部分代码如下:


BeanDefinitionBuilder factoryBuilder
= BeanDefinitionBuilder.genericBeanDefinition(FtpServerFactory.class);
BeanDefinition factoryDefinition
= factoryBuilder.getBeanDefinition();
String factoryName = parserContext.getReaderContext()
.generateBeanName(factoryDefinition);
BeanDefinitionHolder factoryHolder
= new BeanDefinitionHolder(factoryDefinition, factoryName);
registerBeanDefinition(factoryHolder, parserContext.getRegistry());
// set the factory on the listener bean
builder.getRawBeanDefinition().setFactoryBeanName(factoryName);
builder.getRawBeanDefinition().setFactoryMethodName(&quot;createServer&quot;);

非常重要的一段代码!!!!!!!!!向spring上下文注入bean信息。详情可以参考spring资料。后续有时间,再写篇文章探讨下spring的beanfactory管理。

版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: apache ftpServer源码解读与收获(三)