flex与Spring结合

 adobe发布的blazeDS可以使得flex通过RemoteObject方式访问Java对象,而这个Java对象也可以纳入到Spring的管理中,以下是一个例子的主要代码。

开发环境:MyEclipse6.5(已经集成了最新版的Spring2.5) , Tomcat5.5 , JDK5 , BlazeDS

服务端代码

IService.java

public interface IService {
 
 String addItem();
 
 String updateItem(Item item) ;
 
 String deleteItem(String id) ;

}

myServiceImpl.java

public class myServiceImpl implements IService {

 /* (non-Javadoc)
  * @see com.ultrapower.myspring.todo.service.IService#addItem()
  */
 public String addItem() {
  // TODO Auto-generated method stub
  System.out.println("add ok");
  return "add ok";
 }

 /* (non-Javadoc)
  * @see com.ultrapower.myspring.todo.service.IService#deleteItem(java.lang.String)
  */
 public String deleteItem(String id) {
  // TODO Auto-generated method stub
  System.out.println("delete ok");
  return "delete ok";
 }

 /* (non-Javadoc)
  * @see com.ultrapower.myspring.todo.service.IService#updateItem(com.ultrapower.myspring.datamodel.Item)
  */
 public String updateItem(Item item) {
  // TODO Auto-generated method stub
  System.out.println("update ok");
  return "update ok";
 }

}

Item.java

public class Item {
 private String id ;
 private String name ;
 private String value ;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getValue() {
  return value;
 }
 public void setValue(String value) {
  this.value = value;
 }
 
 
}

SpringFactory.java 重要的Java类

public class SpringFactory implements FlexFactory {

 private static final String SOURCE = "source";

 public void initialize(String id, ConfigMap configMap) {
 }

 public FactoryInstance createFactoryInstance(String id, ConfigMap properties) {
  SpringFactoryInstance instance = new SpringFactoryInstance(this, id,
    properties);
  instance.setSource(properties.getPropertyAsString(SOURCE, instance
    .getId()));
  return instance;
 } // end method createFactoryInstance()

 public Object lookup(FactoryInstance inst) {
  SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
  return factoryInstance.lookup();
 }

 static class SpringFactoryInstance extends FactoryInstance {
  SpringFactoryInstance(SpringFactory factory, String id,
    ConfigMap properties) {
   super(factory, id, properties);
  }

  public String toString() {
   return "SpringFactory instance for id=" + getId() + " source="
     + getSource() + " scope=" + getScope();
  }

  public Object lookup() {
   ApplicationContext appContext = WebApplicationContextUtils
     .getWebApplicationContext(flex.messaging.FlexContext
       .getServletConfig().getServletContext());

   String beanName = getSource();
   try {
    return appContext.getBean(beanName);
   } catch (NoSuchBeanDefinitionException nexc) {
    ServiceException e = new ServiceException();
    String msg = "Spring service named '" + beanName
      + "' does not exist.";
    e.setMessage(msg);
    e.setRootCause(nexc);
    e.setDetails(msg);
    e.setCode("Server.Processing");

    throw e;
   } catch (BeansException bexc) {
    ServiceException e = new ServiceException();
    String msg = "Unable to create Spring service named '"
      + beanName + "' ";
    e.setMessage(msg);
    e.setRootCause(bexc);
    e.setDetails(msg);
    e.setCode("Server.Processing");
    throw e;
   }
  }
 }

}

applicatonContext.xml

添加:

<bean id="myService" class="mypackage.myServiceImpl"/>

web.xml

添加:

<servlet>
     <servlet-name>MessageBrokerServlet</servlet-name>
     <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
     <init-param>
         <param-name>services.configuration.file</param-name>
         <param-value>/WEB-INF/flex/services-config.xml</param-value>
     </init-param>
     <init-param>
     <param-name>flex.write.path</param-name>
     <param-value>/WEB-INF/flex</param-value>
    </init-param>    
     <load-on-startup>1</load-on-startup>
 </servlet>

blazeDS配置文件:

services-config.xml中添加:

<factories>
     <factory id="spring" class="mypackage.SpringFactory" />
    </factories>

 remoting-config.xml中添加:

<destination id="todoService">
     <properties>
        <factory>spring</factory>
        <source>myService</source>
     </properties>
   </destination>

flex通过RemoteObject调用该IService接口。

flex端代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.rpc.events.ResultEvent;
   private function init():void{
    
   }
   private function add(event:Event):void{
    flexSpringService.addItem();
   }
   private function update(event:Event):void{
    var itemObj:ItemVO = new ItemVO();
    itemObj.id="5"
    flexSpringService.updateItem(itemObj);
   }
   private function deletet(event:Event):void{    
    flexSpringService.deleteItem("2");
   }
   private function addOk(event:ResultEvent):void{
    Alert.show("add ok");
    resultText.text = String(event.result) + "/n" + resultText.text;
   }
   private function updateOk(event:ResultEvent):void{
    Alert.show("update ok");
    resultText.text = String(event.result) + "/n" + resultText.text;
   }
   private function deleteOk(event:ResultEvent):void{
    Alert.show("delete ok");
    resultText.text = String(event.result) + "/n" + resultText.text;
   }
  ]]>
 </mx:Script>
 <mx:RemoteObject id="flexSpringService" destination="todoService">
  <mx:method name="addItem" result="addOk(event);"/>
  <mx:method name="updateItem" result="updateOk(event);"/>
  <mx:method name="deleteItem" result="deleteOk(event);"/>
 </mx:RemoteObject>
 <mx:Panel width="300" height="300">
  <mx:TextArea id="resultText" width="100%" height="80%"/>
  <mx:HBox width="100%" height="20">
   <mx:Button label="增加" click="add(event);"/>
   <mx:Button label="修改" click="update(event);"/>
   <mx:Button label="删除" click="deletet(event);"/>
  </mx:HBox>
 </mx:Panel>
</mx:Application>

ItemVO 对象是与java中Item对应的对象。

以上是该例子的核心代码,如有不清楚的地方可以直接回复,或者与我联系,我的邮箱是:xieyf_0413@163.com

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值