将Spring与Flex整合(基于嵌入式tomcat)

来源:互联网 发布:使命召唤14知乎 编辑:程序博客网 时间:2024/06/11 14:31

 --->点击阅读更多    


前言: 在用嵌入式Tomcat做Java与Flex通信的服务器时遇到了一个很大的困难,从刚开始遇见问题到解决问题足足花了我半年多的时间。虽然现在看待当时那个问题真的很简单,可是我在想,如果我当时放弃了,那么现在我说不定没有这种成就感。学习的道路是曲折的,遇到困难也在所难免,可是最关键的是我们遇见怎样的困难都能坚持下去,因为,问题总有解决的一天。


         Spring是一个非常流行的轻量级企业框架,主要用来管理JavaBean,因此也被大量企业开发人员所青睐。Spring社区现在支持Flex,我们也可以将Spring配置文件中的bean用于Flex的destination属性。


         在学习之前,你需要知道在嵌入式tomcat中怎么将Java与Flex整合在一起,如果你不知道,请看我之前写的一篇文章:图文并茂、手把手教你怎么将Java项目与Flex4整合


         好了,现在开始整合。


         首先准备Spring所必需的jar包,我用的版本是spring-framework-3.0.5.RELEASE版本的,将解压缩后的jar包都复制到WebRoot目录下的jar文件夹里。当然你还需要将Spring与Flex整合在一起的一个插件jar包添加进去,我选择的是spring-flex-core-1.5.2.RELEASE.jar,如下图所示:




      接下来在src目录下新建一个Spring管理JavaBean的配置文件applicationContext.xml,如下所示:


[html] view plaincopyprint?
  1. <?xml version="1.0"encoding="GBK"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       xmlns:aop="http://www.springframework.org/schema/aop"  
  5.       xmlns:tx="http://www.springframework.org/schema/tx"  
  6.       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.           http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  9.    
  10. </beans>  


         接下来在WebRoot/WEB-INF目录下的web.xml文件中指定配置文件的路径并添加Spring配置文件监听器,代码如下所示:


[html] view plaincopyprint?
  1. <context-param>  
  2.        <param-name>contextConfigLocation</param-name>  
  3.        <param-value>  
  4.            classpath*:applicationContext.xml  
  5.        </param-value>  
  6.     </context-param>  
  7.     <!--Spring ApplicationContext 配置文件监听器-->  
  8.     <listener>  
  9.        <listener-class>  
  10.            org.springframework.web.context.ContextLoaderListener  
  11.        </listener-class>  
  12.     </listener>  
  13.     <!-- 
  14.        Spring Introspector 防内存泄露监听器,它负责处理由JavaBeans Introspector的使用而引起的缓冲泄露 
  15.     -->  
  16.     <listener>  
  17.        <listener-class>  
  18.            org.springframework.web.util.IntrospectorCleanupListener  
  19.        </listener-class>  
  20.     </listener>  


         接下来新建两个java文件,一个是FlexSpringFactory.java,代码如下所示:


[java] view plaincopyprint?
  1. package com.ldfsoft.common;  
  2.   
  3. import flex.messaging.FactoryInstance;  
  4. import flex.messaging.FlexFactory;  
  5. import flex.messaging.config.ConfigMap;  
  6.   
  7. public class FlexSpringFactory implements FlexFactory {  
  8.   
  9.     public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {  
  10.         SpringFactoryInstance instance = new SpringFactoryInstance(this, id,  
  11.                 properties);  
  12.         instance.setSource(properties.getPropertyAsString(SOURCE, instance  
  13.                 .getId()));  
  14.         return instance;  
  15.     }  
  16.   
  17.     public Object lookup(FactoryInstance inst) {  
  18.         SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;  
  19.         return factoryInstance.lookup();  
  20.     }  
  21.   
  22.     public void initialize(String arg0, ConfigMap arg1) {  
  23.   
  24.     }  
  25.   
  26. }  




 

         另一个是SpringFactoryInstance.java,代码如下所示:


[java] view plaincopyprint?
  1. package com.ldfsoft.common;  
  2.    
  3. import org.springframework.beans.BeansException;    
  4. import org.springframework.beans.factory.NoSuchBeanDefinitionException;    
  5. import org.springframework.context.ApplicationContext;    
  6. import org.springframework.web.context.support.WebApplicationContextUtils;    
  7.     
  8. import flex.messaging.FactoryInstance;    
  9. import flex.messaging.config.ConfigMap;    
  10. import flex.messaging.services.ServiceException;    
  11.     
  12. public class SpringFactoryInstance  extends FactoryInstance    
  13. {    
  14.    SpringFactoryInstance(FlexSpringFactory factory, String id, ConfigMapproperties)    
  15.    {    
  16.        super (factory, id, properties);    
  17.    }    
  18.    public  Object lookup()     
  19.    {    
  20.               
  21.        ApplicationContext appContext =WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());    
  22.        String beanName = getSource();    
  23.     
  24.        try     
  25.        {    
  26.            return appContext.getBean(beanName);    
  27.        }    
  28.        catch (NoSuchBeanDefinitionException nexc)    
  29.        {    
  30.            ServiceException e = new ServiceException();    
  31.            throw  e;    
  32.        }    
  33.        catch  (BeansException bexc)    
  34.         {    
  35.            ServiceException e = new ServiceException();    
  36.            throw  e;    
  37.        }     
  38.    }    
  39.         
  40. }     


 

           好了,现在开始在Flex配置文件里配置。


           在service-config.xml配置文件里注册FlexSpringFactory,代码如下所示:


[html] view plaincopyprint?
  1. <factories>  
  2.        <factory id="springFactory"class="com.ldfsoft.common.FlexSpringFactory">  
  3.        </factory>  
  4.     </factories>  


            好了,配置完成了,我们测试一下。


            我们将前面建的一个Java文件SayHello.java(点击这里查看)在Spring的配置文件里注册,如下所示:


[html] view plaincopyprint?
  1. <bean id="sayHello"class="com.ldfsoft.service.SayHello"/>  


                接下来在remoting-config.xml配置文件里这样配置:


[html] view plaincopyprint?
  1. <destination id="sayHello">  
  2.        <properties>  
  3.            <factory>springFactory</factory>  
  4.            <source>sayHello</source>  
  5.        </properties>  
  6.     </destination>  


               而我们在Flex的mxml文件中与Java通信时直接引用destination的id属性即可,如果你运行成功,说明你也配置成功了,祝你好运!


--->点击阅读更多