Spring+flex(Blazeds)注解方式创建项目

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
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">   
  3. <web-app>   
  4.   
  5.     <display-name>BlazeDS Spring Integration Application</display-name>   
  6.     <description>BlazeDS Spring Integration Application</description>   
  7.   
  8.     <context-param>   
  9.         <param-name>contextConfigLocation</param-name>   
  10.         <param-value>   
  11.             /WEB-INF/spring/infrastructure-config.xml,    
  12.             /WEB-INF/spring/security-config.xml,   
  13.             /WEB-INF/spring/app-config.xml   
  14.         </param-value>   
  15.     </context-param>   
  16.        
  17.     <filter>   
  18.         <filter-name>springSecurityFilterChain</filter-name>   
  19.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>   
  20.     </filter>   
  21.   
  22.     <filter-mapping>   
  23.       <filter-name>springSecurityFilterChain</filter-name>   
  24.       <url-pattern>/*</url-pattern>   
  25.     </filter-mapping>   
  26.   
  27.     <!-- Http Flex Session attribute and binding listener support -->   
  28.     <listener>   
  29.         <listener-class>flex.messaging.HttpFlexSession</listener-class>   
  30.     </listener>   
  31.   
  32.     <listener>   
  33.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  34.     </listener>   
  35.   
  36.     <!-- Spring Dispatcher Servlet -->      
  37.     <servlet>   
  38.         <servlet-name>flex</servlet-name>   
  39.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
  40.         <load-on-startup>1</load-on-startup>   
  41.     </servlet>   
  42.   
  43. <!-- begin rds   
  44.     <servlet>   
  45.         <servlet-name>RDSDispatchServlet</servlet-name>   
  46.         <display-name>RDSDispatchServlet</display-name>   
  47.         <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>   
  48.         <init-param>   
  49.             <param-name>useAppserverSecurity</param-name>   
  50.             <param-value>true</param-value>   
  51.         </init-param>   
  52.         <init-param>   
  53.             <param-name>messageBrokerId</param-name>   
  54.             <param-value>_messageBroker</param-value>   
  55.         </init-param>   
  56.         <load-on-startup>10</load-on-startup>   
  57.     </servlet>   
  58.   
  59.     <servlet-mapping id="RDS_DISPATCH_MAPPING">   
  60.         <servlet-name>RDSDispatchServlet</servlet-name>   
  61.         <url-pattern>/CFIDE/main/ide.cfm</url-pattern>   
  62.     </servlet-mapping>   
  63. end rds -->   
  64.   
  65.     <servlet-mapping>   
  66.         <servlet-name>flex</servlet-name>   
  67.         <url-pattern>/messagebroker/*</url-pattern>   
  68.      </servlet-mapping>   
  69.   
  70.     <welcome-file-list>   
  71.         <welcome-file>index.html</welcome-file>   
  72.         <welcome-file>index.htm</welcome-file>   
  73.     </welcome-file-list>   
  74.   
  75.     <!-- for WebSphere deployment, please uncomment -->   
  76.     <!--   
  77.     <resource-ref>   
  78.         <description>Flex Messaging WorkManager</description>   
  79.         <res-ref-name>wm/MessagingWorkManager</res-ref-name>   
  80.         <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>   
  81.         <res-auth>Container</res-auth>   
  82.         <res-sharing-scope>Shareable</res-sharing-scope>   
  83.     </resource-ref>   
  84.     -->   
  85.   
  86. </web-app>  
<?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,代码如下
Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"    
  3.                xmlns:s="library://ns.adobe.com/flex/spark"    
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">   
  5.     <s:layout>   
  6.         <s:BasicLayout/>   
  7.     </s:layout>   
  8.   
  9.   
  10.   
  11.   
  12.     <fx:Script>   
  13.         <![CDATA[   
  14.             import mx.controls.Alert;   
  15.             import mx.rpc.events.ResultEvent;   
  16.                
  17.             protected function button1_clickHandler(event:MouseEvent):void  
  18.             {   
  19.                 service.firstApp("Flex");   
  20.             }   
  21.             protected function remote_resultHandler(event:ResultEvent):void  
  22.             {   
  23.                 Alert.show(String(event.result));   
  24.             }   
  25.         ]]>   
  26.     </fx:Script>   
  27.   
  28.     <fx:Declarations>   
  29.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->   
  30.         <s:RemoteObject id="service" destination="myService" result="remote_resultHandler(event)" endpoint="messagebroker/amf"/>   
  31.     </fx:Declarations>   
  32.     <s:Button x="72" y="101" label="按钮" click="button1_clickHandler(event)"/>   
  33. </s:Application>  
<?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
Java代码 复制代码
  1. package com.test.service;   
  2.   
  3. import org.springframework.flex.remoting.RemotingDestination;   
  4. import org.springframework.flex.remoting.RemotingInclude;   
  5. import org.springframework.stereotype.Service;   
  6.   
  7.   
  8. @Service("myService")   
  9. @RemotingDestination(channels = { "my-amf""my-secure-amf" })   
  10. public class MyService {   
  11.        
  12.     @RemotingInclude  
  13.     public String firstApp(String info){   
  14.            
  15.         System.out.println(info);//从FLEX端获取的信息   
  16.            
  17.         return "Hello World "+info;//返回给FLEX   
  18.            
  19.     }   
  20.   
  21. }  
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
  
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:security="http://www.springframework.org/schema/security"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="   
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd   
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">   
  9.   
  10.     <context:component-scan base-package="com.test.service" />   
  11.     <context:annotation-config />   
  12. </beans>  
<?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
  应该前后台多有东西输出了吧!祝大家好运!
 

你可能感兴趣的:(spring,Flex,创建项目,blazeds,注解方式)