KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > target > AbstractPrototypeBasedTargetSource


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 AbstractBeanFactoryBasedTargetSource {
41
42     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
43         super.setBeanFactory(beanFactory);
44
45         // Check whether the target bean is defined as prototype.
46
if (!beanFactory.isPrototype(getTargetBeanName())) {
47             throw new BeanDefinitionStoreException(
48                     "Cannot use prototype-based TargetSource against non-prototype bean with name '" +
49                     getTargetBeanName() + "': instances would not be independent");
50         }
51     }
52
53     /**
54      * Subclasses should call this method to create a new prototype instance.
55      * @throws BeansException if bean creation failed
56      */

57     protected Object JavaDoc newPrototypeInstance() throws BeansException {
58         if (logger.isDebugEnabled()) {
59             logger.debug("Creating new instance of bean '" + getTargetBeanName() + "'");
60         }
61         return getBeanFactory().getBean(getTargetBeanName());
62     }
63
64     /**
65      * Subclasses should call this method to destroy an obsolote prototype instance.
66      * @param target the bean instance to destroy
67      */

68     protected void destroyPrototypeInstance(Object JavaDoc target) {
69         if (logger.isDebugEnabled()) {
70             logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
71         }
72         if (getBeanFactory() instanceof ConfigurableBeanFactory) {
73             ((ConfigurableBeanFactory) getBeanFactory()).destroyBean(getTargetBeanName(), target);
74         }
75         else if (target instanceof DisposableBean) {
76             try {
77                 ((DisposableBean) target).destroy();
78             }
79             catch (Throwable JavaDoc ex) {
80                 logger.error("Couldn't invoke destroy method of bean with name '" + getTargetBeanName() + "'", ex);
81             }
82         }
83     }
84
85 }
86
Popular Tags