public ObjectFactory create(Context xworkContext) throws Exception { SpringObjectFactory f = new SpringObjectFactory(); xworkContext.getContainer().inject(f); f.setApplicationContext(_applicationContext); f.setAutowireStrategy(_autoWireStrategy); return f; } });
public Object autoWireBean(Object bean) { return autoWireBean(bean, autoWiringFactory); }
/** * Looks up beans using Spring's application context before falling back to the method defined in the {@link * ObjectFactory}. * * @param beanName The name of the bean to look up in the application context * @param extraContext * @return A bean from Spring or the result of calling the overridden * method. * @throws Exception */ @Override public Object buildBean(String beanName, Map<String, Object> extraContext, boolean injectInternal) throws Exception { Object o; if (appContext.containsBean(beanName)) { o = appContext.getBean(beanName); } else { Class beanClazz = getClassInstance(beanName); o = buildBean(beanClazz, extraContext); } if (injectInternal) { injectInternalBeans(o); } return o; }
/** * @param bean * @param autoWiringFactory */ public Object autoWireBean(Object bean, AutowireCapableBeanFactory autoWiringFactory) { if (autoWiringFactory != null) { autoWiringFactory.autowireBeanProperties(bean, autowireStrategy, false); } injectApplicationContext(bean); injectInternalBeans(bean); return bean; }
factory = new SpringObjectFactory(); factory.setApplicationContext(getApplicationContext()); if (autowireStrategy != null) { factory.setAutowireStrategy(autowireStrategy.intValue()); factory.autoWireBean(bean);
injectApplicationContext(bean); return injectInternalBeans(bean); } else if (enableAopSupport) { bean = autoWiringFactory.createBean(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); bean = autoWireBean(bean, autoWiringFactory); bean = autoWiringFactory.initializeBean(bean, bean.getClass().getName()); return bean; bean = autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); bean = autoWiringFactory.initializeBean(bean, bean.getClass().getName()); return autoWireBean(bean, autoWiringFactory); LOG.error("Error building bean", e); return autoWireBean(super.buildBean(clazz, extraContext), autoWiringFactory);
@Inject(value="applicationContextPath",required=false) public void setApplicationContextPath(String ctx) { if (ctx != null) { setApplicationContext(new ClassPathXmlApplicationContext(ctx)); } }
/** * Set the Spring ApplicationContext that should be used to look beans up with. * * @param appContext The Spring ApplicationContext that should be used to look beans up with. */ public void setApplicationContext(ApplicationContext appContext) throws BeansException { this.appContext = appContext; autoWiringFactory = findAutoWiringBeanFactory(this.appContext); }
LOG.debug("Returning autowired instance created by default ObjectFactory"); return autoWireBean(super.buildBean(beanName, extraContext), autoWiringFactory);
/** * If the given context is assignable to AutowireCapbleBeanFactory or contains a parent or a factory that is, then * set the autoWiringFactory appropriately. * * @param context */ protected AutowireCapableBeanFactory findAutoWiringBeanFactory(ApplicationContext context) { if (context instanceof AutowireCapableBeanFactory) { // Check the context return (AutowireCapableBeanFactory) context; } else if (context instanceof ConfigurableApplicationContext) { // Try and grab the beanFactory return ((ConfigurableApplicationContext) context).getBeanFactory(); } else if (context.getParent() != null) { // And if all else fails, try again with the parent context return findAutoWiringBeanFactory(context.getParent()); } return null; }