01: /*
02: * Copyright 2002-2006 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.framework.autoproxy.target;
18:
19: import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
20: import org.springframework.aop.target.LazyInitTargetSource;
21: import org.springframework.beans.factory.config.BeanDefinition;
22: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
23:
24: /**
25: * TargetSourceCreator that enforces a LazyInitTargetSource for each bean
26: * that is defined as "lazy-init". This will lead to a proxy created for
27: * each of those beans, allowing to fetch a reference to such a bean
28: * without actually initialized the target bean instance.
29: *
30: * <p>To be registered as custom TargetSourceCreator for an auto-proxy creator,
31: * in combination with custom interceptors for specific beans or for the
32: * creation of lazy-init proxies only. For example, as autodetected
33: * infrastructure bean in an XML application context definition:
34: *
35: * <pre class="code">
36: * <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
37: * <property name="customTargetSourceCreators">
38: * <list>
39: * <bean class="org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator"/>
40: * </list>
41: * </property>
42: * </bean>
43: *
44: * <bean id="myLazyInitBean" class="mypackage.MyBeanClass" lazy-init="true">
45: * ...
46: * </bean></pre>
47: *
48: * @author Juergen Hoeller
49: * @since 1.2
50: * @see org.springframework.beans.factory.config.BeanDefinition#isLazyInit
51: * @see org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#setCustomTargetSourceCreators
52: * @see org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
53: */
54: public class LazyInitTargetSourceCreator extends
55: AbstractBeanFactoryBasedTargetSourceCreator {
56:
57: protected boolean isPrototypeBased() {
58: return false;
59: }
60:
61: protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
62: Class beanClass, String beanName) {
63:
64: if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
65: BeanDefinition definition = ((ConfigurableListableBeanFactory) getBeanFactory())
66: .getBeanDefinition(beanName);
67: if (definition.isLazyInit()) {
68: return new LazyInitTargetSource();
69: }
70: }
71: return null;
72: }
73:
74: }
|