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:
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: * <bean id="serviceTarget" class="example.MyService" lazy-init="true">
36: * ...
37: * </bean>
38: *
39: * <bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">
40: * <property name="targetSource">
41: * <bean class="org.springframework.aop.target.LazyInitTargetSource">
42: * <property name="targetBeanName"><idref local="serviceTarget"/></property>
43: * </bean>
44: * </property>
45: * </bean></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
60: AbstractBeanFactoryBasedTargetSource {
61:
62: private Object target;
63:
64: public synchronized Object 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 targetObject) {
78: }
79:
80: }
|