---
title: "FLEX和spring、hibernate的集成"
author: "caowei3047"
date: "2008-12-10T10:03:40Z"
tags: ["Flex", "Hibernate", "Spring", "Web", "XML"]
slug: "flex-spring-hibernate"
draft: false
lastmod: "2008-12-10T10:03:40Z"
id: 289849
url: "https://caowei3047.iteye.com/blog/289849"
---

# [FLEX和spring、hibernate的集成](http://www.daoyu.com/?p=72)

Posted under [Web相关](http://www.daoyu.com/?cat=5 "显示Web相关的所有日志") by admin

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

编写SpringFactory.java类：

java 代码

 

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

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

xml 代码

xml 代码

 

1. &lt;factories&gt;
2. &lt;factory id=“spring” class=“com.fire.spring.SpringFactory”/&gt;
3. factories&gt;

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

xml 代码

 

1. &lt;destination id=“provinceService”&gt;
2. &lt;properties&gt;
3. &lt;factory&gt;spring&lt;/factory&gt;
4. &lt;source&gt;provinceService&lt;/source&gt;
5. &lt;/properties&gt;
6. &lt;/destination&gt;

 

[« FLEX带进度指示的文件上传](http://www.daoyu.com/?p=71)

[FMS回调FLEX端的Function »](http://www.daoyu.com/?p=73)