ESB产品MULE学习笔记系列:一、怎样让Mule在webapp中结合Spring使用 -...

来源:百度文库 编辑:神马文学网 时间:2024/04/26 05:41:59
怎样让Mule在webapp中结合Spring使用
从形势来看,如果应用不使用 Spring 就感觉有点落伍——说法有点别扭:好像有点过。诚然, Spring 给我们带来了太多的好处,以至于几乎大部分的产品都以声称能够整合 Spring 为荣, Mule 也不能免俗:)
从官方来看, mule 与 spring 的结合有三种做法:
1 、 Using Spring as a Component Factory
How to configure the Spring Container with Mule so that Managed components and other Mule objects can be loaded from Spring.
2 、 Configuring the Mule Server From a Spring Context
A Mule server is just a bunch of beans! How to load a Mule instance from the Spring Container.
3 、 Configuring a Spring context using Mule Xml
There are lots of reasons why you might want to use Mule and Spring together, but configuring Mule in Spring bean Xml can be a verbose process. Now you can configure it using Mule Xml and mix Spring beans in the configuration.
1.1.1.          Using Spring as a Component Factory
我下面首先尝试的是第一种。
1.1.1.1.      web.xml

contextConfigLocation
/WEB-INF/applicationContext-mule.xml


              org.springframework.web.context.ContextLoaderListener

1.1.1.2.      配置文件(举例)








false











echoComponent






vm://echo






1.1.1.3.      评价
这种方法是比较纯的 spring 配置手段 ( 注意在此使用的 DTD 为 spring-beans.dtd) ,不利于把握 mule 的配置感觉。不推荐使用。
1.1.2.          Configuring a Spring context using Mule Xml
1.1.2.1.      Web.xml

contextConfigLocation
/WEB-INF/applicationContext-mule.xml,
/WEB-INF/ede-config.xml


            org.springframework.web.context.ContextLoaderListener

1.1.2.2.      配置文件(举例, applicationContext-ede-core.xml )




Enterprice DataExpress












1.1.2.3.      配置文件(举例, applicationContext-ede-extend.xml )











1.1.2.4.      评价
这种方法是比较 Mule-friendly 的配置手段 ( 注意在此使用的 DTD 为 mule-spring-configuration.dtd) ,从整体感觉来看,与一般的纯 Mule 配置感觉类似。
推荐使用。
1.1.3.          自定义方式
从产品研发来看,自定义模型配置加载方式有着诸多的好处,这里不讲。
我们的自定义模型配置加载方式的目标是:
1、              可以兼容标准的基于 mule 配置文件配置的模型,同时也要兼容根据其他定义方式(如基于数据库)的可编程式模型加载;
2、              可以更多的干预系统默认的加载方式。
1.1.3.1.      Web.xml

com.nci.ede.config
applicationContext-ede-core.xml,
applicationContext-ede-extend.xml,
applicationContext-sample-echo2.xml



com.nci.ede.system.config.EdeBuilderContextListener

这里有一个重要的 Listener ,用来在系统启动的时候自动加载配置信息,其核心代码如下:
public void contextInitialized(ServletContextEvent event)
{
String config = event.getServletContext().getInitParameter(CONFIG_INIT_PARAMETER);
if (config == null) {
config = getDefaultConfigResource();
}
try {
createManager(config, event.getServletContext());
} catch (ConfigurationException e) {
event.getServletContext().log(e.getMessage(), e);
}
}
protected UMOManager createManager(String configResource, ServletContext context) throws ConfigurationException{
//WebappMuleXmlConfigurationBuilder builder = new WebappMuleXmlConfigurationBuilder(context);
SpringConfigurationBuilder builder = new SpringConfigurationBuilder();
UMOManager manager = builder.configure(configResource);
try {
// 通过 spring 的 bean factory 获取
EdeConfigurationLoader loader = (EdeConfigurationLoader)SpringHelper.getBean(manager,"edeConfigurationLoader");
loader.loadConfig();
} catch (ObjectNotFoundException e1) {
e1.printStackTrace();
} catch (EdeException e) {
e.printStackTrace();
}
return manager;
}
其中所调用的自定义加载器 edeConfigurationLoader 可以在 spring 中注入。