001: /*
002: * Copyright 2002-2007 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.aop.framework.autoproxy.target;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import org.springframework.aop.TargetSource;
023: import org.springframework.aop.framework.autoproxy.TargetSourceCreator;
024: import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
025: import org.springframework.beans.factory.BeanFactory;
026: import org.springframework.beans.factory.BeanFactoryAware;
027: import org.springframework.beans.factory.DisposableBean;
028: import org.springframework.beans.factory.support.AbstractBeanFactory;
029: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
030: import org.springframework.beans.factory.support.RootBeanDefinition;
031:
032: /**
033: * Convenient superclass for
034: * {@link org.springframework.aop.framework.autoproxy.TargetSourceCreator}
035: * implementations that require creating multiple instances of a prototype bean.
036: *
037: * <p>Uses an internal BeanFactory to manage the target instances,
038: * copying the original bean definition to this internal factory.
039: * This is necessary because the original BeanFactory will just
040: * contain the proxy instance created through auto-proxying.
041: *
042: * <p>Requires running in an
043: * {@link org.springframework.beans.factory.support.AbstractBeanFactory}.
044: *
045: * @author Rod Johnson
046: * @author Juergen Hoeller
047: * @see org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource
048: * @see org.springframework.beans.factory.support.AbstractBeanFactory
049: */
050: public abstract class AbstractBeanFactoryBasedTargetSourceCreator
051: implements TargetSourceCreator, BeanFactoryAware,
052: DisposableBean {
053:
054: protected final Log logger = LogFactory.getLog(getClass());
055:
056: private AbstractBeanFactory beanFactory;
057:
058: private DefaultListableBeanFactory internalBeanFactory;
059:
060: public final void setBeanFactory(BeanFactory beanFactory) {
061: if (!(beanFactory instanceof AbstractBeanFactory)) {
062: throw new IllegalArgumentException(
063: "Cannot do auto-TargetSource creation with a BeanFactory that doesn't extend AbstractBeanFactory: "
064: + beanFactory);
065: }
066: this .beanFactory = (AbstractBeanFactory) beanFactory;
067: this .internalBeanFactory = new DefaultListableBeanFactory(
068: beanFactory);
069: }
070:
071: /**
072: * Return the BeanFactory that this TargetSourceCreators runs in.
073: */
074: protected final BeanFactory getBeanFactory() {
075: return this .beanFactory;
076: }
077:
078: /**
079: * Destroys the internal BeanFactory on shutdown of the TargetSourceCreator.
080: */
081: public void destroy() {
082: this .internalBeanFactory.destroySingletons();
083: }
084:
085: public final TargetSource getTargetSource(Class beanClass,
086: String beanName) {
087: AbstractBeanFactoryBasedTargetSource targetSource = createBeanFactoryBasedTargetSource(
088: beanClass, beanName);
089: if (targetSource == null) {
090: return null;
091: }
092:
093: if (logger.isDebugEnabled()) {
094: logger
095: .debug("Configuring AbstractBeanFactoryBasedTargetSource: "
096: + targetSource);
097: }
098:
099: // We need to override just this bean definition, as it may reference other beans
100: // and we're happy to take the parent's definition for those.
101: // Always use a prototype.
102: RootBeanDefinition bd = this .beanFactory
103: .getMergedBeanDefinition(beanName);
104: RootBeanDefinition bdCopy = new RootBeanDefinition(bd);
105: bdCopy.setSingleton(!isPrototypeBased());
106: this .internalBeanFactory.registerBeanDefinition(beanName,
107: bdCopy);
108:
109: // Complete configuring the PrototypeTargetSource.
110: targetSource.setTargetBeanName(beanName);
111: targetSource.setBeanFactory(this .internalBeanFactory);
112:
113: return targetSource;
114: }
115:
116: /**
117: * Return whether this TargetSourceCreator is prototype-based.
118: * The "singleton" flag of the target bean definition will be set accordingly.
119: * <p>Default is "true".
120: * @see org.springframework.beans.factory.config.BeanDefinition#isSingleton()
121: */
122: protected boolean isPrototypeBased() {
123: return true;
124: }
125:
126: /**
127: * Subclasses must implement this method to return a new AbstractPrototypeBasedTargetSource
128: * if they wish to create a custom TargetSource for this bean, or <code>null</code> if they are
129: * not interested it in, in which case no special target source will be created.
130: * Subclasses should not call <code>setTargetBeanName</code> or <code>setBeanFactory</code>
131: * on the AbstractPrototypeBasedTargetSource: This class' implementation of
132: * <code>getTargetSource()</code> will do that.
133: * @param beanClass the class of the bean to create a TargetSource for
134: * @param beanName the name of the bean
135: * @return the AbstractPrototypeBasedTargetSource, or <code>null</code> if we don't match this
136: */
137: protected abstract AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
138: Class beanClass, String beanName);
139:
140: }
|