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.target;
018
019import org.springframework.beans.BeansException;
020
021/**
022 * {@link org.springframework.aop.TargetSource} implementation that
023 * creates a new instance of the target bean for each request,
024 * destroying each instance on release (after each request).
025 *
026 * <p>Obtains bean instances from its containing
027 * {@link org.springframework.beans.factory.BeanFactory}.
028 *
029 * @author Rod Johnson
030 * @author Juergen Hoeller
031 * @see #setBeanFactory
032 * @see #setTargetBeanName
033 */
034@SuppressWarnings("serial")
035public class PrototypeTargetSource extends AbstractPrototypeBasedTargetSource {
036
037        /**
038         * Obtain a new prototype instance for every call.
039         * @see #newPrototypeInstance()
040         */
041        @Override
042        public Object getTarget() throws BeansException {
043                return newPrototypeInstance();
044        }
045
046        /**
047         * Destroy the given independent instance.
048         * @see #destroyPrototypeInstance
049         */
050        @Override
051        public void releaseTarget(Object target) {
052                destroyPrototypeInstance(target);
053        }
054
055        @Override
056        public String toString() {
057                return "PrototypeTargetSource for target bean with name '" + getTargetBeanName() + "'";
058        }
059
060}