本文章主要参考并基于--http://wangyisong.javaeye.com——跟我StepByStep学FLEX教程(首先对他表示感谢
)
请在参考我的文章时一定要先看看上面的文章(其实王一松大大已经写得很清楚了,各位看倌看了上面的文章,大可不看小弟的文章了)。
相关开发环境工具:
1、Flex Builder3 插件版。呵呵,这个就不用说了吧。
2、eclipse或者myeclipse。我是用的是eclipse3.4于MyEclipse7.5。
3、Java Development Kit(JDK),版本1.5以上(包括1.5)。作者使用的是1.6。
4、BlazeDS。我显得是最新版的
5、Tomcat。作者使用的6.0。
(关于开发环境的安装和配置,就不在此絮叨了,网上N多,请百度或Google)
现在就开始了;
1.将Spring的jar包拷贝到WEB-INF的lib下;
2.在web.xml中注册Spring,如下配置:
1
<!-- Spring configuration file (Not needed if you don't use Spring) -->
2
<context-param>
3
<param-name>contextConfigLocation</param-name>
4
<param-value>/WEB-INF/applicationContext.xml</param-value>
5
</context-param>
6
7
<!-- Spring ContextLoaderListener (Not needed if you don't use Spring) -->
8
<listener>
9
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10
</listener>
11
12
3、增加SpringFactory.java,代码如下(注:此代码是从王大大的blog上复制过来的,请对他表示感谢并请不要告诉他,(没得到的他的同意,请保密
))
1
package com.samples.factories;
2
3
import org.springframework.context.ApplicationContext;
4
import org.springframework.web.context.support.WebApplicationContextUtils;
5
import org.springframework.beans.BeansException;
6
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
7
8
import flex.messaging.FactoryInstance;
9
import flex.messaging.FlexFactory;
10
import flex.messaging.config.ConfigMap;
11
import flex.messaging.services.ServiceException;
12
13
/** *//**
14
* This interface is implemented by factory components which provide
15
* instances to the flex messaging framework. To configure flex data services
16
* to use this factory, add the following lines to your services-config.xml
17
* file (located in the WEB-INF/flex directory of your web application).
18
*
19
* <factories>
20
* <factory id="spring" class="flex.samples.factories.SpringFactory" />
21
* </factories>
22
*
23
* You also must configure the web application to use spring and must copy the spring.jar
24
* file into your WEB-INF/lib directory. To configure your app server to use spring,
25
* you add the following lines to your WEB-INF/web.xml file:
26
*
27
* <context-param>
28
* <param-name>contextConfigLocation</param-name>
29
* <param-value>/WEB-INF/applicationContext.xml</param-value>
30
* </context-param>
31
*
32
* <listener>
33
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
34
* </listener>
35
*
36
* Then you put your spring bean configuration in WEB-INF/applicationContext.xml (as per the
37
* line above). For example:
38
*
39
* <?xml version="1.0" encoding="UTF-8"?>
40
* <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
41
*
42
* <beans>
43
* <bean name="weatherBean" class="dev.weather.WeatherService" singleton="true"/>
44
* </beans>
45
*
46
* Now you are ready to define a destination in flex that maps to this existing service.
47
* To do this you'd add this to your WEB-INF/flex/remoting-config.xml:
48
*
49
* <destination id="WeatherService">
50
* <properties>
51
* <factory>spring</factory>
52
* <source>weatherBean</source>
53
* </properties>
54
* </destination>
55
*
56
* @author Jeff Vroom
57
*/
58
public class SpringFactory implements FlexFactory
59

{
60
private static final String SOURCE = "source";
61
62
/** *//**
63
* This method can be used to initialize the factory itself. It is called with configuration
64
* parameters from the factory tag which defines the id of the factory.
65
*/
66
public void initialize(String id, ConfigMap configMap)
{}
67
68
/** *//**
69
* This method is called when we initialize the definition of an instance
70
* which will be looked up by this factory. It should validate that
71
* the properties supplied are valid to define an instance.
72
* Any valid properties used for this configuration must be accessed to
73
* avoid warnings about unused configuration elements. If your factory
74
* is only used for application scoped components, this method can simply
75
* return a factory instance which delegates the creation of the component
76
* to the FactoryInstance's lookup method.
77
*/
78
public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
79
{
80
SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
81
instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
82
System.out.println("SpringSpringSpring" + instance.toString());
83
return instance;
84
} // end method createFactoryInstance()
85
86
/** *//**
87
* Returns the instance specified by the source
88
* and properties arguments. For the factory, this may mean
89
* constructing a new instance, optionally registering it in some other
90
* name space such as the session or JNDI, and then returning it
91
* or it may mean creating a new instance and returning it.
92
* This method is called for each request to operate on the
93
* given item by the system so it should be relatively efficient.
94
* <p>
95
* If your factory does not support the scope property, it
96
* report an error if scope is supplied in the properties
97
* for this instance.
98
*/
99
public Object lookup(FactoryInstance inst)
100
{
101
SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
102
return factoryInstance.lookup();
103
}
104
105
106
static class SpringFactoryInstance extends FactoryInstance
107
{
108
SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
109
{
110
super(factory, id, properties);
111
}
112
113
114
public String toString()
115
{
116
return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
117
}
118
119
public Object lookup()
120
{
121
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
122
String beanName = getSource();
123
124
try
125
{
126
return appContext.getBean(beanName);
127
}
128
catch (NoSuchBeanDefinitionException nexc)
129
{
130
ServiceException e = new ServiceException();
131
String msg = "Spring service named '" + beanName + "' does not exist.";
132
e.setMessage(msg);
133
e.setRootCause(nexc);
134
e.setDetails(msg);
135
e.setCode("Server.Processing");
136
throw e;
137
}
138
catch (BeansException bexc)
139
{
140
ServiceException e = new ServiceException();
141
String msg = "Unable to create Spring service named '" + beanName + "' ";
142
e.setMessage(msg);
143
e.setRootCause(bexc);
144
e.setDetails(msg);
145
e.setCode("Server.Processing");
146
throw e;
147
}
148
}
149
150
}
151
152
}
153
4.在services-config.xml中注册SpringFacotry,配置如下:
1
<factories>
2
<factory id="spring" class="com.samples.factories.SpringFactory"/>
3
</factories>
4
5
5.接下来就要在applicationContext.mxl中注册helloJavaFlexBean,配置如下
1
<bean id="helloJavaFlexBean" class="com.test.HelloJavaFlex">
2
</bean>
6.在remoting-config.xml中将SpringBean公开给Flex客户端,配置如下:
1
<destination id="helloJavaFlex">
2
<properties>
3
<factory>spring</factory>
4
<source>helloJavaFlexBean</source>
5
</properties>
6
</destination>
至此flex于spring的整合就ok。如果你有看王大大blog,那么你将获取相关的一些demo。解下来就是与hibernate的整合了
7.把Hibernate相关jar包拷贝到WEB-INF的lib下
8.添加hibernate.cfg.xml文件到src下
9.在applicationContext.xml中配置,通过Spring方式整合Hibernate
1
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
2
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
3
</bean>
10.增加Hibernate的对象映射文件,User.hbm.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
<!--
5
Mapping file autogenerated by MyEclipse Persistence Tools
6
-->
7
<hibernate-mapping>
8
<class name="com.model.User" table="user" catalog="test">
9
<id name="id" type="java.lang.Integer">
10
<column name="id" />
11
<generator class="native" />
12
</id>
13
<property name="username" type="java.lang.String">
14
<column name="username" />
15
</property>
16
<property name="password" type="java.lang.String">
17
<column name="password" />
18
</property>
19
</class>
20
</hibernate-mapping>
21
11.在applicationContext.xml中配置Dao
1
<bean id="userService" class="com.dao.impl.UserDaoImpl" >
2
<property name="sessionFactory" ref="sessionFactory"></property>
3
</bean>
这样flex和spring和hibernate就整合好了。
读者在读取上面的文章如果觉得很乱的话,可先请阅读王大大的文章后再来看作者的文章。
我会将我demo上传,大家可以下载
(注意在放整合是hibernate和spring的jar中有重复请自行排除重复的jar包,不过请一定注意对于asm.jar和asm-2.2.3.jar请排除asm-2.2.3.jar,作者在整合是根据jar版本排除时,先是留的asm_2.2.3.jar,结果启动tomcat报错:NoClassDefFoundError: org/objectweb/asm/Type ,将asm.jar重新拷回,排除asm_2.2.3.jar后运行tomgcat通过(orz,人品问题吧
))
http://www.rayfile.com/files/8864c370-6656-11df-b657-0015c55db73d/ 这个是我的demo的下载链接。