001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.config;
018:
019: import org.springframework.beans.BeansException;
020: import org.springframework.beans.factory.BeanFactory;
021: import org.springframework.beans.factory.BeanFactoryAware;
022: import org.springframework.beans.factory.ObjectFactory;
023: import org.springframework.util.Assert;
024:
025: /**
026: * A {@link org.springframework.beans.factory.FactoryBean} implementation that
027: * returns a value which is an {@link org.springframework.beans.factory.ObjectFactory}
028: * that in turn returns a bean sourced from a {@link org.springframework.beans.factory.BeanFactory}.
029: *
030: * <p>As such, this may be used to avoid having a client object directly calling
031: * {@link org.springframework.beans.factory.BeanFactory#getBean(String)} to get
032: * a (typically prototype) bean from a
033: * {@link org.springframework.beans.factory.BeanFactory}, which would be a
034: * violation of the inversion of control principle. Instead, with the use
035: * of this class, the client object can be fed an
036: * {@link org.springframework.beans.factory.ObjectFactory} instance as a
037: * property which directly returns only the one target bean (again, which is
038: * typically a prototype bean).
039: *
040: * <p>A sample config in an XML-based
041: * {@link org.springframework.beans.factory.BeanFactory} might look as follows:
042: *
043: * <pre class="code"><beans>
044: *
045: * <!-- Prototype bean since we have state -->
046: * <bean id="myService" class="a.b.c.MyService" singleton="false"/>
047: *
048: * <bean id="myServiceFactory"
049: * class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
050: * <property name="targetBeanName"><idref local="myService"/></property>
051: * </bean>
052: *
053: * <bean id="clientBean" class="a.b.c.MyClientBean">
054: * <property name="myServiceFactory" ref="myServiceFactory"/>
055: * </bean>
056: *
057: *</beans></pre>
058: *
059: * <p>The attendant <code>MyClientBean</code> class implementation might look
060: * something like this:
061: *
062: * <pre class="code">package a.b.c;
063: *
064: *import org.springframework.beans.factory.ObjectFactory;
065: *
066: *public class MyClientBean {
067: *
068: * private ObjectFactory myServiceFactory;
069: *
070: * public void setMyServiceFactory(ObjectFactory myServiceFactory) {
071: * this.myServiceFactory = myServiceFactory;
072: * }
073: *
074: * public void someBusinessMethod() {
075: * // get a 'fresh', brand new MyService instance
076: * MyService service = this.myServiceFactory.getObject();
077: * // use the service object to effect the business logic...
078: * }
079: *}</pre>
080: *
081: * <p>An alternate approach to this application of an object creational pattern
082: * would be to use the {@link ServiceLocatorFactoryBean}
083: * to source (prototype) beans. The {@link ServiceLocatorFactoryBean} approach
084: * has the advantage of the fact that one doesn't have to depend on any
085: * Spring-specific interface such as {@link org.springframework.beans.factory.ObjectFactory},
086: * but has the disadvantage of requiring runtime class generation. Please do
087: * consult the
088: * {@link ServiceLocatorFactoryBean ServiceLocatorFactoryBean JavaDoc} for a
089: * fuller discussion of this issue.
090: *
091: * @author Colin Sampaleanu
092: * @since 2004-05-11
093: * @see org.springframework.beans.factory.ObjectFactory
094: * @see ServiceLocatorFactoryBean
095: */
096: public class ObjectFactoryCreatingFactoryBean extends
097: AbstractFactoryBean implements BeanFactoryAware {
098:
099: private String targetBeanName;
100:
101: private BeanFactory beanFactory;
102:
103: /**
104: * Set the name of the target bean.
105: * <p>
106: * The target does not <i>have</> to be a prototype bean, but realisticially
107: * always will be (because if the target bean were a singleton, then said
108: * singleton bean could simply be injected straight into the dependent object,
109: * thus obviating the need for the extra level of indirection afforded by
110: * the approach encapsulated by this class). Please note that no exception
111: * will be thrown if the supplied <code>targetBeanName</code> does not
112: * reference a prototype bean.
113: */
114: public void setTargetBeanName(String targetBeanName) {
115: this .targetBeanName = targetBeanName;
116: }
117:
118: public void setBeanFactory(BeanFactory beanFactory) {
119: this .beanFactory = beanFactory;
120: }
121:
122: protected Object createInstance() {
123: return new ObjectFactory() {
124: public Object getObject() throws BeansException {
125: return beanFactory.getBean(targetBeanName);
126: }
127: };
128: }
129:
130: public void afterPropertiesSet() throws Exception {
131: Assert.hasText(targetBeanName, "targetBeanName is required");
132: super .afterPropertiesSet();
133: }
134:
135: public Class getObjectType() {
136: return ObjectFactory.class;
137: }
138:
139: }
|