/** * Set the name of the bean that is to be scoped. */ public void setTargetBeanName(String targetBeanName) { this.targetBeanName = targetBeanName; this.scopedTargetSource.setTargetBeanName(targetBeanName); }
/** * Create a CommonsPoolTargetSource with default settings. * Default maximum size of the pool is 8. * @see #setMaxSize * @see GenericObjectPoolConfig#setMaxTotal */ public CommonsPool2TargetSource() { setMaxSize(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL); }
@Override public String toString() { return "PrototypeTargetSource for target bean with name '" + getTargetBeanName() + "'"; }
/** * Determine a TargetSource for the given target (or TargetSource). * @param target target. If this is an implementation of TargetSource it is * used as our TargetSource; otherwise it is wrapped in a SingletonTargetSource. * @return a TargetSource for this object */ protected TargetSource createTargetSource(Object target) { if (target instanceof TargetSource) { return (TargetSource) target; } else { return new SingletonTargetSource(target); } }
@Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { super.setBeanFactory(beanFactory); // Check whether the target bean is defined as prototype. if (!beanFactory.isPrototype(getTargetBeanName())) { throw new BeanDefinitionStoreException( "Cannot use prototype-based TargetSource against non-prototype bean with name '" + getTargetBeanName() + "': instances would not be independent"); } }
@Bean public CommonsPool2TargetSource poolTargetSource() { CommonsPool2TargetSource pool = new CommonsPool2TargetSource(); pool.setMaxSize(3); pool.setTargetBeanName("myObjectTarget"); return pool; }
@Test public void referenceIdentityByDefault() throws Exception { CommonsPool2TargetSource targetSource = new CommonsPool2TargetSource(); targetSource.setMaxWait(1); prepareTargetSource(targetSource); Object first = targetSource.getTarget(); Object second = targetSource.getTarget(); assertTrue(first instanceof SerializablePerson); assertTrue(second instanceof SerializablePerson); assertEquals(first, second); targetSource.releaseTarget(first); targetSource.releaseTarget(second); }
@Override public Object getTarget() throws Exception { return getBeanFactory().getBean(getTargetBeanName()); }
@Override public final void setBeanFactory(BeanFactory beanFactory) throws BeansException { super.setBeanFactory(beanFactory); try { createPool(); } catch (Throwable ex) { throw new BeanInitializationException("Could not create instance pool for TargetSource", ex); } }
/** * Obtain a new prototype instance for every call. * @see #newPrototypeInstance() */ @Override public Object getTarget() throws BeansException { return newPrototypeInstance(); }
@Override public Class<?> getObjectType() { if (this.proxy != null) { return this.proxy.getClass(); } return this.scopedTargetSource.getTargetClass(); }
/** * Destroy the given independent instance. * @see #destroyPrototypeInstance */ @Override public void releaseTarget(Object target) { destroyPrototypeInstance(target); }
/** * Creates and holds an ObjectPool instance. * @see #createObjectPool() */ @Override protected final void createPool() { logger.debug("Creating Commons object pool"); this.pool = createObjectPool(); }
/** * Return an EmptyTargetSource for the given target Class. * @param targetClass the target Class (may be {@code null}) * @param isStatic whether the TargetSource should be marked as static * @see #getTargetClass() */ public static EmptyTargetSource forClass(@Nullable Class<?> targetClass, boolean isStatic) { return (targetClass == null && isStatic ? INSTANCE : new EmptyTargetSource(targetClass, isStatic)); }
@Override public void destroyObject(PooledObject<Object> p) throws Exception { destroyPrototypeInstance(p.getObject()); }
private void prepareTargetSource(CommonsPool2TargetSource targetSource) { String beanName = "target"; StaticApplicationContext applicationContext = new StaticApplicationContext(); applicationContext.registerPrototype(beanName, SerializablePerson.class); targetSource.setTargetBeanName(beanName); targetSource.setBeanFactory(applicationContext); }
@Override protected Object createObject() { return new InitCountingBean(); } @Override
@Override public Object getTarget() throws Exception { return newPrototypeInstance(); }
@Test public void testFunctionality() { testFunctionality("pooled"); }
/** * Set the given object as target. * Will create a SingletonTargetSource for the object. * @see #setTargetSource * @see org.springframework.aop.target.SingletonTargetSource */ public void setTarget(Object target) { setTargetSource(new SingletonTargetSource(target)); }