Java代码 复制代码 收藏代码

  1.   
  2. package com.opensymphony.xwork2.spring;   
  3.   
  4. import com.opensymphony.xwork2.ObjectFactory;   
  5. import com.opensymphony.xwork2.inject.Inject;   
  6. import com.opensymphony.xwork2.util.logging.Logger;   
  7. import com.opensymphony.xwork2.util.logging.LoggerFactory;   
  8. import org.springframework.beans.BeansException;   
  9. import org.springframework.beans.factory.UnsatisfiedDependencyException;   
  10. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;   
  11. import org.springframework.context.ApplicationContext;   
  12. import org.springframework.context.ApplicationContextAware;   
  13. import org.springframework.context.ConfigurableApplicationContext;   
  14. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  15.   
  16. import java.util.HashMap;   
  17. import java.util.Map;   
  18.   
  19. /**  
  20.  * Simple implementation of the ObjectFactory that makes use of Spring's application context if one has been configured,  
  21.  * before falling back on the default mechanism of instantiating a new class using the class name. <p/> In order to use  
  22.  * this class in your application, you will need to instantiate a copy of this class and set it as XWork's ObjectFactory  
  23.  * before the xwork.xml file is parsed. In a servlet environment, this could be done using a ServletContextListener.  
  24.  *  
  25.  * @author Simon Stewart (sms@lateral.net)  
  26.  */  
  27. public class SpringObjectFactory extends ObjectFactory implements ApplicationContextAware {   
  28.     private static final Logger LOG = LoggerFactory.getLogger(SpringObjectFactory.class);   
  29.   
  30.     protected ApplicationContext appContext;   
  31.     protected AutowireCapableBeanFactory autoWiringFactory;   
  32.     protected int autowireStrategy = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;   
  33.     private final Map<String, Object> classes = new HashMap<String, Object>();   
  34.     private boolean useClassCache = true;   
  35.     private boolean alwaysRespectAutowireStrategy = false;   
  36.   
  37.       
  38.     public Object buildBean(String beanName, Map<String, Object> extraContext, boolean injectInternal) throws Exception {   
  39.         Object o;   
  40.         //检查Spring容器中是否有该bean,如果有就获取该bean,   
  41.         //appContext.getBean()方法会负责 创建或取出该bean   
  42.         if (appContext.containsBean(beanName)) {   
  43.             o = appContext.getBean(beanName);   
  44.         } else {//如果spring容器中没有,就自己创建   
  45.             Class beanClazz = getClassInstance(beanName);   
  46.             o = buildBean(beanClazz, extraContext);   
  47.         }   
  48.         //如果该bean需要注入,就通过   
  49.         //com.opensymphony.xwork2.inject.Container.inject()注入该bean   
  50.         if (injectInternal) {   
  51.             injectInternalBeans(o);   
  52.         }   
  53.         return o;   
  54.     }   
  55.