要和Spring整合所以先简单讲下Spring(如果要对Spring没有接触过读者也没关系按照讲内容也能做但是这样没啥实际意义所以要对本讲要有所收获必须对Spring有定理解)
首先下载Spring作者选择Spring版本Spring Framework2.0.8可以在官网
http://www.springsource.org/下载也可以去网络搜索都可以
图片看不清楚?请点击这里查看原图(大图)
Spring对于J2EE来说读者应该都非常熟悉Spring以其轻量级应用迅速普及
对于Spring都需要了解两个重要概念:
1、控制反转(IoC=Inversion of Control)依赖注入(DI=Dependency Injection);
通俗点来讲比如作者在工作中经常使用硬件设备:Dell620笔记本电脑、USB活动硬盘、aigo移动U盘大家很熟悉吧
这 3个设备都支持USB接口需要将数据复制到外边存储设备时根据实际情况决定保存在U盘还是活动硬盘
相信这种操作大家司空见惯而这也正是所谓依赖注入个典型例子
笔记本电脑和外围存储通过笔记本USB接口相连对于笔记本而言只是将用户指定数据发送到USB接口而这些数据何去何从则由当前USB设备决定
2、面向切面编程(AOP=Aspect Oriented Programming);
这个大家可能觉得很抽象其实现在大家应该都接触过作者举两个例子:个是WebWork框架Intecepor就是AOP应用;再个大家更熟悉了J2EE系统Log4j日志系统对于读者做系统来说就是个AOP
切面切面就是个截面所做不对现有系统进行破坏但是随时可以打开关闭这么理解就是很通俗通俗理解了
这儿就简单提及下主要还是讲Flex和Spring整合读者就步步来做吧:)
呵呵大家觉得Spring概念容易理解了吧:)
Flex和Spring整合:
1、将Springjar包拷贝到WEB-INFlib下;(这个是必须否则如何整合Spring)
2、在web.xml中注册Spring如下配置:
<!-- Spring configuration file (Not needed you don't use Spring) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Spring ContextLoaderListener (Not needed you don't use Spring) -->
<listener>
<listener->org.springframework.web.context.ContextLoaderListener</listener->
</listener>
3、增加SpringFactory.java代码如下:
package com.samples.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;
/**
* This erface is implemented by factory components which provide
* instances to the flex messaging framework. To configure flex data services
* to use this factory, add the following lines to your services-config.xml
* file (located in the WEB-INF/flex directory of your web application).
*
* <factories>
* <factory id="spring" ="flex.samples.factories.SpringFactory" />
* </factories>
*
* You also must configure the web application to use spring and must copy the spring.jar
* file o your WEB-INF/lib directory. To configure your app server to use spring,
* you add the following lines to your WEB-INF/web.xml file:
*
* <context-param>
* <param-name>contextConfigLocation</param-name>
* <param-value>/WEB-INF/applicationContext.xml</param-value>
* </context-param>
*
* <listener>
* <listener->org.springframework.web.context.ContextLoaderListener</listener->
* </listener>
*
* Then you put your spring bean configuration in WEB-INF/applicationContext.xml (as per the
* line above). For example:
*
* <?xml version="1.0" encoding="UTF-8"?>
* <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
*
* <beans>
* <bean name="weatherBean" ="dev.weather.WeatherService" singleton="true"/>
* </beans>
*
* Now you are ready to a destination in flex that maps to this existing service.
* To do this you'd add this to your WEB-INF/flex/remoting-config.xml:
*
* <destination id="WeatherService">
* <properties>
* <factory>spring</factory>
* <source>weatherBean</source>
* </properties>
* </destination>
*
* @author Jeff Vroom
*/
public SpringFactory implements FlexFactory
{
private 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 s 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 an instance.
* Any valid properties used for this configuration must be accessed to
* avoid warnings about unused configuration elements. If your factory
* is _disibledevent=> instance.Source(properties.getPropertyAsString(SOURCE, instance.getId));
.out.prln("SpringSpringSpring" + instance.toString);
instance;
} // end method createFactoryInstance
/**
* Returns the instance specied by the source
* and properties arguments. For the factory, this may mean
* constructing a instance, optionally registering it in some other
* name space such as the session or JNDI, and then ing it
* or it may mean creating a instance and ing it.
* This method is called for each request to operate _disibledevent=> factoryInstance.lookup;
}
SpringFactoryInstance extends FactoryInstance
{
SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
{
super(factory, id, properties);
}
public String toString
{
"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
{
appContext.getBean(beanName);
}
catch (NoSuchBeanDefinitionException nexc)
{
ServiceException e = ServiceException;
String msg = "Spring service named '" + beanName + "' does not exist.";
e.Message(msg);
e.RootCause(nexc);
e.Details(msg);
e.Code("Server.Processing");
throw e;
}
catch (BeansException bexc)
{
ServiceException e = ServiceException;
String msg = "Unable to create Spring service named '" + beanName + "' ";
e.Message(msg);
e.RootCause(bexc);
e.Details(msg);
e.Code("Server.Processing");
throw e;
}
}
}
}
4、在services-config.xml中注册SpringFacotry配置如下:
<factories>
<factory id="spring" ="com.samples.factories.SpringFactory"/>
</factories>
5、接下来就要在applicationContext.mxl中注册helloJavaFlexBean配置如下:
<bean id="helloJavaFlexBean" ="com.test.HelloJavaFlex">
</bean>
6、在remoting-config.xml中将SpringBean公开给Flex客户端配置如下:
<destination id="helloJavaFlex">
<properties>
<factory>spring</factory>
<source>helloJavaFlexBean</source>
</properties>
</destination>
其它代码都不用改动运行下和Demo11运行效果样
读者可能会有疑问增加了这么多配置效果还和Demo11样?
1、这样整合对于这种演示小来说确实意义不大但是大型项目这种Spring方式优点就非常明显了和Flex客户端公开是SpringBean声明所以无论底层逻辑甚至文件名称得改动都不会有什么影响;
2、Spring整合进来就可以使用Spring很多特性了呵呵下个DEMO就会使用Spring自带JDBCTemplate访问数据库;
3、Spring整合对于Hibernate整合提供了更大便捷这个也会在后边DEMO中提及
下讲对这这讲Demo做个简单解释
文章来源:http://wangyisong.javaeye.com/blog/418818
最新评论