@Override @Nullable protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPool2TargetSource cpts = new CommonsPool2TargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }
/** * Implementation of abstract getTarget() method. * We look for a target held in a ThreadLocal. If we don't find one, * we create one and bind it to the thread. No synchronization is required. */ @Override public Object getTarget() throws BeansException { ++this.invocationCount; Object target = this.targetInThread.get(); if (target == null) { if (logger.isDebugEnabled()) { logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " + "creating one and binding it to thread '" + Thread.currentThread().getName() + "'"); } // Associate target with ThreadLocal. target = newPrototypeInstance(); this.targetInThread.set(target); synchronized (this.targetSet) { this.targetSet.add(target); } } else { ++this.hitCount; } return target; }
/** * Dispose of targets if necessary; clear ThreadLocal. * @see #destroyPrototypeInstance */ @Override public void destroy() { logger.debug("Destroying ThreadLocalTargetSource bindings"); synchronized (this.targetSet) { for (Object target : this.targetSet) { destroyPrototypeInstance(target); } this.targetSet.clear(); } // Clear ThreadLocal, just in case. this.targetInThread.remove(); }
/** * Test for SPR-1442. Destroyed target should re-associated with thread and not throw NPE */ @Test public void testReuseDestroyedTarget() { ThreadLocalTargetSource source = (ThreadLocalTargetSource)this.beanFactory.getBean("threadLocalTs"); // try first time source.getTarget(); source.destroy(); // try second time try { source.getTarget(); } catch (NullPointerException ex) { fail("Should not throw NPE"); } }
/** * Implementation of abstract getTarget() method. * We look for a target held in a ThreadLocal. If we don't find one, * we create one and bind it to the thread. No synchronization is required. */ @Override public Object getTarget() throws BeansException { ++this.invocationCount; Object target = this.targetInThread.get(); if (target == null) { if (logger.isDebugEnabled()) { logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " + "creating one and binding it to thread '" + Thread.currentThread().getName() + "'"); } // Associate target with ThreadLocal. target = newPrototypeInstance(); this.targetInThread.set(target); synchronized (this.targetSet) { this.targetSet.add(target); } } else { ++this.hitCount; } return target; }
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class beanClass, String beanName) { // See if there's a pooling attribute. Collection atts = this.attributes.getAttributes(beanClass, ThreadLocalAttribute.class); if (atts.isEmpty()) { // No pooling attribute: don't create a custom TargetSource. return null; } else { return new ThreadLocalTargetSource(); } }
/** * Dispose of targets if necessary; clear ThreadLocal. * @see #destroyPrototypeInstance */ @Override public void destroy() { logger.debug("Destroying ThreadLocalTargetSource bindings"); synchronized (this.targetSet) { for (Object target : this.targetSet) { destroyPrototypeInstance(target); } this.targetSet.clear(); } // Clear ThreadLocal, just in case. this.targetInThread.remove(); }
/** * Implementation of abstract getTarget() method. * We look for a target held in a ThreadLocal. If we don't find one, * we create one and bind it to the thread. No synchronization is required. */ public Object getTarget() throws BeansException { ++this.invocationCount; Object target = this.targetInThread.get(); if (target == null) { if (logger.isDebugEnabled()) { logger.debug("No target for prototype '" + getTargetBeanName() + "' found in thread: " + "creating one and binding it to thread '" + Thread.currentThread().getName() + "'"); } // associate target with ThreadLocal target = newPrototypeInstance(); this.targetInThread.set(target); this.targetSet.add(target); } else { ++this.hitCount; } return target; }
@Override @Nullable protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPool2TargetSource cpts = new CommonsPool2TargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }
/** * Dispose of targets if necessary; clear ThreadLocal. * @see #destroyPrototypeInstance */ @Override public void destroy() { logger.debug("Destroying ThreadLocalTargetSource bindings"); synchronized (this.targetSet) { for (Object target : this.targetSet) { destroyPrototypeInstance(target); } this.targetSet.clear(); } // Clear ThreadLocal, just in case. this.targetInThread.remove(); }
/** * Implementation of abstract getTarget() method. * We look for a target held in a ThreadLocal. If we don't find one, * we create one and bind it to the thread. No synchronization is required. */ @Override public Object getTarget() throws BeansException { ++this.invocationCount; Object target = this.targetInThread.get(); if (target == null) { if (logger.isDebugEnabled()) { logger.debug("No target for prototype '" + getTargetBeanName() + "' bound to thread: " + "creating one and binding it to thread '" + Thread.currentThread().getName() + "'"); } // Associate target with ThreadLocal. target = newPrototypeInstance(); this.targetInThread.set(target); synchronized (this.targetSet) { this.targetSet.add(target); } } else { ++this.hitCount; } return target; }
protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPoolTargetSource cpts = new CommonsPoolTargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }
@Override protected final AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource( Class<?> beanClass, String beanName) { if (beanName.startsWith(PREFIX_COMMONS_POOL)) { CommonsPool2TargetSource cpts = new CommonsPool2TargetSource(); cpts.setMaxSize(25); return cpts; } else if (beanName.startsWith(PREFIX_THREAD_LOCAL)) { return new ThreadLocalTargetSource(); } else if (beanName.startsWith(PREFIX_PROTOTYPE)) { return new PrototypeTargetSource(); } else { // No match. Don't create a custom target source. return null; } }