001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.aop.framework.autoproxy.target;
018
019import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
020import org.springframework.aop.target.LazyInitTargetSource;
021import org.springframework.beans.factory.config.BeanDefinition;
022import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
023
024/**
025 * TargetSourceCreator that enforces a LazyInitTargetSource for each bean
026 * that is defined as "lazy-init". This will lead to a proxy created for
027 * each of those beans, allowing to fetch a reference to such a bean
028 * without actually initializing the target bean instance.
029 *
030 * <p>To be registered as custom TargetSourceCreator for an auto-proxy creator,
031 * in combination with custom interceptors for specific beans or for the
032 * creation of lazy-init proxies only. For example, as autodetected
033 * infrastructure bean in an XML application context definition:
034 *
035 * <pre class="code">
036 * &lt;bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;
037 *   &lt;property name="customTargetSourceCreators"&gt;
038 *     &lt;list&gt;
039 *       &lt;bean class="org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator"/&gt;
040 *     &lt;/list&gt;
041 *   &lt;/property&gt;
042 * &lt;/bean&gt;
043 *
044 * &lt;bean id="myLazyInitBean" class="mypackage.MyBeanClass" lazy-init="true"&gt;
045 *   ...
046 * &lt;/bean&gt;</pre>
047 *
048 * @author Juergen Hoeller
049 * @since 1.2
050 * @see org.springframework.beans.factory.config.BeanDefinition#isLazyInit
051 * @see org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#setCustomTargetSourceCreators
052 * @see org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
053 */
054public class LazyInitTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
055
056        @Override
057        protected boolean isPrototypeBased() {
058                return false;
059        }
060
061        @Override
062        protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
063                        Class<?> beanClass, String beanName) {
064
065                if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
066                        BeanDefinition definition =
067                                        ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName);
068                        if (definition.isLazyInit()) {
069                                return new LazyInitTargetSource();
070                        }
071                }
072                return null;
073        }
074
075}