1<!-- Spring configuration file (Not needed if you don't use Spring) --> 2<context-param> 3<param-name>contextConfigLocation</param-name> 4<param-value>/WEB-INF/applicationContext.xml</param-value> 5</context-param> 6 7<!-- Spring ContextLoaderListener (Not needed if you don't use Spring) --> 8<listener> 9<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 10</listener> 11 12
1package com.samples.factories; 2 3import org.springframework.context.ApplicationContext; 4import org.springframework.web.context.support.WebApplicationContextUtils; 5import org.springframework.beans.BeansException; 6import org.springframework.beans.factory.NoSuchBeanDefinitionException; 7 8import flex.messaging.FactoryInstance; 9import flex.messaging.FlexFactory; 10import flex.messaging.config.ConfigMap; 11import flex.messaging.services.ServiceException; 12 13/** *//** 14 * This interface is implemented by factory components which provide 15 * instances to the flex messaging framework. To configure flex data services 16 * to use this factory, add the following lines to your services-config.xml 17 * file (located in the WEB-INF/flex directory of your web application). 18 * 19 * <factories> 20 * <factory id="spring" class="flex.samples.factories.SpringFactory" /> 21 * </factories> 22 * 23 * You also must configure the web application to use spring and must copy the spring.jar 24 * file into your WEB-INF/lib directory. To configure your app server to use spring, 25 * you add the following lines to your WEB-INF/web.xml file: 26 * 27 * <context-param> 28 * <param-name>contextConfigLocation</param-name> 29 * <param-value>/WEB-INF/applicationContext.xml</param-value> 30 * </context-param> 31 * 32 * <listener> 33 * <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 34 * </listener> 35 * 36 * Then you put your spring bean configuration in WEB-INF/applicationContext.xml (as per the 37 * line above). For example: 38 * 39 * <?xml version="1.0" encoding="UTF-8"?> 40 * <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 41 * 42 * <beans> 43 * <bean name="weatherBean" class="dev.weather.WeatherService" singleton="true"/> 44 * </beans> 45 * 46 * Now you are ready to define a destination in flex that maps to this existing service. 47 * To do this you'd add this to your WEB-INF/flex/remoting-config.xml: 48 * 49 * <destination id="WeatherService"> 50 * <properties> 51 * <factory>spring</factory> 52 * <source>weatherBean</source> 53 * </properties> 54 * </destination> 55 * 56 * @author Jeff Vroom 57*/ 58publicclass SpringFactory implements FlexFactory 59{ 60privatestaticfinal String SOURCE ="source"; 61 62/** *//** 63 * This method can be used to initialize the factory itself. It is called with configuration 64 * parameters from the factory tag which defines the id of the factory. 65*/ 66publicvoid initialize(String id, ConfigMap configMap) {} 67 68/** *//** 69 * This method is called when we initialize the definition of an instance 70 * which will be looked up by this factory. It should validate that 71 * the properties supplied are valid to define an instance. 72 * Any valid properties used for this configuration must be accessed to 73 * avoid warnings about unused configuration elements. If your factory 74 * is only used for application scoped components, this method can simply 75 * return a factory instance which delegates the creation of the component 76 * to the FactoryInstance's lookup method. 77*/ 78public FactoryInstance createFactoryInstance(String id, ConfigMap properties) 79{ 80 SpringFactoryInstance instance =new SpringFactoryInstance(this, id, properties); 81 instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId())); 82 System.out.println("SpringSpringSpring"+ instance.toString()); 83return instance; 84 }// end method createFactoryInstance() 85 86/** *//** 87 * Returns the instance specified by the source 88 * and properties arguments. For the factory, this may mean 89 * constructing a new instance, optionally registering it in some other 90 * name space such as the session or JNDI, and then returning it 91 * or it may mean creating a new instance and returning it. 92 * This method is called for each request to operate on the 93 * given item by the system so it should be relatively efficient. 94 * <p> 95 * If your factory does not support the scope property, it 96 * report an error if scope is supplied in the properties 97 * for this instance. 98*/ 99public Object lookup(FactoryInstance inst) 100{ 101 SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst; 102return factoryInstance.lookup(); 103 } 104 105 106staticclass SpringFactoryInstance extends FactoryInstance 107{ 108 SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties) 109{ 110super(factory, id, properties); 111 } 112 113 114public String toString() 115{ 116return"SpringFactory instance for id="+ getId() +" source="+ getSource() +" scope="+ getScope(); 117 } 118 119public Object lookup() 120{ 121 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext()); 122 String beanName = getSource(); 123 124try 125{ 126return appContext.getBean(beanName); 127 } 128catch (NoSuchBeanDefinitionException nexc) 129{ 130 ServiceException e =new ServiceException(); 131 String msg ="Spring service named '"+ beanName +"' does not exist."; 132 e.setMessage(msg); 133 e.setRootCause(nexc); 134 e.setDetails(msg); 135 e.setCode("Server.Processing"); 136throw e; 137 } 138catch (BeansException bexc) 139{ 140 ServiceException e =new ServiceException(); 141 String msg ="Unable to create Spring service named '"+ beanName +"' "; 142 e.setMessage(msg); 143 e.setRootCause(bexc); 144 e.setDetails(msg); 145 e.setCode("Server.Processing"); 146throw e; 147 } 148 } 149 150 } 151 152} 153