--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+ <beans>
+
+ <bean id="test"
+ class="org.springframework.context.support.ClassPathXmlApplicationContext">
+ <constructor-arg>
+ <list>
+ <value>org/wamblee/general/spring1.xml</value>
+ </list>
+ </constructor-arg>
+ </bean>
+
+ </beans>
\ No newline at end of file
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans>
+
+ <bean id="java.lang.String"
+ class="java.lang.String">
+ <constructor-arg><value>hello</value></constructor-arg>
+ </bean>
+
+</beans>
\ No newline at end of file
import org.wamblee.io.InputResource;
/**
- * The standard means to obtain the bean factory.
+ * The standard means to obtain the bean factory. This works by reading a
+ * property {@value #BEAN_FACTORY_CLASS} from a property file named
+ * {@value #BEAN_KERNEL_PROP_FILE} from the class path. This property identifies
+ * the bean factory implementation to use. The configured bean factory must have
+ * a no-arg constructor.
*/
public final class BeanKernel {
* Cached bean factory.
*/
private static BeanFactory BEAN_FACTORY;
-
+
/**
* Disabled constructor.
- *
+ *
*/
- private BeanKernel() {
+ private BeanKernel() {
// Empty
}
}
/**
- * Gets the bean factory.
- * @return Bean factory.
+ * Gets the bean factory.
+ *
+ * @return Bean factory.
*/
public static BeanFactory getBeanFactory() {
synchronized (BeanFactory.class) {
*
* @return Bean factory.
*/
- private static BeanFactory lookupBeanFactory() {
+ static BeanFactory lookupBeanFactory() {
InputResource resource = new ClassPathResource(BEAN_KERNEL_PROP_FILE);
InputStream is;
try {
}
try {
Properties props = new Properties();
- props.load(resource.getInputStream());
+ props.load(is);
String className = props.getProperty(BEAN_FACTORY_CLASS);
Class beanFactory = Class.forName(className);
return (BeanFactory) beanFactory.newInstance();
+
+
+
/*
* Copyright 2005 the original author or authors.
*
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- */
+ */
package org.wamblee.general;
import org.springframework.beans.BeansException;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
/**
- * Bean factory which uses Spring.
+ * Bean factory which uses Spring. This bean factory cannot be configured
+ * directly in the {@link org.wamblee.general.BeanKernel} because it does not
+ * provide a default no-arg constructor. Therefore, it must be delegated to or
+ * it must tbe subclassed to provide a default constructor.
*/
public class SpringBeanFactory implements BeanFactory {
-
- private String _factoryName;
-
+
+ private BeanFactoryReference _factoryReference;
+
/**
- * Constructs the bean factory.
- * @param aFactoryName Spring bean factory to use.
+ * Constructs the bean factory.
+ *
+ * @param aSelector
+ * Selector to find the appropriate bean ref context.
+ * @param aFactoryName
+ * Spring bean factory to use.
*/
- public SpringBeanFactory(String aFactoryName) {
- _factoryName = aFactoryName;
+ public SpringBeanFactory(String aSelector, String aFactoryName) {
+ BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator
+ .getInstance(aSelector);
+ _factoryReference = locator.useBeanFactory(aFactoryName);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.general.BeanFactory#find(java.lang.String)
*/
public Object find(String aId) {
- return find(aId, Object.class);
+ return find(aId, Object.class);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.general.BeanFactory#find(java.lang.Class)
*/
public <T> T find(Class<T> aClass) {
return find(aClass.getName(), aClass);
}
- /* (non-Javadoc)
- * @see org.wamblee.general.BeanFactory#find(java.lang.String, java.lang.Class)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.general.BeanFactory#find(java.lang.String,
+ * java.lang.Class)
*/
public <T> T find(String aId, Class<T> aClass) {
- BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance();
- BeanFactoryReference beanFactory = locator.useBeanFactory(_factoryName);
-
try {
- Object obj = beanFactory.getFactory().getBean(aId, aClass);
- assert obj != null;
- return aClass.cast(obj);
- } catch (BeansException e) {
+ Object obj = _factoryReference.getFactory().getBean(aId, aClass);
+ assert obj != null;
+ return aClass.cast(obj);
+ } catch (BeansException e) {
throw new BeanFactoryException(e.getMessage(), e);
}
}
--- /dev/null
+/*
+ * Copyright 2005 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wamblee.general;
+
+import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests the spring bean factory.
+ */
+public class SpringBeanFactoryTest extends TestCase {
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ }
+
+ public void testExistingBeanRefContext() {
+ SpringBeanFactory factory = new SpringBeanFactory("org/wamblee/general/beanRefContext.xml", "test");
+
+ String value1 = factory.find(String.class);
+ assertEquals("hello", value1);
+ String value2 = (String)factory.find("java.lang.String");
+ assertEquals("hello", value2);
+ String value3 = factory.find("java.lang.String", String.class);
+ assertEquals("hello", value3);
+ }
+}