springside4(spring4.2.5) 整合 flex(blazeds)
1 pom.xml 添加依賴包
<!-- https://mvnrepository.com/artifact/org.springframework.flex/spring-flex-core --> < dependency > < groupId > org.springframework.flex </ groupId > < artifactId > spring-flex-core </ artifactId > < version > 1.0.3.RELEASE </ version > </ dependency > < dependency > < groupId > commons-httpclient </ groupId > < artifactId > commons-httpclient </ artifactId > < version > 3.0.1 </ version > </ dependency > < dependency > < groupId > com.adobe.blazeds </ groupId > < artifactId > blazeds-proxy </ artifactId > < version > 4.0.0.14931 </ version > </ dependency > < dependency > < groupId > com.adobe.blazeds </ groupId > < artifactId > blazeds-core </ artifactId > < version > 4.0.0.14931 </ version > </ dependency > < dependency > < groupId > com.adobe.blazeds </ groupId > < artifactId > blazeds-remoting </ artifactId > < version > 4.0.0.14931 </ version > </ dependency > < dependency > < groupId > com.adobe.blazeds </ groupId > < artifactId > blazeds-common </ artifactId > < version > 4.0.0.14931 </ version > </ dependency > < dependency > < groupId > com.adobe.blazeds </ groupId > < artifactId > blazeds-opt </ artifactId > < version > 4.0.0.14931 </ version > </ dependency > < dependency > < groupId > com.adobe.blazeds </ groupId > < artifactId > blazeds-rds-server </ artifactId > < version > 4.0.0.14931 </ version > </ dependency >
如果maven中央中央庫無法下載下傳可以到這裡下載下傳本地jar包然後手動打包到maven中中央庫。
http://download.csdn.net/detail/muzili90s/9627781
2 拷貝 blazeds /flex 下所有的檔案到項目到項目web-inf/flex
可以在這裡下載下傳到flex配置檔案
http://download.csdn.net/detail/muzili90s/9627781
messaging-config.xml proxy-config.xml remoting-config.xml services-config.xml version.properties
3 修改 web.xml
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version = "2.5" > < display-name > myspring </ display-name > < context-param > < param-name > contextConfigLocation </ param-name > < param-value > classpath*:/applicationContext.xml, classpath*:/applicationContext-shiro.xml </ param-value > </ context-param > < context-param > < param-name > spring.profiles.default </ param-name > < param-value > production </ param-value > </ context-param > < context-param > < param-name > flex.class.path </ param-name > < param-value > /WEB-INF/flex/hotfixes,/WEB-INF/flex/jars </ param-value > </ context-param > < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > < listener > < listener-class > flex.messaging.HttpFlexSession </ listener-class > </ listener > < filter > < filter-name > encodingFilter </ filter-name > < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class > < init-param > < param-name > encoding </ param-name > < param-value > UTF-8 </ param-value > </ init-param > < init-param > < param-name > forceEncoding </ param-name > < param-value > true </ param-value > </ init-param > </ filter > < filter-mapping > < filter-name > encodingFilter </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < filter > < filter-name > openEntityManagerInViewFilter </ filter-name > < filter-class > org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter </ filter-class > </ filter > < filter-mapping > < filter-name > openEntityManagerInViewFilter </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < filter > < filter-name > shiroFilter </ filter-name > < filter-class > org.springframework.web.filter.DelegatingFilterProxy </ filter-class > < init-param > < param-name > targetFilterLifecycle </ param-name > < param-value > true </ param-value > </ init-param > </ filter > < filter-mapping > < filter-name > shiroFilter </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < filter > < filter-name > sitemeshFilter </ filter-name > < filter-class > com.opensymphony.sitemesh.webapp.SiteMeshFilter </ filter-class > </ filter > < filter-mapping > < filter-name > sitemeshFilter </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < servlet > < servlet-name > springServlet </ servlet-name > < servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class > < init-param > < param-name > contextConfigLocation </ param-name > < param-value > /WEB-INF/spring-mvc.xml </ param-value > </ init-param > < load-on-startup > 1 </ load-on-startup > </ servlet > < servlet-mapping > < servlet-name > springServlet </ servlet-name > < url-pattern > / </ url-pattern > </ servlet-mapping > <!-- MessageBroker Servlet --> < servlet > < servlet-name > MessageBrokerServlet </ servlet-name > < servlet-class > flex.messaging.MessageBrokerServlet </ servlet-class > < init-param > < param-name > services.configuration.file </ param-name > < param-value > /WEB-INF/flex/services-config.xml </ param-value > </ init-param > < init-param > < param-name > flex.write.path </ param-name > < param-value > /WEB-INF/flex </ param-value > </ init-param > < load-on-startup > 1 </ load-on-startup > </ servlet > < servlet-mapping > < servlet-name > MessageBrokerServlet </ servlet-name > < url-pattern > /messagebroker/* </ url-pattern > </ servlet-mapping > < session-config > < session-timeout > 20 </ session-timeout > </ session-config > < error-page > < exception-type > java.lang.Throwable </ exception-type > < location > /WEB-INF/views/error/500.jsp </ location > </ error-page > < error-page > < error-code > 500 </ error-code > < location > /WEB-INF/views/error/500.jsp </ location > </ error-page > < error-page > < error-code > 404 </ error-code > < location > /WEB-INF/views/error/404.jsp </ location > </ error-page > </ web-app >
4 測試
package com.bceapp.springbbs.myspring.service.test; import org.springframework.stereotype.Component; @Component public class FlexServiceTest { private static int num = 0; public String getHello() { System. out .println( "In Here" ); num ++; return "You are In Java Service" + num ; } public String showInputVal(String input ) { System. out .println( "Input Here" ); num ++; return "You Input Value: " + input + "; Num:" + num ; } }
<?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 " > <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; protected function button1_clickHandler(event:MouseEvent): void { this .remoteObject.getName(); } protected function remoteObject_faultHandler(event:FaultEvent): void { Alert.show(event.fault.toString()); } protected function getNameResultsHandler(event:ResultEvent): void { Alert.show(event.result.toString()); } protected function servFlex_resultHandler(event:ResultEvent): void { // TODO Auto-generated method stub // Alert.show("OK: "+event.result, "Msg"); } protected function servFlex_faultHandler(event:FaultEvent): void { // TODO Auto-generated method stub // Alert.show("OK: "+event.fault, "Msg"); } protected function button2_clickHandler(event:MouseEvent): void { this .servFlex.getHello(); } protected function getNameResultsHandler2(event:ResultEvent): void { Alert.show(event.result.toString()); } ]]> </fx:Script> <fx:Declarations> <!-- --> <s:RemoteObject id=" servFlex " destination=" javaService " result="servFlex_resultHandler(event)" fault="servFlex_faultHandler(event)" > <s:method name=" getHello " result="getNameResultsHandler2(event)" /> </s:RemoteObject> </fx:Declarations> <s:Button x=" 51 " y=" 35 " label=" 1 " click="button1_clickHandler(event)" /> <s:Button x=" 151 " y=" 35 " label=" 2 " click="button2_clickHandler(event)" /> </s:Application>
直接使用spring攔截flex請求有問題,spring4.2.5和 blazeds 有沖突
如果有牛人把這個問題解決了 請告訴我。不勝感激
忘了最重要的一步了
添加server remote
<?xml version="1.0" encoding="UTF-8"?> <service id="remoting-service" class="flex.messaging.services.RemotingService">
<adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> </adapters>
<default-channels> <channel ref="my-amf"/> </default-channels>
<destination id="javaService"> <properties> <source>com.bceapp.springbbs.myspring.service.test.FlexServiceTest</source> </properties> </destination>
</service>
--------------------------------------------------------------------------------------------------------------
flex 調用 spring
6 增加一個spring的工廠類
package com.bceapp.springbbs.myspring.web.flex; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import flex.messaging.FactoryInstance; import flex.messaging.FlexFactory; import flex.messaging.config.ConfigMap; import flex.messaging.services.ServiceException; public class SpringFactory implements FlexFactory { private static final String SOURCE = "source" ; public void initialize(String id , ConfigMap configMap ) {} public FactoryInstance createFactoryInstance(String id , ConfigMap properties ) { SpringFactoryInstance instance = new SpringFactoryInstance( this , id , properties ); instance .setSource( properties .getPropertyAsString( SOURCE , instance .getId())); return instance ; } // end method createFactoryInstance() public Object lookup(FactoryInstance inst ) { SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst ; return factoryInstance .lookup(); } static class SpringFactoryInstance extends FactoryInstance { SpringFactoryInstance(SpringFactory factory , String id , ConfigMap properties ) { super ( factory , id , properties ); } public String toString() { return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope(); } public Object lookup() { ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext()); String beanName = getSource(); try { return appContext .getBean( beanName ); } catch (NoSuchBeanDefinitionException nexc ) { ServiceException e = new ServiceException(); String msg = "Spring service named '" + beanName + "' does not exist." ; e .setMessage( msg ); e .setRootCause( nexc ); e .setDetails( msg ); e .setCode( "Server.Processing" ); throw e ; } catch (BeansException bexc ) { ServiceException e = new ServiceException(); String msg = "Unable to create Spring service named '" + beanName + "' " ; e .setMessage( msg ); e .setRootCause( bexc ); e .setDetails( msg ); e .setCode( "Server.Processing" ); throw e ; } } } }
7 在services-config中增加一個spring工廠
< factories > < factory id = "springFactory" class = "com.bceapp.springbbs.myspring.web.flex.SpringFactory" /> </ factories > 8 在remoting-config中增加一個destination
< destination id = "javaService" > < properties > < factory > springFactory </ factory > <!-- 這裡隻定義名字 會在spring容器裡找到bean 注解方式為類名第一個字母小寫-->
< source > testUserMybatisService </ source > </ properties > </ destination >
至此flex整合spring4完成。