Index: SpringObjectFactory.java
===================================================================
RCS file: /cvs/xwork-optional/xwork-spring/src/java/com/opensymphony/xwork/spring/SpringObjectFactory.java,v
retrieving revision 1.3
diff -u -r1.3 SpringObjectFactory.java
--- SpringObjectFactory.java 29 Jun 2004 10:05:24 -0000 1.3
+++ SpringObjectFactory.java 10 Aug 2004 14:51:06 -0000
@@ -4,6 +4,9 @@
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
@@ -14,58 +17,91 @@
import com.opensymphony.xwork.ObjectFactory;
/**
- * Simple implementation of the ObjectFactory that makes use of Spring's application
- * context if one has been configured, before falling back on the default mechanism of
- * instantiating a new class using the class name.
- *
- * In order to use this class in your application, you will need to instantiate a
- * copy of this class and set it as XWork's ObjectFactory before the xwork.xml file
- * is parsed. In a servlet environment, this could be done using a ServletContextListener.
- *
+ * Simple implementation of the ObjectFactory that makes use of Spring's
+ * application context if one has been configured, before falling back on the
+ * default mechanism of instantiating a new class using the class name.
+ *
+ * In order to use this class in your application, you will need to instantiate
+ * a copy of this class and set it as XWork's ObjectFactory before the xwork.xml
+ * file is parsed. In a servlet environment, this could be done using a
+ * ServletContextListener.
+ *
* @author Simon Stewart (
sms@lateral.net)
*/
-public class SpringObjectFactory extends ObjectFactory implements ApplicationContextAware {
- //~ Static fields/initializers /////////////////////////////////////////////
-
- private static final Log log = LogFactory.getLog(SpringObjectFactory.class);
-
- //~ Instance fields ////////////////////////////////////////////////////////
-
- private ApplicationContext appContext;
-
- //~ Methods ////////////////////////////////////////////////////////////////
-
- /**
- * 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;
- }
-
- /**
- * 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
- * @return A bean from Spring or the result of calling the overridden method.
- */
- public Object buildBean(String beanName) throws Exception {
- try {
- return appContext.getBean(beanName);
- } catch (NoSuchBeanDefinitionException e) {
- return super.buildBean(beanName);
- }
- }
-
- /**
- * This method sets the ObjectFactory used by XWork to this object. It's
- * best used as the "init-method" of a Spring bean definition in order to
- * hook Spring and XWork together properly (as an alternative to the
- * {@link SpringObjectFactoryListener}
- */
- public void initObjectFactory() {
- ObjectFactory.setObjectFactory(this);
- }
-}
+public class SpringObjectFactory extends ObjectFactory implements
+ ApplicationContextAware {
+ private Map classMap = new HashMap();
+ //~ Static fields/initializers
+ // /////////////////////////////////////////////
+
+ private static final Log log = LogFactory.getLog(SpringObjectFactory.class);
+
+ //~ Instance fields
+ // ////////////////////////////////////////////////////////
+
+ private ApplicationContext appContext;
+
+ //~ Methods
+ // ////////////////////////////////////////////////////////////////
+
+ /**
+ * 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;
+ }
+
+ /**
+ * 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
+ * @return A bean from Spring or the result of calling the overridden
+ * method.
+ */
+ public Object buildBean(String beanName) throws Exception {
+ try {
+ return appContext.getBean(beanName);
+ } catch (NoSuchBeanDefinitionException e) {
+ return super.buildBean(beanName);
+ }
+ }
+
+ /**
+ * This method sets the ObjectFactory used by XWork to this object. It's
+ * best used as the "init-method" of a Spring bean definition in order to
+ * hook Spring and XWork together properly (as an alternative to the
+ * {@link SpringObjectFactoryListener}
+ */
+ public void initObjectFactory() {
+ ObjectFactory.setObjectFactory(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.opensymphony.xwork.ObjectFactory#getClassInstance(java.lang.String)
+ */
+ public Class getClassInstance(String className) throws ClassNotFoundException {
+ Class c = (Class) classMap.get(className);
+ if(c!=null)
+ return c;
+ if (appContext == null)
+ return super.getClassInstance(className);
+ if (!appContext.containsBeanDefinition(className))
+ return super.getClassInstance(className);
+ Object o = appContext.getBean(className);
+ if (o == null)
+ return super.getClassInstance(className);
+ c = o.getClass();
+ classMap.put(className,c);
+ return c;
+ }
+}
\ No newline at end of file