案例学习BlazeDS+Spring之二Spring BlazeDS Integration 101
发布时间:2020-12-15 04:39:02 所属栏目:百科 来源:网络整理
导读:Spring BlazeDS Integration 101 这展示BlazeDS+Spring最基本的DEMO。 一、运行DEMO: 1、运行应用程序,URL:http://localhost:8400/spring-flex-testdrive/spring-blazeds-101/index.html 2、单击“Get Data”:DataGrid将填充数据,这个数据是ProductDAO
Spring BlazeDS Integration 101
这展示BlazeDS+Spring最基本的DEMO。
一、运行DEMO:
1、运行应用程序,URL:http://localhost:8400/spring-flex-testdrive/spring-blazeds-101/index.html
2、单击“Get Data”:DataGrid将填充数据,这个数据是ProductDAO Java类的findAll()方法返回的数据。
二、理解代码:
1、spring-blazeds-101/src/Main.mxml:
?? ?在Main.mxml中,使用RemoteObject来调用服务端方法:
<s:RemoteObject id="ro" destination="productService" endpoint="http://localhost:8400/spring-flex-testdrive/messagebroker/amf"/>
?? ?通过RemoteObject标记使您可以访问使用 Action Message Format (AMF) 编码的 Java 对象的方法。destination属性是服务的目标。该值应与服务器上flex-servlet.xml文件中的目标条目匹配。endpoint属性可使开发人员在编译或以编程方式创建 ChannelSet 时快速指定 RemoteObject 目标的端点,而无需引用服务配置文件。
?? ?RemoteObject的使用方式很简单,直接按远程对象上的方法调用即可,如ro.findAll()。如果不想处理此方法调用后的异步事件,直接将方法的lastResult值赋给控制的dataProvider即可,如:
DataGrid的dataProvider="{ro.findAll.lastResult}"
2、{context-root}/WEB-INF/flex-servlet.xml?
?? ?使用<flex:remoting-destination />标签,将Spring的bean通过BlazeDS暴露给前台。如:
<flex:remoting-destination ref="productService" />
此处引用的productService其实为一Spring的bean。
3、{context-root}/WEB-INF/spring/app-config.xml?
该配置文件中定义了一个ProductDAO bean,dataSource通过构造器注入。。
<bean id="productService" class="org.springframework.flex.samples.product.ProductDAO">
<constructor-arg ref="dataSource" />
</bean>
4、datasource在infrastructure-config.xml中定义:
该数据源使用的是h2数据库,在Test Drive中,用H2替换了HSQLDB。H2数据库的特点如官方(
http://www.h2database.com/)所言:
“Very fast,open source,JDBC API;Embedded and server modes; in-memory databases;Browser based Console application;Small footprint: around 1 MB jar file size”
dbInit bean用于初始化示例数据库,主要是使用JdbcTemplate来创建数据库、表,插入相关数据等。
<jdbc:embedded-database id="dataSource" type="H2" />
<bean id="dbInit" class="org.springframework.flex.samples.util.DatabaseInitializer">
<constructor-arg ref="dataSource" />
</bean>
5、/org/springframework/flex/samples/product/ProducDAO.java?
ProductDAO是数据访问对象,负责调用数据库,并返回数据给前台。
public ProductDAO(DataSource dataSource) {
?? ?this.dataSource = dataSource;//构造器注入
}
public List<Product> findAll() {
?? ?List<Product> list = new ArrayList<Product>();
?? ?Connection c = null;
?? ?try {
?? ? ? c = this.dataSource.getConnection();
?? ? ? Statement s = c.createStatement();
?? ? ? ResultSet rs = s.executeQuery("SELECT * FROM product ORDER BY name");
?? ? ? while (rs.next()) {
?? ? ? ? list.add(new Product(rs.getInt("id"),rs.getString("name"),rs.getString("description"),rs.getString("image"),
?? ? ? ? ? ? ? ? ? ?rs.getString("category"),rs.getDouble("price"),rs.getInt("qty")));
?? ? ? }
?? ?} catch (SQLException e) {
?? ? ? ?e.printStackTrace();
?? ? ? ?throw new RuntimeException(e);
?? ?}
?? ?return list;
}
除了findAll方法外,还有诸如:findByName,findById,create,update,remove。
6、/org/springframework/flex/samples/product/IProductDAO.java
ProductDAO,包括其它DAO类,都实现相应的DAO接口,这些接口继承自IGenericDAO。
public interface IProductDAO extends IGenericDAO<Product>{}
7、/org/springframework/flex/samples/dao/IGenericDAO.java
接口IGenericDAO如下定义:
public interface IGenericDAO<T> {
?? ?public List<T> findAll();
?? ?public List<T> findByName(String name);
?? ?public T findById(int id);
?? ?public T create(T item);
?? ?public boolean update(T item);
?? ?public boolean remove(T item);
}
三、小结:
?? ?这个DEMO使用底层的JDBC代码实现数据库操作,并没有真成抽象出一DAO来简化操作,这样做是保持本DEMO更简单,提供一个基本的DEMO,只关注Spring和BlazeDS的集成。
?? ?使用RemoteObject,你能直接调用部署在应用服务器上Java对象的方法,并且接受返回值。返回值可以是简单的数据类型,对象,对像集合,对象图等等。
?? ?使用Spring BlazeDS集成,Spring beans使用remoting-destination标签来暴露。如productService bean在app-config.xml里定义,在flex-servlet.xml作为remoting destination暴露。
?? ?服务端方法返回的Java对象反序化成动态或类型化的AS对象。在这个示例中,并没有显示定义Product Java类的AS版本,因此Product对象反序例为动态对象。在InSync03之后的示例,将开始使用强类型对象。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |