spring集成blazeds,使用@RemoteDestination实现远程服务

 

web.xml

添加以下

view plain copy to clipboard print ?
  1. <servlet>  
  2.         <servlet-name>spring-flex</servlet-name>  
  3.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.         <init-param>  
  5.             <param-name>contextConfigLocation</param-name>  
  6.             <param-value>classpath:spring-flex.xml</param-value>  
  7.         </init-param>  
  8.     </servlet>  
<servlet> <servlet-name>spring-flex</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-flex.xml</param-value> </init-param> </servlet>这里的init-param必须写,不写默认会报找不到xx-servlet.xml文件。
view plain copy to clipboard print ?
  1. <servlet-mapping>  
  2.         <servlet-name>spring-flex</servlet-name>  
  3.         <url-pattern>/messagebroker/*</url-pattern>  
  4.     </servlet-mapping>  
<servlet-mapping> <servlet-name>spring-flex</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>这里是接受了blazeds的访问,我是采用了异步加载即单独的文件的方式,如果将spring-flex.xml集成进applicationContext.xml的话,之后使用blazeds会再将此文件加载一遍,就造成了不必要的资源浪费了。

第一次访问时会动态加载spring-flex.xml文件中的内容,来完成初始化,所以第一次会比较慢。

下面是spring-flex.xml

view plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:flex="http://www.springframework.org/schema/flex"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.                            http://www.springframework.org/schema/flex  
  9.                            http://www.springframework.org/schema/flex/spring-flex-1.0.xsd  
  10.                            http://www.springframework.org/schema/context   
  11.                             http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  12.                            ">  
  13. <!--    <bean id="_messageBroker" class="org.springframework.flex.messaging.MessageBrokerFactoryBean" >  -->  
  14. <!--     <property name="servicesConfigPath" value="classpath:services-config.xml" />  -->  
  15. <!-- </bean> -->  
  16.     <context:component-scan base-package="com.wynlink.*" />  
  17.     <!-- flex -->  
  18.     <!-- 为了把请求路由给 MessageBroker,添加以下 tag;;;例如BlazeDS XML配置文件默认位置为:/ WEB-INF/flex/services-config.xml。但可以使用services-config-path属性重新配置路径。classpath在maven项目中默认为src/main/resources路径下。 -->  
  19.     <!-- 如果路径发送改变用这个<flex:message-broker services-config-path="classpath*:services-config.xml"/> -->  
  20.     <flex:message-broker services-config-path="classpath*:services-config.xml">  
  21.     </flex:message-broker>  
  22. </beans>  
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flex="http://www.springframework.org/schema/flex" 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/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!-- <bean id="_messageBroker" class="org.springframework.flex.messaging.MessageBrokerFactoryBean" > --> <!-- <property name="servicesConfigPath" value="classpath:services-config.xml" /> --> <!-- </bean> --> <context:component-scan base-package="com.wynlink.*" /> <!-- flex --> <!-- 为了把请求路由给 MessageBroker,添加以下 tag;;;例如BlazeDS XML配置文件默认位置为:/ WEB-INF/flex/services-config.xml。但可以使用services-config-path属性重新配置路径。classpath在maven项目中默认为src/main/resources路径下。 --> <!-- 如果路径发送改变用这个<flex:message-broker services-config-path="classpath*:services-config.xml"/> --> <flex:message-broker services-config-path="classpath*:services-config.xml"> </flex:message-broker> </beans> 之前的配置文件中并没有写<context:component-scan base-package="com.wynlink.*" />,但是一样正常,只是最近突然发现spring-flex.xml在加载时不会帮我自动初始化使用@RemoteDestination注解的类,加上这个后就没问题了。

下面是类得部分代码

view plain copy to clipboard print ?
  1. @Service("loginService")  
  2. @RemotingDestination(channels={"my-amf"}, value="loginService")  
  3. @Transactional(readOnly=true,rollbackFor=Exception.class)  
  4. public class LoginService {  
  5.     @Autowired  
  6.     private LoginDAO dao;  
  7.       
  8.     public User login(User user) {  
  9.         try {  
  10.             return dao.login(user.getUsername(), user.getPassword());  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.             return null;  
  14.         }  
  15.     }  
  16.       
  17.     public String getLoginUser() {  
  18.         User user = (User) FlexContext.getFlexSession().getAttribute("user");  
  19.         if(null == user)  
  20.             return null;  
  21.         return user.getUsername() + "|" + user.getRole();  
  22.     }  
  23. }  
@Service("loginService") @RemotingDestination(channels={"my-amf"}, value="loginService") @Transactional(readOnly=true,rollbackFor=Exception.class) public class LoginService { @Autowired private LoginDAO dao; public User login(User user) { try { return dao.login(user.getUsername(), user.getPassword()); } catch (Exception e) { e.printStackTrace(); return null; } } public String getLoginUser() { User user = (User) FlexContext.getFlexSession().getAttribute("user"); if(null == user) return null; return user.getUsername() + "|" + user.getRole(); } } @service和@transaction是spring通过注解自动加载类和事务控制,跟本文无太大关系


你可能感兴趣的:(spring集成blazeds,使用@RemoteDestination实现远程服务)