Flex和spring集成时需要的SpringFactory

阅读更多
  1. package springFactory;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.web.context.support.WebApplicationContextUtils;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  6. import flex.messaging.FactoryInstance;
  7. import flex.messaging.FlexFactory;
  8. import flex.messaging.config.ConfigMap;
  9. import flex.messaging.services.ServiceException;
  10. public class SpringFactory implements FlexFactory
  11. {
  12. private static final String SOURCE = "source";
  13. public void initialize(String id, ConfigMap configMap) {}
  14. public FactoryInstance createFactoryInstance(String id, ConfigMap properties)
  15. {
  16. SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties);
  17. instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId()));
  18. return instance;
  19. } // end method createFactoryInstance()
  20. public Object lookup(FactoryInstance inst)
  21. {
  22. SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst;
  23. return factoryInstance.lookup();
  24. }
  25. static class SpringFactoryInstance extends FactoryInstance
  26. {
  27. SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties)
  28. {
  29. super(factory, id, properties);
  30. }
  31. public String toString()
  32. {
  33. return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope();
  34. }
  35. public Object lookup()
  36. {
  37. ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext());
  38. String beanName = getSource();
  39. try
  40. {
  41. return appContext.getBean(beanName);
  42. }
  43. catch (NoSuchBeanDefinitionException nexc)
  44. {
  45. ServiceException e = new ServiceException();
  46. String msg = "Spring service named '" + beanName + "' does not exist.";
  47. e.setMessage(msg);
  48. e.setRootCause(nexc);
  49. e.setDetails(msg);
  50. e.setCode("Server.Processing");
  51. throw e;
  52. }
  53. catch (BeansException bexc)
  54. {
  55. ServiceException e = new ServiceException();
  56. String msg = "Unable to create Spring service named '" + beanName + "' ";
  57. e.setMessage(msg);
  58. e.setRootCause(bexc);
  59. e.setDetails(msg);
  60. e.setCode("Server.Processing");
  61. throw e;
  62. }
  63. }
  64. }
  65. }

你可能感兴趣的:(SpringFactory,Flex)