1 /* 2 * Copyright 2002-2006 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.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 AbstractBeanFactoryBasedTargetSourceCreator { 55 56 protected boolean isPrototypeBased() { 57 return false; 58 } 59 60 protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( 61 Class beanClass, String beanName) { 62 63 if (getBeanFactory() instanceof ConfigurableListableBeanFactory) { 64 BeanDefinition definition = 65 ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName); 66 if (definition.isLazyInit()) { 67 return new LazyInitTargetSource(); 68 } 69 } 70 return null; 71 } 72 73 } 74