整合Flash Builder4.0+MyEclipse 8.6+BlazeDS+Sping 3.0+Hibernate3.3+MySQL5.5+Tomcat+JDK1.6
,设置好环境变量。
Tomcat 6.0 。 去官方下载 MyEclipse 8.6 。下载完毕,我先问候它妈一下--那么鬼难用! 在弹出的对话框的左侧栏,点 General--> 点 Capabilities ,然后把 Classic Update 前的框选上;其次是对于 Flash Builder 的网页模版 js 报错,解决方法是: 在 window--preferences--MyEclipse--Validation 然后把 JavaScript Validator 这一项的 Manual 和 Build 的勾去掉。
设置一下 Tomcat 。
去官方网站下载 Flash Builder 4.0 插件版以及 BlazeDS 。安装前者以及解压后者。
下载 MySQL 以及 GUI (本人比较懒)和 jdbc 的 jar 包。
Spring 和 Hibernate 均使用 MyEclipse 内置即可。
为了可以调试,还必须下载调试版的 Flash player.
现在开始我们的整合之旅。
我们将会写一个登录验证的例子。我们遵循从底层到上层的开发模式,由数据库开始。打开 MySQL 的 GUI 工具,创建一个叫做 MyFlexDB 的数据库。再创建一个叫做" User "的表,表的字段包含了 id , username , password 三个字段,其中 id 为自增并且为表主键。脚本如下:
CREATE TABLE `myflexdb`.`User` (
`id` INTEGER NOT NULL DEFAULT NULL AUTO_INCREMENT,
`username` VARCHAR(45),
`password` VARCHAR(45),
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
接着,我们创建一个 Flex 工程,名字叫做 MyFlex 。截屏如下:
输出配置信息,上下文根目录和内容文件夹如下:
令人很不爽的是 context 竟然不可以修改,真的不爽,搞到得硬编码指定 endpoint 值。点击 next ,看到 最后点击 finish.
在服务端编写一个测试类 Test ,在客户端进行调用,配置 remoting-config.xml (位于 WEB-INF 文件夹下面的 Flex 文件夹)。
Test 类如下:
/**
* @author 黄记新
*
*/
public class Test {
public String testStr() {
return "hello, 黄记新 ";
}
}
配置文件修改成为:
[/b][b]Test
默认使用 my-amf 通道即可。
前端主程序文件如下:
flex/spark " xmlns:mx=" library://ns.adobe.com/flex/mx " minWidth=" 955 " minHeight=" 600 " >
用 MyEclipse 部署一下,启动服务器。
允许之后我们可以看到这样的画面:
点击按钮可以看到界面报错了,如下图: 原因是上下文错误。我们必须显式地指出 endpoint 的值。修改一下主文件,在远程类里面加上 : endpoint ='http://127.0.0.1:8080/MyFlex/messagebroker/amf'
再次运行可以看到:
接下来测试一下调试环境,使用调试的方式运行程序。
就是喜欢 MyEclipse 这种简单的调试环境。调试也成功了。
到此我们开始进入第二阶段的整合。
首先为工程加入 Spring 的 jar 包。如图所示,一般也别管什么东西了就一股脑加上吧。 点击 next 可以看到 此处主要是让用户是否对工程加入 AOP 支持和新建上下文配置文件的目录。
打开 Hibernate 的视图,新建数据库链接。
新建此链接等下我们在加入 Hibernate 支持的时候会使用到。
此处我们使用 Spring 来管理 Hibernate 链接。
此时 Spring 的上下文配置文件如下:
org.hibernate.dialect.MySQLDialect
新建三个包: com.myflex.model , com.myflex.service, com.myflex.dao
接下来我们做数据库映射,从数据库生成 VO , dao 。打开 Hibernate 视图。映射生成结果如图: 将 dao 文件拖拽到另外一个包中。
系统自动更新配置文件和引用位置。
我们做一下测试,顺便加几条数据到数据库当中。修改前面的 Test 类如下:
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
import com.myflex.dao.UserDAO;
import com.myflex.model.User;
/**
*
*/
/**
* @author 黄记新
*
*/
public class Test {
public String testStr() {
return "hello, 黄记新 ";
}
public static void main(String[] args) {
User user=new User();
user.setUsername(" 黄记新 ");
user.setPassword("123456");
// 使用上下文获得 dao 对象;
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext .xml");
UserDAO userDao=(UserDAO)context.getBean("UserDAO");
userDao.save(user);
// 查找输出数据库对象
List list=userDao.findAll();
for (User user2 : list) {
System.out.println(user2.getId()+" "+user2.getUsername()+" "+user2.getPassword());
}
}
}
运行之后可以看到:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA pplicationContext).
log4j:WARN Please initialize the log4j system properly.
1 黄记新 123456
2 韩冰 123456
接下来我们进入第三阶段,配置 web.xml. 主要是配置日志文件, Spring 启动侦听器等。配置完毕后它的文件内容如下:
[/b][b]MyFlex
[/b][b]flex.class.path [/b][b]/WEB-INF/ flex / hotfixes ,/WEB-INF/ flex /jars [/b][b]contextConfigLocation [/b][b]/WEB-INF/classes/applicationContext.xml [/b][b]log4jConfigLocation [/b][b]/WEB-INF/log4j.properties [/b][b]webAppRootKey [/b][b]webApp.root Flex Session attribute and binding listener support --> [/b][b]flex.messaging.HttpFlexSession [/b][b]org.springframework.web.context.ContextLoaderListe ner [/b][b]org.springframework.web.util.Log4jConfigListener [/b][b]MessageBrokerServlet [/b][b]flex.messaging.MessageBrokerServlet [/b][b]services.configuration.file [/b][b]/WEB-INF/ flex /services-config.xml [/b][b]flex.write.path [/b][b]/WEB-INF/ flex [/b][b]1 [/b][b]MessageBrokerServlet [/b][b]/ messagebroker /* [/b][b]index.html [/b][b]index.htm [/b][b]index.jsp [/b][b]default.html [/b][b]default.htm [/b][b]default.jsp [/b][b]/MyFlex.html [/b][b]BASIC [/b][b]Flex Messaging WorkManager [/b][b]wm /MessagingWorkManager com.ibm.websphere.asynchbeans.WorkManager Container [/b][b]Shareable 接下来我们增加一个工程类以及修改配置文件:
在服务端增加一个工程类,代码如下:
package com.myflex.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplica tionContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefini tionException;
import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;
/**
* This interface is implemented by factory components which provide
* instances to the flex messaging framework. To configure flex data services
* to use this factory, add the following lines to your services - config.xml
* file (located in the WEB - INF/ flex directory of your web application).
*
*
*
*
*
* You also must configure the web application to use spring and must copy the spring.jar
* file into your WEB - INF/ lib directory. To configure your app server to use spring,
* you add the following lines to your WEB - INF/web.xml file:
*
*
* contextConfigLocation
* /WEB - INF/applicationContext.xml
*
*
*
* org.springframework.web.context.ContextLo aderListener
*
*
* Then you put your spring bean configuration in WEB - INF/applicationContext.xml (as per the
* line above). For example:
*
*
*
*
*
*
*
*
* Now you are ready to define a destination in flex that maps to this existing service.
* To do this you'd add this to your WEB - INF/ flex / remoting - config.xml:
*
*
*
* spring
* weatherBean
*
*
*
* @author Jeff Vroom
*/
public class SpringFactory implements FlexFactory
{ private static final String SOURCE = "source" ; /** * This method can be used to initialize the factory itself. It is called with configuration * parameters from the factory tag which defines the id of the factory. */ public void initialize(String id, ConfigMap configMap) {} /** * This method is called when we initialize the definition of an instance * which will be looked up by this factory. It should validate that * the properties supplied are valid to define an instance. * Any valid properties used for this configuration must be accessed to * avoid warnings about unused configuration elements. If your factory * is only used for application scoped components, this method can simply * return a factory instance which delegates the creation of the component * to the FactoryInstance's lookup method. */ 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() /** * Returns the instance specified by the source * and properties arguments. For the factory, this may mean * constructing a new instance, optionally registering it in some other * name space such as the session or JNDI, and then returning it * or it may mean creating a new instance and returning it. * This method is called for each request to operate on the * given item by the system so it should be relatively efficient. * * If your factory does not support the scope property, it * report an error if scope is supplied in the properties * for this 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 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; } } } } 在 services-config.xml 文件当中增加如下标签:
在 remoting-config.xml 文件当中增加:
[/b][b]springFactory
[/b][b]UserDAO
修改主文件:
flex/spark "
xmlns:mx=" library://ns.adobe.com/flex/mx "
minWidth=" 955 " minHeight=" 600 "
pageTitle=" MyFlex " >
最后阶段了,就是为 DAO 加入事务能力。
[/b][b]
[/b][b]PROPAGATION_REQUIRED
猜你喜欢
转载自wok71wok.iteye.com/blog/1572928
今日推荐
周排行