FLEX和spring、hibernate的集成

FLEX和spring、hibernate的集成

Posted under Web相关 by admin

flex的后台使用spring+hibernate,spring+hibernate的集成方法和j2ee的项目中方法相同。主要是flex和spring的集成

编写SpringFactory.java类:

java 代码

 

  1. package com.fire.spring;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.web.context.support.WebApplicationContextUtils;   
  5. import org.springframework.beans.BeansException;   
  6. import org.springframework.beans.factory.NoSuchBeanDefinitionException;   
  7.   
  8. import flex.messaging.FactoryInstance;   
  9. import flex.messaging.FlexFactory;   
  10. import flex.messaging.config.ConfigMap;   
  11. import flex.messaging.services.ServiceException;   
  12.   
  13. public class SpringFactory implements FlexFactory   
  14. {   
  15.     private static final String SOURCE = “source”;   
  16.   
  17.        public void initialize(String id, ConfigMap configMap) {}   
  18.   
  19.         public FactoryInstance createFactoryInstance(String id, ConfigMap properties)   
  20.     {   
  21.         SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);   
  22.         instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));   
  23.         return instance;   
  24.     } // end method createFactoryInstance()   
  25.   
  26.         public Object lookup(FactoryInstance inst)   
  27.     {   
  28.         SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;   
  29.         return factoryInstance.lookup();   
  30.     }    
  31.   
  32.   
  33.     static class SpringFactoryInstance extends FactoryInstance   
  34.     {   
  35.         SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)   
  36.         {   
  37.             super(factory, id, properties);   
  38.         }   
  39.   
  40.   
  41.         public String toString()   
  42.         {   
  43.             return “SpringFactory instance for id=” + getId() + “ source=” + getSource() + “ scope=” + getScope();   
  44.         }   
  45.   
  46.         public Object lookup()    
  47.         {   
  48.             ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());   
  49.             String beanName = getSource();   
  50.   
  51.             try  
  52.             {   
  53.                 return appContext.getBean(beanName);   
  54.             }   
  55.             catch (NoSuchBeanDefinitionException nexc)   
  56.             {   
  57.                 ServiceException e = new ServiceException();   
  58.                 String msg = “Spring service named ’” + beanName + “‘ does not exist.”;   
  59.                 e.setMessage(msg);   
  60.                 e.setRootCause(nexc);   
  61.                 e.setDetails(msg);   
  62.                 e.setCode(“Server.Processing”);   
  63.                 throw e;   
  64.             }   
  65.             catch (BeansException bexc)   
  66.             {   
  67.                 ServiceException e = new ServiceException();   
  68.                 String msg = “Unable to create Spring service named ’” + beanName + “‘ ”;   
  69.                 e.setMessage(msg);   
  70.                 e.setRootCause(bexc);   
  71.                 e.setDetails(msg);   
  72.                 e.setCode(“Server.Processing”);   
  73.                 throw e;   
  74.             }    
  75.         }   
  76.            
  77.     }    
  78.   
  79. }    
  80.   

配置WEB-INF/flex/service-config.xml,在最后添加如下代码:

xml 代码

xml 代码

 

  1. <factories>  
  2.         <factory id=“spring” class=“com.fire.spring.SpringFactory”/>  
  3.     factories>  

 在使用remoteobject的时候,配置remote-config.xml时,如下进行配置:

xml 代码

 

  1. <destination id=“provinceService”>  
  2.         <properties>  
  3.             <factory>spring</factory>  
  4.             <source>provinceService</source>  
  5.         </properties>  
  6.     </destination>  

 

你可能感兴趣的:(spring,xml,Web,Hibernate,Flex)