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;
023import org.springframework.lang.Nullable;
024
025/**
026 * TargetSourceCreator that enforces a LazyInitTargetSource for each bean
027 * that is defined as "lazy-init". This will lead to a proxy created for
028 * each of those beans, allowing to fetch a reference to such a bean
029 * without actually initializing the target bean instance.
030 *
031 * <p>To be registered as custom TargetSourceCreator for an auto-proxy creator,
032 * in combination with custom interceptors for specific beans or for the
033 * creation of lazy-init proxies only. For example, as autodetected
034 * infrastructure bean in an XML application context definition:
035 *
036 * <pre class="code">
037 * &lt;bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;
038 *   &lt;property name="customTargetSourceCreators"&gt;
039 *     &lt;list&gt;
040 *       &lt;bean class="org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator"/&gt;
041 *     &lt;/list&gt;
042 *   &lt;/property&gt;
043 * &lt;/bean&gt;
044 *
045 * &lt;bean id="myLazyInitBean" class="mypackage.MyBeanClass" lazy-init="true"&gt;
046 *   ...
047 * &lt;/bean&gt;</pre>
048 *
049 * @author Juergen Hoeller
050 * @since 1.2
051 * @see org.springframework.beans.factory.config.BeanDefinition#isLazyInit
052 * @see org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#setCustomTargetSourceCreators
053 * @see org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
054 */
055public class LazyInitTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
056
057        @Override
058        protected boolean isPrototypeBased() {
059                return false;
060        }
061
062        @Override
063        @Nullable
064        protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
065                        Class<?> beanClass, String beanName) {
066
067                if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
068                        BeanDefinition definition =
069                                        ((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName);
070                        if (definition.isLazyInit()) {
071                                return new LazyInitTargetSource();
072                        }
073                }
074                return null;
075        }
076
077}