这是我从几个文档综合而来,虽是亲手写的,却是整理而已,故不算原创,如有错不足之处,请勿喷!
Flex和Spring的整合:
1、先把spring支持的包拷贝到lib下。
2、web.xml注册spring。注册代码如下:
<!-- Spring configuration file (Not needed if you don't use Spring) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener(Not needed if you don't use Spring) -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
3、增加SpringFactory.java。代码如下:
package com.samples.factories;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.services.ServiceException;
public class SpringFactory implements FlexFactory{
private static final String SOURCE = "source";
public FactoryInstance createFactoryInstance(String id, ConfigMap properties){
SpringFactoryInstance instance = new SpringFactoryInstance(this,id,properties);
instance.setSource(properties.getPropertyAsString(SOURCE,instance.getId()));
System.out.println("SpringSpringSpring" + instance.toString());
return instance;
}
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 ac = WebApplicationContextUtils.getWebApplicationContext(flex
.messaging.FlexContext.getServletConfig().getServletContext());
String beanName = getSource();
try{
return ac.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;
}
}
}
}
4、在service-config.xml中注册SpringFactory,配置如下:
<factories>
<factory id="spring" class="com.samples.factories.SpringFactory"/>
</factories>
至此,Spring配置完成。如果Tomcat日志不报错,说明spring添加成功。
【配置数据源】
在applicationContext.xml中增加datasource的配置,如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://...?useUnicode=true&characterEncoding="UTF-8"/>
<property name="username" value="root">
<property name="password" value="sa">
<bean>
5、增加业务层代码:
【Bean】
package pojo;
public class Userinfor{
private int id;
private String name;
private String pwd;
setter and getter method
}
【Dao】
package dao;
import java.util.Collection;
import org.springframework.dao.DataAccessException;
public interface UserDao{
public Collection findAll() throws DataAccessException;
}
【Impl】
package dao.impl;
import java.util.Collection;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.UserDao;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
public Collection findAll() throws DataAccessException{
List list = this.getHibernateTemplate().find("from Userinfor as a");
return list;
}
}
6、在applicationContext.xml中配置bean,配置如下:
<bean id="UserDaoBean" class="dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
7、在remoting-config.xml中配置向Flex客户端公开的bean,配置如下:
<destination id="userService">
<properties>
<factory>spring</factory>
<source>UserDaoBean</source>
</properties>
</destination>
8、配置Flex客户端的代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.rpc.events.FaultEvnet;
private function findAllFaultHandler(event:FaultEvent):void{
Alert.show(event.fault.faultString,"Error");
}
]]>
</mx:Script>
<mx:RemoteObject id="srv" destination="userService">
<mx:method name="findAll" fault="findAllFaultHandler(event)"/>
</mx:RemoteObject>
<mx:HDividedBox width="100%" height="100%">
<mx:Panel title="Inventory Management" width="100%" height="100%">
<mx:DataGrid id="list" width="100%" height="100%" dataProvider="{srv.findAll.lastResult}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="pwd" headerText="password">
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:HDividedBox>
</mx:Application>
9、最后需要注意的地方:
右击项目-->properties-->Flex Compiler -->查看Additional compiler arguments:是否是项目在Tomcat目录下的工程的位置。