本文章主要參考并基于--http://wangyisong.javaeye.com——跟我StepByStep學(xué)FLEX教程(首先對(duì)他表示感謝
)
請(qǐng)?jiān)趨⒖嘉业奈恼聲r(shí)一定要先看看上面的文章(其實(shí)王一松大大已經(jīng)寫得很清楚了,各位看倌看了上面的文章,大可不看小弟的文章了)。
相關(guān)開(kāi)發(fā)環(huán)境工具:
1、Flex Builder3 插件版。呵呵,這個(gè)就不用說(shuō)了吧。
2、eclipse或者myeclipse。我是用的是eclipse3.4于MyEclipse7.5。
3、Java Development Kit(JDK),版本1.5以上(包括1.5)。作者使用的是1.6。
4、BlazeDS。我顯得是最新版的
5、Tomcat。作者使用的6.0。
(關(guān)于開(kāi)發(fā)環(huán)境的安裝和配置,就不在此絮叨了,網(wǎng)上N多,請(qǐng)百度或Google)
現(xiàn)在就開(kāi)始了;
1.將Spring的jar包拷貝到WEB-INF的lib下;
2.在web.xml中注冊(cè)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上復(fù)制過(guò)來(lái)的,請(qǐng)對(duì)他表示感謝并請(qǐng)不要告訴他,(沒(méi)得到的他的同意,請(qǐng)保密
))
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中注冊(cè)SpringFacotry,配置如下:
1
<factories>
2
<factory id="spring" class="com.samples.factories.SpringFactory"/>
3
</factories>
4
5
5.接下來(lái)就要在applicationContext.mxl中注冊(cè)helloJavaFlexBean,配置如下
1
<bean id="helloJavaFlexBean" class="com.test.HelloJavaFlex">
2
</bean>
6.在remoting-config.xml中將SpringBean公開(kāi)給Flex客戶端,配置如下:
1
<destination id="helloJavaFlex">
2
<properties>
3
<factory>spring</factory>
4
<source>helloJavaFlexBean</source>
5
</properties>
6
</destination>
至此flex于spring的整合就ok。如果你有看王大大blog,那么你將獲取相關(guān)的一些demo。解下來(lái)就是與hibernate的整合了
7.把Hibernate相關(guān)jar包拷貝到WEB-INF的lib下
8.添加hibernate.cfg.xml文件到src下
9.在applicationContext.xml中配置,通過(guò)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的對(duì)象映射文件,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就整合好了。
讀者在讀取上面的文章如果覺(jué)得很亂的話,可先請(qǐng)閱讀王大大的文章后再來(lái)看作者的文章。
我會(huì)將我demo上傳,大家可以下載
(注意在放整合是hibernate和spring的jar中有重復(fù)請(qǐng)自行排除重復(fù)的jar包,不過(guò)請(qǐng)一定注意對(duì)于asm.jar和asm-2.2.3.jar請(qǐng)排除asm-2.2.3.jar,作者在整合是根據(jù)jar版本排除時(shí),先是留的asm_2.2.3.jar,結(jié)果啟動(dòng)tomcat報(bào)錯(cuò):NoClassDefFoundError: org/objectweb/asm/Type ,將asm.jar重新拷回,排除asm_2.2.3.jar后運(yùn)行tomgcat通過(guò)(orz,人品問(wèn)題吧
))
http://www.rayfile.com/files/8864c370-6656-11df-b657-0015c55db73d/ 這個(gè)是我的demo的下載鏈接。