KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 /**
22  * {@link org.springframework.aop.TargetSource} that lazily accesses a
23  * singleton bean from a {@link org.springframework.beans.factory.BeanFactory}.
24  *
25  * <p>Useful when a proxy reference is needed on initialization but
26  * the actual target object should not be initialized until first use.
27  * When the target bean is defined in an
28  * {@link org.springframework.context.ApplicationContext} (or a
29  * <code>BeanFactory</code> that is eagerly pre-instantiating singleton beans)
30  * it must be marked as "lazy-init" too, else it will be instantiated by said
31  * <code>ApplicationContext</code> (or <code>BeanFactory</code>) on startup.
32  * <p>For example:
33  *
34  * <pre class="code">
35  * &lt;bean id="serviceTarget" class="example.MyService" lazy-init="true"&gt;
36  * ...
37  * &lt;/bean&gt;
38  *
39  * &lt;bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;
40  * &lt;property name="targetSource"&gt;
41  * &lt;bean class="org.springframework.aop.target.LazyInitTargetSource"&gt;
42  * &lt;property name="targetBeanName"&gt;&lt;idref local="serviceTarget"/&gt;&lt;/property&gt;
43  * &lt;/bean&gt;
44  * &lt;/property&gt;
45  * &lt;/bean&gt;</pre>
46  *
47  * The "serviceTarget" bean will not get initialized until a method on the
48  * "service" proxy gets invoked.
49  *
50  * <p>Subclasses can extend this class and override the {@link #postProcessTargetObject(Object)} to
51  * perform some additional processing with the target object when it is first loaded.
52  *
53  * @author Juergen Hoeller
54  * @author Rob Harrop
55  * @since 1.1.4
56  * @see org.springframework.beans.factory.BeanFactory#getBean
57  * @see #postProcessTargetObject
58  */

59 public class LazyInitTargetSource extends AbstractBeanFactoryBasedTargetSource {
60
61     private Object JavaDoc target;
62
63
64     public synchronized Object JavaDoc getTarget() throws BeansException {
65         if (this.target == null) {
66             this.target = getBeanFactory().getBean(getTargetBeanName());
67             postProcessTargetObject(this.target);
68         }
69         return this.target;
70     }
71
72     /**
73      * Subclasses may override this method to perform additional processing on
74      * the target object when it is first loaded.
75      * @param targetObject the target object that has just been instantiated (and configured)
76      */

77     protected void postProcessTargetObject(Object JavaDoc targetObject) {
78     }
79
80 }
81
Popular Tags