001/* 002 * Copyright 2002-2018 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.aop.support.DefaultIntroductionAdvisor; 020import org.springframework.aop.support.DelegatingIntroductionInterceptor; 021import org.springframework.beans.BeansException; 022import org.springframework.beans.factory.BeanFactory; 023import org.springframework.beans.factory.BeanInitializationException; 024import org.springframework.beans.factory.DisposableBean; 025import org.springframework.lang.Nullable; 026 027/** 028 * Abstract base class for pooling {@link org.springframework.aop.TargetSource} 029 * implementations which maintain a pool of target instances, acquiring and 030 * releasing a target object from the pool for each method invocation. 031 * This abstract base class is independent of concrete pooling technology; 032 * see the subclass {@link CommonsPool2TargetSource} for a concrete example. 033 * 034 * <p>Subclasses must implement the {@link #getTarget} and 035 * {@link #releaseTarget} methods based on their chosen object pool. 036 * The {@link #newPrototypeInstance()} method inherited from 037 * {@link AbstractPrototypeBasedTargetSource} can be used to create objects 038 * in order to put them into the pool. 039 * 040 * <p>Subclasses must also implement some of the monitoring methods from the 041 * {@link PoolingConfig} interface. The {@link #getPoolingConfigMixin()} method 042 * makes these stats available on proxied objects through an IntroductionAdvisor. 043 * 044 * <p>This class implements the {@link org.springframework.beans.factory.DisposableBean} 045 * interface in order to force subclasses to implement a {@link #destroy()} 046 * method, closing down their object pool. 047 * 048 * @author Rod Johnson 049 * @author Juergen Hoeller 050 * @see #getTarget 051 * @see #releaseTarget 052 * @see #destroy 053 */ 054@SuppressWarnings("serial") 055public abstract class AbstractPoolingTargetSource extends AbstractPrototypeBasedTargetSource 056 implements PoolingConfig, DisposableBean { 057 058 /** The maximum size of the pool. */ 059 private int maxSize = -1; 060 061 062 /** 063 * Set the maximum size of the pool. 064 * Default is -1, indicating no size limit. 065 */ 066 public void setMaxSize(int maxSize) { 067 this.maxSize = maxSize; 068 } 069 070 /** 071 * Return the maximum size of the pool. 072 */ 073 @Override 074 public int getMaxSize() { 075 return this.maxSize; 076 } 077 078 079 @Override 080 public final void setBeanFactory(BeanFactory beanFactory) throws BeansException { 081 super.setBeanFactory(beanFactory); 082 try { 083 createPool(); 084 } 085 catch (Throwable ex) { 086 throw new BeanInitializationException("Could not create instance pool for TargetSource", ex); 087 } 088 } 089 090 091 /** 092 * Create the pool. 093 * @throws Exception to avoid placing constraints on pooling APIs 094 */ 095 protected abstract void createPool() throws Exception; 096 097 /** 098 * Acquire an object from the pool. 099 * @return an object from the pool 100 * @throws Exception we may need to deal with checked exceptions from pool 101 * APIs, so we're forgiving with our exception signature 102 */ 103 @Override 104 @Nullable 105 public abstract Object getTarget() throws Exception; 106 107 /** 108 * Return the given object to the pool. 109 * @param target object that must have been acquired from the pool 110 * via a call to {@code getTarget()} 111 * @throws Exception to allow pooling APIs to throw exception 112 * @see #getTarget 113 */ 114 @Override 115 public abstract void releaseTarget(Object target) throws Exception; 116 117 118 /** 119 * Return an IntroductionAdvisor that providing a mixin 120 * exposing statistics about the pool maintained by this object. 121 */ 122 public DefaultIntroductionAdvisor getPoolingConfigMixin() { 123 DelegatingIntroductionInterceptor dii = new DelegatingIntroductionInterceptor(this); 124 return new DefaultIntroductionAdvisor(dii, PoolingConfig.class); 125 } 126 127}