Spring+flex(Blazeds)註解方式創建項目
轉自:http://szydnjw.javaeye.com/blog/852134
本人第一次發表文章,如有不妥,請指正。
進入正題,spring+flex網上方法很多,但用註解的文章很少,現用註解的方式配置一個簡單的實例。
所用的開發環境eclipse3.5+flex4+spring3+blazeds(4.0.1.17657)
ps:Blazeds版本最低爲4.0.1.17657+
因爲用Blazeds所以SPRING就不必專門去下載了。先創建個基本FLEX+java的項目,相信大家一定多會。不會的去網上找下,一大推。主要是在讓我們選擇 blazeds war文件時,我們選擇blazeds-spring.war(版本4.0.1.17657+)這樣就自動把所有的JAR包多放到lib包下了包括SPRING的。
首先配置WEB.XML
接着創建FLEX APP命名隨便,我現在命名爲index.mxml,代碼如下
接着是JAVA端程序
MyService.java
然後我們在WebContent/WEB-INF/spring下,修改app-config.xml SPRING配置文件。該文件是在選擇BLAZEDS-SPRING.WAR後自動生成的。
修改後的 app-config.xml
好了,啓動TOMCAT,然後輸入地址,我的是
http://localhost:9090/oa/index.html
應該前後臺多有東西輸出了吧!祝大家好運!
進入正題,spring+flex網上方法很多,但用註解的文章很少,現用註解的方式配置一個簡單的實例。
所用的開發環境eclipse3.5+flex4+spring3+blazeds(4.0.1.17657)
ps:Blazeds版本最低爲4.0.1.17657+
因爲用Blazeds所以SPRING就不必專門去下載了。先創建個基本FLEX+java的項目,相信大家一定多會。不會的去網上找下,一大推。主要是在讓我們選擇 blazeds war文件時,我們選擇blazeds-spring.war(版本4.0.1.17657+)這樣就自動把所有的JAR包多放到lib包下了包括SPRING的。
首先配置WEB.XML
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <display-name>BlazeDS Spring Integration Application</display-name>
- <description>BlazeDS Spring Integration Application</description>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/spring/infrastructure-config.xml,
- /WEB-INF/spring/security-config.xml,
- /WEB-INF/spring/app-config.xml
- </param-value>
- </context-param>
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- Http Flex Session attribute and binding listener support -->
- <listener>
- <listener-class>flex.messaging.HttpFlexSession</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- Spring Dispatcher Servlet -->
- <servlet>
- <servlet-name>flex</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- begin rds
- <servlet>
- <servlet-name>RDSDispatchServlet</servlet-name>
- <display-name>RDSDispatchServlet</display-name>
- <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
- <init-param>
- <param-name>useAppserverSecurity</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>messageBrokerId</param-name>
- <param-value>_messageBroker</param-value>
- </init-param>
- <load-on-startup>10</load-on-startup>
- </servlet>
- <servlet-mapping id="RDS_DISPATCH_MAPPING">
- <servlet-name>RDSDispatchServlet</servlet-name>
- <url-pattern>/CFIDE/main/ide.cfm</url-pattern>
- </servlet-mapping>
- end rds -->
- <servlet-mapping>
- <servlet-name>flex</servlet-name>
- <url-pattern>/messagebroker/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- </welcome-file-list>
- <!-- for WebSphere deployment, please uncomment -->
- <!--
- <resource-ref>
- <description>Flex Messaging WorkManager</description>
- <res-ref-name>wm/MessagingWorkManager</res-ref-name>
- <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
- <res-auth>Container</res-auth>
- <res-sharing-scope>Shareable</res-sharing-scope>
- </resource-ref>
- -->
- </web-app>
接着創建FLEX APP命名隨便,我現在命名爲index.mxml,代碼如下
- <?xml version="1.0" encoding="utf-8"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
- <s:layout>
- <s:BasicLayout/>
- </s:layout>
- <fx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import mx.rpc.events.ResultEvent;
- protected function button1_clickHandler(event:MouseEvent):void
- {
- service.firstApp("Flex");
- }
- protected function remote_resultHandler(event:ResultEvent):void
- {
- Alert.show(String(event.result));
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- 將非可視元素(例如服務、值對象)放在此處 -->
- <s:RemoteObject id="service" destination="myService" result="remote_resultHandler(event)" endpoint="messagebroker/amf"/>
- </fx:Declarations>
- <s:Button x="72" y="101" label="按鈕" click="button1_clickHandler(event)"/>
- </s:Application>
接着是JAVA端程序
MyService.java
- package com.test.service;
- import org.springframework.flex.remoting.RemotingDestination;
- import org.springframework.flex.remoting.RemotingInclude;
- import org.springframework.stereotype.Service;
- @Service("myService")
- @RemotingDestination(channels = { "my-amf", "my-secure-amf" })
- public class MyService {
- @RemotingInclude
- public String firstApp(String info){
- System.out.println(info);//從FLEX端獲取的信息
- return "Hello World "+info;//返回給FLEX
- }
- }
然後我們在WebContent/WEB-INF/spring下,修改app-config.xml SPRING配置文件。該文件是在選擇BLAZEDS-SPRING.WAR後自動生成的。
修改後的 app-config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:security="http://www.springframework.org/schema/security"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:component-scan base-package="com.test.service" />
- <context:annotation-config />
- </beans>
好了,啓動TOMCAT,然後輸入地址,我的是
http://localhost:9090/oa/index.html
應該前後臺多有東西輸出了吧!祝大家好運!