Dorado 9 : 06. 定义Dorado中的Spring MVC

dorado工程一般情况下在web.xml中都引入dorado提供的SpringContextLoaderListerner,如:

<listener>
        <listener-class>com.bstek.dorado.web.servlet.SpringContextLoaderListener</listener-class>
</listener>

该Listener继承org.springframework.web.context.ContextLoaderListener,负责初始化和加载doradohome下的相关xml配置文件。

但是在某些项目中可能已经自行扩展了Spring中的ContextLoaderListener或者采用Spring自身提供的org.springframework.context.ContextLoaderListener,这个时候如果想引入dorado,如果直接在web.xml中添加SpringContextLoaderListerner是不行的。

我们可以通过如下步骤解决这个问题:

步骤一:调整web.xml的配置,使用dorado提供的DoradoPreloadListener类, 如下的XML配置:
<listener>
    <listener-class>com.bstek.dorado.web.servlet.DoradoPreloadListener</listener-class>
</listener> 

注意其中的Listener为DoradoPreloadListener,而不是默认的SpringContextLoaderListerner

另外这个Listener在web.xml的配置中要放在Spring的ContextLoaderListener之前

步骤二:在applicationContext.xml中引入<d:import-dorado>

将SpringContextLoaderListerner调整为DoradoPreloadListener后, Spring的xml配置文件加载的入口就是项目中自定义的ContextLoaderListener实现类了,由于它并不知道doradohome资源文件的存在,因此我们需要在applicationContext.xml中添加配置,从而可以自动加载doradohome中的配置文件,配置范例如下:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:d="http://www.bstek.com/dorado/schema"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
                        http://www.bstek.com/dorado/schema 
                        http://www.bstek.com/dorado/schema/spring-dorado-9.0.xsd " >
     <d:import-dorado /> 
</beans>

注意:

  • 第一和第二行的xmlns和xsi中都引入了dorado相关的schema和xsd的声明
  • 第三行添加<d:import-dorado />声明

这样项目中自定义的ContextLoaderListener就会自动加载Dorado相关的系统XML配置文件。