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.scope;
018
019import java.io.Serializable;
020
021import org.springframework.beans.factory.config.ConfigurableBeanFactory;
022import org.springframework.util.Assert;
023
024/**
025 * Default implementation of the {@link ScopedObject} interface.
026 *
027 * <p>Simply delegates the calls to the underlying
028 * {@link ConfigurableBeanFactory bean factory}
029 * ({@link ConfigurableBeanFactory#getBean(String)}/
030 * {@link ConfigurableBeanFactory#destroyScopedBean(String)}).
031 *
032 * @author Juergen Hoeller
033 * @since 2.0
034 * @see org.springframework.beans.factory.BeanFactory#getBean
035 * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#destroyScopedBean
036 */
037@SuppressWarnings("serial")
038public class DefaultScopedObject implements ScopedObject, Serializable {
039
040        private final ConfigurableBeanFactory beanFactory;
041
042        private final String targetBeanName;
043
044
045        /**
046         * Creates a new instance of the {@link DefaultScopedObject} class.
047         * @param beanFactory the {@link ConfigurableBeanFactory} that holds the scoped target object
048         * @param targetBeanName the name of the target bean
049         */
050        public DefaultScopedObject(ConfigurableBeanFactory beanFactory, String targetBeanName) {
051                Assert.notNull(beanFactory, "BeanFactory must not be null");
052                Assert.hasText(targetBeanName, "'targetBeanName' must not be empty");
053                this.beanFactory = beanFactory;
054                this.targetBeanName = targetBeanName;
055        }
056
057
058        @Override
059        public Object getTargetObject() {
060                return this.beanFactory.getBean(this.targetBeanName);
061        }
062
063        @Override
064        public void removeFromScope() {
065                this.beanFactory.destroyScopedBean(this.targetBeanName);
066        }
067
068}