001/*
002 * Copyright 2002-2012 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.target.dynamic;
018
019import org.springframework.beans.factory.BeanFactory;
020import org.springframework.util.Assert;
021
022/**
023 * Refreshable TargetSource that fetches fresh target beans from a BeanFactory.
024 *
025 * <p>Can be subclassed to override {@code requiresRefresh()} to suppress
026 * unnecessary refreshes. By default, a refresh will be performed every time
027 * the "refreshCheckDelay" has elapsed.
028 *
029 * @author Rob Harrop
030 * @author Rod Johnson
031 * @author Juergen Hoeller
032 * @author Mark Fisher
033 * @since 2.0
034 * @see org.springframework.beans.factory.BeanFactory
035 * @see #requiresRefresh()
036 * @see #setRefreshCheckDelay
037 */
038public class BeanFactoryRefreshableTargetSource extends AbstractRefreshableTargetSource {
039
040        private final BeanFactory beanFactory;
041
042        private final String beanName;
043
044
045        /**
046         * Create a new BeanFactoryRefreshableTargetSource for the given
047         * bean factory and bean name.
048         * <p>Note that the passed-in BeanFactory should have an appropriate
049         * bean definition set up for the given bean name.
050         * @param beanFactory the BeanFactory to fetch beans from
051         * @param beanName the name of the target bean
052         */
053        public BeanFactoryRefreshableTargetSource(BeanFactory beanFactory, String beanName) {
054                Assert.notNull(beanFactory, "BeanFactory is required");
055                Assert.notNull(beanName, "Bean name is required");
056                this.beanFactory = beanFactory;
057                this.beanName = beanName;
058        }
059
060
061        /**
062         * Retrieve a fresh target object.
063         */
064        @Override
065        protected final Object freshTarget() {
066                return this.obtainFreshBean(this.beanFactory, this.beanName);
067        }
068
069        /**
070         * A template method that subclasses may override to provide a
071         * fresh target object for the given bean factory and bean name.
072         * <p>This default implementation fetches a new target bean
073         * instance from the bean factory.
074         * @see org.springframework.beans.factory.BeanFactory#getBean
075         */
076        protected Object obtainFreshBean(BeanFactory beanFactory, String beanName) {
077                return beanFactory.getBean(beanName);
078        }
079
080}