SSH+Flex3框架集成

最近公司要在网管软件中添加网络拓扑图。经查询使用Flex 做最好。最方便,于是花了一个礼拜学习Flex,终于在SpringGraph的帮助下完成任务。 经过几天努力见网上Flex3集成SSH并不是很多。于是记下步骤,以供参考。我们的项目是SSH框架的。后来要添加Flex3进来。 则Flex3得集成到Spring。先说说原理(原理也是我猜的,待会去查证,不对的话请大家纠正。)。Flex3集成spring 就是在spring的基础上,把Flex3要调用的那个类,用blazeds.war 暴露出来,再用Flex3 到页面直接调用。
步骤: 添加Flex3到j2ee工程我就不讲了,直接讲重点。先到网上下载了一个flex-spring的一个项目,因为这个项目里面本来就写好了Flex和Spring的集成,我们把里面除了spring的jar包都拷到我们的工程lib目录下,也不知道是谁弄的,待会上传到附件里面。下载 blazeds.war 把里面的jar全部拷出来放到项目中。
二。然后把blazeds.war中 WEB-INF/flex/下的全部 xml文件拷出来,放到工程目录WEB-INF/flex/下。
三。添加web.xml 的注册信息。 把要添加的信息列出来。
<!-- Flex code start -->
<!-- flex 工程的默认配置 -->
<context-param>
<param-name>flex.class.path</param-name>
<param-value>
/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars
</param-value>
</context-param>
<!-- Flex code end -->


<!-- Flex code add -->
<!-- Http Flex Session attribute and binding listener support-->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>

<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-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>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Flex code end -->

<!-- Flex code add -->
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<!-- Flex code end -->

把对应的放到响应的位置即可。MessageBrokerServlet 传了一个参数--services-config.xml 。然后在里面添加一个
<!-- 调用SpringFactory注入service-->
<factories>
<factory id="spring" class="com.capinfo.flexssh.factories.SpringFactory" />
</factories>

SpringFactory类的源码为:

package com.capinfo.flexssh.factories;

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";

/**
*
* This method can be used to initialize the factory itself. It is called
* with configuration
*
* parameters from the factory tag which defines the id of the factory.
*/

public void initialize(String id, ConfigMap configMap) {
}

/**
*
* This method is called when we initialize the definition of an instance
* which will be looked
*
* up by this factory. It should validate that the properties supplied are
* valid to define an
*
* instance. Any valid properties used for this configuration must be
* accessed to avoid warnings
*
* about unused configuration elements. If your factory is only used for
* application scoped
*
* components, this method can simply return a factory instance which
* delegates the creation of
*
* the component to the FactoryInstance's lookup method.
*/

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()

/**
*
* Returns the instance specified by the source and properties arguments.
* For the factory, this
*
* may mean constructing a new instance, optionally registering it in some
* other name space such
*
* as the session or JNDI, and then returning it or it may mean creating a
* new instance and
*
* returning it. This method is called for each request to operate on the
* given item by the
*
* system so it should be relatively efficient.
*
* <p>
*
* If your factory does not support the scope property, it report an error
* if scope is supplied
*
* in the properties for this instance.
*/

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;

}

}

}

}


在remoting-config.xml 中添加要调用的信息
<!-- 使用spring配置的service -->

<destination id="ToPoService">
<properties>
<factory>spring</factory><!-- 必须指定spring为factory 使用刚刚自己写的 SpringFactory类 创建一个ToPoService服务-->
<source>ToPoServiceDao</source><!-- applicationContext.xml中配置的业务层 也就是spring配置的Bean id 你要管理的那个类的ID -->
</properties>
</destination>


这里出现的 ToPoServiceDao 在spring注册文件里面 是这样的:
<!-- Flex code addd -->
<bean id="ToPoServiceDao" class="cn.com.superv.emanager.base.dao.hibernate.ToPoServiceDAOHibernate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="vissAmResourcetreeDAO">
<ref bean="VissAmResourcetreeDAO"/>
</property>
<property name="vissAmMonitorAreacodeDAO">
<ref bean="VissAmMonitorAreacodeDAO"/>
</property>
<property name="monitorGroupDAO">
<ref bean="MonitorGroupDAO"/>
</property>
<property name="monitorDAO">
<ref bean="MonitorDAO"/>
</property>
</bean>
<!-- Flex code end -->


在MXML文件中的RemoteObject 中调用该类的 getToPoTree 方法。


<mx:RemoteObject id="firstRO" destination="ToPoService" endpoint="/xxxxxx/messagebroker/amf"> <!-- xxxxxx一般为后台工程名 -->
<mx:method name="getToPoTree" result="handlerResultGetToPoTree(event)" fault="handlerFaultGetToPoTree(event)"/>
</mx:RemoteObject>

完毕。 附件里面有全部的jar包和配置文件信息。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值