01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.aop.target;
18:
19: import org.springframework.beans.BeansException;
20: import org.springframework.beans.factory.BeanDefinitionStoreException;
21: import org.springframework.beans.factory.BeanFactory;
22: import org.springframework.beans.factory.DisposableBean;
23: import org.springframework.beans.factory.config.ConfigurableBeanFactory;
24:
25: /**
26: * Base class for dynamic TargetSources that can create new prototype bean
27: * instances to support a pooling or new-instance-per-invocation strategy.
28: *
29: * <p>Such TargetSources must run in a BeanFactory, as it needs to call the
30: * <code>getBean</code> method to create a new prototype instance.
31: * Therefore, this base class extends {@link AbstractBeanFactoryBasedTargetSource}.
32: *
33: * @author Rod Johnson
34: * @author Juergen Hoeller
35: * @see org.springframework.beans.factory.BeanFactory#getBean
36: * @see PrototypeTargetSource
37: * @see ThreadLocalTargetSource
38: * @see CommonsPoolTargetSource
39: */
40: public abstract class AbstractPrototypeBasedTargetSource extends
41: AbstractBeanFactoryBasedTargetSource {
42:
43: public void setBeanFactory(BeanFactory beanFactory)
44: throws BeansException {
45: super .setBeanFactory(beanFactory);
46:
47: // Check whether the target bean is defined as prototype.
48: if (!beanFactory.isPrototype(getTargetBeanName())) {
49: throw new BeanDefinitionStoreException(
50: "Cannot use prototype-based TargetSource against non-prototype bean with name '"
51: + getTargetBeanName()
52: + "': instances would not be independent");
53: }
54: }
55:
56: /**
57: * Subclasses should call this method to create a new prototype instance.
58: * @throws BeansException if bean creation failed
59: */
60: protected Object newPrototypeInstance() throws BeansException {
61: if (logger.isDebugEnabled()) {
62: logger.debug("Creating new instance of bean '"
63: + getTargetBeanName() + "'");
64: }
65: return getBeanFactory().getBean(getTargetBeanName());
66: }
67:
68: /**
69: * Subclasses should call this method to destroy an obsolote prototype instance.
70: * @param target the bean instance to destroy
71: */
72: protected void destroyPrototypeInstance(Object target) {
73: if (logger.isDebugEnabled()) {
74: logger.debug("Destroying instance of bean '"
75: + getTargetBeanName() + "'");
76: }
77: if (getBeanFactory() instanceof ConfigurableBeanFactory) {
78: ((ConfigurableBeanFactory) getBeanFactory()).destroyBean(
79: getTargetBeanName(), target);
80: } else if (target instanceof DisposableBean) {
81: try {
82: ((DisposableBean) target).destroy();
83: } catch (Throwable ex) {
84: logger.error(
85: "Couldn't invoke destroy method of bean with name '"
86: + getTargetBeanName() + "'", ex);
87: }
88: }
89: }
90:
91: }
|