Spring BlazeDS

本文详细介绍了使用SpringBlazeDS插件整合Spring、BlazeDS、Hibernate等技术,解决配置问题及远程对象调用的实现过程。通过搭建动态web项目、配置依赖库、使用注解方式注入bean,最终实现flex调用服务的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

乱七八糟的记录...

这里有需要的东西: http://blog.springsource.com/author/jgrelle/

之前找的资料都是2008年的,配置了半天什么都没配起来.后来才找到 spring 针对 BlazeDS 的插件.上面地址就能找到资料信息和下载地址.

具体使用方法下载下来的包里有API和文档了.

http://www.ityangba.com/thread-261-1-1.html 这里有个项目:flex+blazeds+hibernate+spring小示例.这里面没带包,所以我也没直接拿过来用,还是在 BlazeDS.war 解开后的项目基础上进行配置,增加了 spring 3.06 .这个版本会报找不到 org/aopalliance/intercept/MethodInterceptor ,去 http://sourceforge.net/projects/aopalliance/files/aopalliance/ 下载.还有就是这个示例也是需要上面的 Spring BlazeDS 插件.


2011-9-7 更新

另一篇视频.http://www.jamesward.com/2010/01/17/flex-4-and-java-spring-hibernate-in-flash-builder-4/

框架配置还是问题不断,最麻烦的就是测试.看了上面的视频,直接创建一个新的 dynamic web project, 然后再把 blazeds-turnkey 里 blazeds.war 的 lib +spring-flex-1.5.0.RELEASE+一些必要的 jar(hibernate Spring...) 弄进来,再结合http://www.ityangba.com/thread-261-1-1.html 示例的配置文件并在项目测试里创建tomcat运行环境.框架总算是搭建起来了.

再后来,flex调用 remoteObject 是连接上了.获取简单的字符串没问题,但要获取被注入的 bean 的时候,都提示 null. 上网搜了下.原来是需要把要注入的 bean 写入xml

<bean id="myxx" class="com.gitom.webPlatform.XX"></bean>

<bean id="foo" class="com.gitom.webPlatform.Foo">
        <property name="xx" ref="myxx"></property>
        <flex:remoting-destination />
</bean>

比如 flex 调用的是 foo 的 ,foo 里需要注入一个 xx 的 bean,需要如上写的配置.把 xx 注入.

原先在使用SSH的时候,都是通过annotation,自动扫描并注入.没怎么写过xml的配置,这次弄了半天annotation都不成功.才反应过来应该是因为 web.xml 配置是要使用XML来配.

<context:component-scan base-package="com.gitom"/> 没起作用...

而且通过控制台发现,当flex调用服务的时候,后台才去读取 bean 的配置文件,而不是在服务器一启动就把bean创建到内存里.


后来仔细看了下视频的配置文件.在 spring 的主配置文件里添加了如下的配置:

<flex:message-broker>
        <flex:remoting-service default-channels="my-amf"/>
</flex:message-broker>
<context:annotation-config/>
<context:component-scan base-package="com.gitom"/>

终于是实现 annotation 的方式注入了.

项目搭建成功...

总结一下:

主要的 lib:

hibernate 的 jar

spring 的 jar

spring-flex-1.5.0.RELEASE

blazeds-turnkey 代码包里 blazeds.war 里的 lib

在服务器环境下测试时,按照错误提示,增加一些公共jar.

项目搭建:

在eclipse里创建一个 dynamic web project ,把以上提及的 jar 放到项目的 lib 里.

拷贝 blazeds.war 里的 web-inf/flex 整个文件夹到项目的 WebContent/web-inf 文件夹里.

在 WebContent/web-inf 里创建 spring 文件夹(这个是放 spring bean 的文件夹.不建也行,可以直接放在 WebContent/web-inf 下,不过下面提供的 web.xml配置文件也要改下路径. ).

web.xml 在 WebContent/web-inf 目录下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="webPlatformDriver" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/ApplicationContext.xml</param-value>
	</context-param>
	<!-- spring整合BlazeDS的请求分发器Servlet -->
	<servlet>
		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/Application_BlazeDS.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
		<url-pattern>/messagebroker/*</url-pattern>
	</servlet-mapping>
	
	<!-- spring容器的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- Http Flex Session attribute and binding listener support -->
	<listener>
		<listener-class>flex.messaging.HttpFlexSession</listener-class>
	</listener>
	
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

ApplicationContext.xml spring bean的主配置文件. 在 spring  目录下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:flex="http://www.springframework.org/schema/flex"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/flex
		http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
	">
	<flex:message-broker>
		<flex:remoting-service default-channels="my-amf"/>
	</flex:message-broker>
	<context:annotation-config/>
	<context:component-scan base-package="com.gitom"/>
	
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	      <property name="locations">
			<list>
				<value>WEB-INF/c3p0.properties</value>
			</list>
		</property>
	</bean>
	<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${c3p0.driverClass}"></property>
        <property name="jdbcUrl" value="${c3p0.url}"></property>
        <property name="user" value="${c3p0.user}"></property>
        <property name="password" value="${c3p0.password}"></property>
        <property name="acquireIncrement"><value>5</value></property>
        <property name="initialPoolSize"><value>10</value></property>
        <property name="maxIdleTime"><value>60</value></property>
        <property name="maxPoolSize"><value>30</value></property>
        <property name="minPoolSize"><value>5</value></property>
        <property name="acquireRetryAttempts" value="10"></property>
        <property name="breakAfterAcquireFailure" value="false"></property>
	</bean>
	<bean id="mySessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="myDataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.gitom</value>
			</list>
		</property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop><!--使显示的sql语句-->
			</props>
		</property>
	</bean>
	<bean id="hibernateTemlpate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="mySessionFactory"></property>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="mySessionFactory"/>
	</bean>
	<tx:annotation-driven/>
	<aop:aspectj-autoproxy/>
</beans>

Application_BlazeDS.xml 现在是为空,因为是用 annotation 的配置方法来.如果要使用xml的配置在这里加入. 在 spring目录下,这里配置的bean只在flex调用的时候才会被创建.上面的 ApplicationContext.xml 里的是服务器启动就创建.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:flex="http://www.springframework.org/schema/flex"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/flex
		http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
	">
</beans>


创建tomcat的服务器环境.启动并测试该项目.按控制台输出的错误提示加载需要的公共包.


剩下的和SSH注入方法差不多,看看视频.基本就这样了.


项目就基本搭建完成了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值