org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor Java Examples

The following examples show how to use org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DisposableBeanAdapter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Search for all DestructionAwareBeanPostProcessors in the List.
 * @param processors the List to search
 * @return the filtered List of DestructionAwareBeanPostProcessors
 */
@Nullable
private List<DestructionAwareBeanPostProcessor> filterPostProcessors(List<BeanPostProcessor> processors, Object bean) {
	List<DestructionAwareBeanPostProcessor> filteredPostProcessors = null;
	if (!CollectionUtils.isEmpty(processors)) {
		filteredPostProcessors = new ArrayList<>(processors.size());
		for (BeanPostProcessor processor : processors) {
			if (processor instanceof DestructionAwareBeanPostProcessor) {
				DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
				if (dabpp.requiresDestruction(bean)) {
					filteredPostProcessors.add(dabpp);
				}
			}
		}
	}
	return filteredPostProcessors;
}
 
Example #2
Source File: DisposableBeanAdapter.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Search for all DestructionAwareBeanPostProcessors in the List.
 * @param processors the List to search
 * @return the filtered List of DestructionAwareBeanPostProcessors
 */
@Nullable
private List<DestructionAwareBeanPostProcessor> filterPostProcessors(List<BeanPostProcessor> processors, Object bean) {
	List<DestructionAwareBeanPostProcessor> filteredPostProcessors = null;
	if (!CollectionUtils.isEmpty(processors)) {
		filteredPostProcessors = new ArrayList<>(processors.size());
		for (BeanPostProcessor processor : processors) {
			if (processor instanceof DestructionAwareBeanPostProcessor) {
				DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
				if (dabpp.requiresDestruction(bean)) {
					filteredPostProcessors.add(dabpp);
				}
			}
		}
	}
	return filteredPostProcessors;
}
 
Example #3
Source File: DisposableBeanAdapter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Check whether the given bean has destruction-aware post-processors applying to it.
 * @param bean the bean instance
 * @param postProcessors the post-processor candidates
 */
public static boolean hasApplicableProcessors(Object bean, List<BeanPostProcessor> postProcessors) {
	if (!CollectionUtils.isEmpty(postProcessors)) {
		for (BeanPostProcessor processor : postProcessors) {
			if (processor instanceof DestructionAwareBeanPostProcessor) {
				DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
				try {
					if (dabpp.requiresDestruction(bean)) {
						return true;
					}
				}
				catch (AbstractMethodError err) {
					// A pre-4.3 third-party DestructionAwareBeanPostProcessor...
					// As of 5.0, we can let requiresDestruction be a Java 8 default method which returns true.
					return true;
				}
			}
		}
	}
	return false;
}
 
Example #4
Source File: DisposableBeanAdapter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Search for all DestructionAwareBeanPostProcessors in the List.
 * @param processors the List to search
 * @return the filtered List of DestructionAwareBeanPostProcessors
 */
private List<DestructionAwareBeanPostProcessor> filterPostProcessors(List<BeanPostProcessor> processors, Object bean) {
	List<DestructionAwareBeanPostProcessor> filteredPostProcessors = null;
	if (!CollectionUtils.isEmpty(processors)) {
		filteredPostProcessors = new ArrayList<DestructionAwareBeanPostProcessor>(processors.size());
		for (BeanPostProcessor processor : processors) {
			if (processor instanceof DestructionAwareBeanPostProcessor) {
				DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
				try {
					if (dabpp.requiresDestruction(bean)) {
						filteredPostProcessors.add(dabpp);
					}
				}
				catch (AbstractMethodError err) {
					// A pre-4.3 third-party DestructionAwareBeanPostProcessor...
					// As of 5.0, we can let requiresDestruction be a Java 8 default method which returns true.
					filteredPostProcessors.add(dabpp);
				}
			}
		}
	}
	return filteredPostProcessors;
}
 
Example #5
Source File: AbstractBeanFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
	Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
	this.beanPostProcessors.remove(beanPostProcessor);
	this.beanPostProcessors.add(beanPostProcessor);
	if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
		this.hasInstantiationAwareBeanPostProcessors = true;
	}
	if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
		this.hasDestructionAwareBeanPostProcessors = true;
	}
}
 
Example #6
Source File: DisposableBeanAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 */
private DisposableBeanAdapter(Object bean, String beanName, boolean invokeDisposableBean,
		boolean nonPublicAccessAllowed, @Nullable String destroyMethodName,
		@Nullable List<DestructionAwareBeanPostProcessor> postProcessors) {

	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean = invokeDisposableBean;
	this.nonPublicAccessAllowed = nonPublicAccessAllowed;
	this.acc = null;
	this.destroyMethodName = destroyMethodName;
	this.beanPostProcessors = postProcessors;
}
 
Example #7
Source File: DisposableBeanAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes a copy of the state of this class,
 * filtering out non-serializable BeanPostProcessors.
 */
protected Object writeReplace() {
	List<DestructionAwareBeanPostProcessor> serializablePostProcessors = null;
	if (this.beanPostProcessors != null) {
		serializablePostProcessors = new ArrayList<DestructionAwareBeanPostProcessor>();
		for (DestructionAwareBeanPostProcessor postProcessor : this.beanPostProcessors) {
			if (postProcessor instanceof Serializable) {
				serializablePostProcessors.add(postProcessor);
			}
		}
	}
	return new DisposableBeanAdapter(this.bean, this.beanName, this.invokeDisposableBean,
			this.nonPublicAccessAllowed, this.destroyMethodName, serializablePostProcessors);
}
 
Example #8
Source File: DisposableBeanAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Search for all DestructionAwareBeanPostProcessors in the List.
 * @param postProcessors the List to search
 * @return the filtered List of DestructionAwareBeanPostProcessors
 */
private List<DestructionAwareBeanPostProcessor> filterPostProcessors(List<BeanPostProcessor> postProcessors) {
	List<DestructionAwareBeanPostProcessor> filteredPostProcessors = null;
	if (!CollectionUtils.isEmpty(postProcessors)) {
		filteredPostProcessors = new ArrayList<DestructionAwareBeanPostProcessor>(postProcessors.size());
		for (BeanPostProcessor postProcessor : postProcessors) {
			if (postProcessor instanceof DestructionAwareBeanPostProcessor) {
				filteredPostProcessors.add((DestructionAwareBeanPostProcessor) postProcessor);
			}
		}
	}
	return filteredPostProcessors;
}
 
Example #9
Source File: DisposableBeanAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 */
private DisposableBeanAdapter(Object bean, String beanName, boolean invokeDisposableBean,
		boolean nonPublicAccessAllowed, String destroyMethodName,
		List<DestructionAwareBeanPostProcessor> postProcessors) {

	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean = invokeDisposableBean;
	this.nonPublicAccessAllowed = nonPublicAccessAllowed;
	this.acc = null;
	this.destroyMethodName = destroyMethodName;
	this.beanPostProcessors = postProcessors;
}
 
Example #10
Source File: AbstractBeanFactory.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
	Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
	this.beanPostProcessors.remove(beanPostProcessor);
	this.beanPostProcessors.add(beanPostProcessor);
	if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
		this.hasInstantiationAwareBeanPostProcessors = true;
	}
	if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
		this.hasDestructionAwareBeanPostProcessors = true;
	}
}
 
Example #11
Source File: DisposableBeanAdapter.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Serializes a copy of the state of this class,
 * filtering out non-serializable BeanPostProcessors.
 */
protected Object writeReplace() {
	List<DestructionAwareBeanPostProcessor> serializablePostProcessors = null;
	if (this.beanPostProcessors != null) {
		serializablePostProcessors = new ArrayList<DestructionAwareBeanPostProcessor>();
		for (DestructionAwareBeanPostProcessor postProcessor : this.beanPostProcessors) {
			if (postProcessor instanceof Serializable) {
				serializablePostProcessors.add(postProcessor);
			}
		}
	}
	return new DisposableBeanAdapter(this.bean, this.beanName, this.invokeDisposableBean,
			this.nonPublicAccessAllowed, this.destroyMethodName, serializablePostProcessors);
}
 
Example #12
Source File: DisposableBeanAdapter.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Search for all DestructionAwareBeanPostProcessors in the List.
 * @param postProcessors the List to search
 * @return the filtered List of DestructionAwareBeanPostProcessors
 */
private List<DestructionAwareBeanPostProcessor> filterPostProcessors(List<BeanPostProcessor> postProcessors) {
	List<DestructionAwareBeanPostProcessor> filteredPostProcessors = null;
	if (postProcessors != null && !postProcessors.isEmpty()) {
		filteredPostProcessors = new ArrayList<DestructionAwareBeanPostProcessor>(postProcessors.size());
		for (BeanPostProcessor postProcessor : postProcessors) {
			if (postProcessor instanceof DestructionAwareBeanPostProcessor) {
				filteredPostProcessors.add((DestructionAwareBeanPostProcessor) postProcessor);
			}
		}
	}
	return filteredPostProcessors;
}
 
Example #13
Source File: DisposableBeanAdapter.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 */
private DisposableBeanAdapter(Object bean, String beanName, boolean invokeDisposableBean,
		boolean nonPublicAccessAllowed, String destroyMethodName,
		List<DestructionAwareBeanPostProcessor> postProcessors) {

	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean = invokeDisposableBean;
	this.nonPublicAccessAllowed = nonPublicAccessAllowed;
	this.acc = null;
	this.destroyMethodName = destroyMethodName;
	this.beanPostProcessors = postProcessors;
}
 
Example #14
Source File: AbstractBeanFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
	Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
	this.beanPostProcessors.remove(beanPostProcessor);
	this.beanPostProcessors.add(beanPostProcessor);
	if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
		this.hasInstantiationAwareBeanPostProcessors = true;
	}
	if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
		this.hasDestructionAwareBeanPostProcessors = true;
	}
}
 
Example #15
Source File: DisposableBeanAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Serializes a copy of the state of this class,
 * filtering out non-serializable BeanPostProcessors.
 */
protected Object writeReplace() {
	List<DestructionAwareBeanPostProcessor> serializablePostProcessors = null;
	if (this.beanPostProcessors != null) {
		serializablePostProcessors = new ArrayList<DestructionAwareBeanPostProcessor>();
		for (DestructionAwareBeanPostProcessor postProcessor : this.beanPostProcessors) {
			if (postProcessor instanceof Serializable) {
				serializablePostProcessors.add(postProcessor);
			}
		}
	}
	return new DisposableBeanAdapter(this.bean, this.beanName, this.invokeDisposableBean,
			this.nonPublicAccessAllowed, this.destroyMethodName, serializablePostProcessors);
}
 
Example #16
Source File: DisposableBeanAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 */
private DisposableBeanAdapter(Object bean, String beanName, boolean invokeDisposableBean,
		boolean nonPublicAccessAllowed, String destroyMethodName,
		List<DestructionAwareBeanPostProcessor> postProcessors) {

	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean = invokeDisposableBean;
	this.nonPublicAccessAllowed = nonPublicAccessAllowed;
	this.acc = null;
	this.destroyMethodName = destroyMethodName;
	this.beanPostProcessors = postProcessors;
}
 
Example #17
Source File: AbstractBeanFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
	Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
	// Remove from old position, if any
	this.beanPostProcessors.remove(beanPostProcessor);
	// Track whether it is instantiation/destruction aware
	if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
		this.hasInstantiationAwareBeanPostProcessors = true;
	}
	if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
		this.hasDestructionAwareBeanPostProcessors = true;
	}
	// Add to end of list
	this.beanPostProcessors.add(beanPostProcessor);
}
 
Example #18
Source File: DisposableBeanAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check whether the given bean has destruction-aware post-processors applying to it.
 * @param bean the bean instance
 * @param postProcessors the post-processor candidates
 */
public static boolean hasApplicableProcessors(Object bean, List<BeanPostProcessor> postProcessors) {
	if (!CollectionUtils.isEmpty(postProcessors)) {
		for (BeanPostProcessor processor : postProcessors) {
			if (processor instanceof DestructionAwareBeanPostProcessor) {
				DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
				if (dabpp.requiresDestruction(bean)) {
					return true;
				}
			}
		}
	}
	return false;
}
 
Example #19
Source File: DisposableBeanAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Serializes a copy of the state of this class,
 * filtering out non-serializable BeanPostProcessors.
 */
protected Object writeReplace() {
	List<DestructionAwareBeanPostProcessor> serializablePostProcessors = null;
	if (this.beanPostProcessors != null) {
		serializablePostProcessors = new ArrayList<>();
		for (DestructionAwareBeanPostProcessor postProcessor : this.beanPostProcessors) {
			if (postProcessor instanceof Serializable) {
				serializablePostProcessors.add(postProcessor);
			}
		}
	}
	return new DisposableBeanAdapter(this.bean, this.beanName, this.invokeDisposableBean,
			this.nonPublicAccessAllowed, this.destroyMethodName, serializablePostProcessors);
}
 
Example #20
Source File: DisposableBeanAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new DisposableBeanAdapter for the given bean.
 */
private DisposableBeanAdapter(Object bean, String beanName, boolean invokeDisposableBean,
		boolean nonPublicAccessAllowed, @Nullable String destroyMethodName,
		@Nullable List<DestructionAwareBeanPostProcessor> postProcessors) {

	this.bean = bean;
	this.beanName = beanName;
	this.invokeDisposableBean = invokeDisposableBean;
	this.nonPublicAccessAllowed = nonPublicAccessAllowed;
	this.acc = null;
	this.destroyMethodName = destroyMethodName;
	this.beanPostProcessors = postProcessors;
}
 
Example #21
Source File: AbstractBeanFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
	Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
	// Remove from old position, if any
	this.beanPostProcessors.remove(beanPostProcessor);
	// Track whether it is instantiation/destruction aware
	if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
		this.hasInstantiationAwareBeanPostProcessors = true;
	}
	if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
		this.hasDestructionAwareBeanPostProcessors = true;
	}
	// Add to end of list
	this.beanPostProcessors.add(beanPostProcessor);
}
 
Example #22
Source File: DisposableBeanAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check whether the given bean has destruction-aware post-processors applying to it.
 * @param bean the bean instance
 * @param postProcessors the post-processor candidates
 */
public static boolean hasApplicableProcessors(Object bean, List<BeanPostProcessor> postProcessors) {
	if (!CollectionUtils.isEmpty(postProcessors)) {
		for (BeanPostProcessor processor : postProcessors) {
			if (processor instanceof DestructionAwareBeanPostProcessor) {
				DestructionAwareBeanPostProcessor dabpp = (DestructionAwareBeanPostProcessor) processor;
				if (dabpp.requiresDestruction(bean)) {
					return true;
				}
			}
		}
	}
	return false;
}
 
Example #23
Source File: DisposableBeanAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Serializes a copy of the state of this class,
 * filtering out non-serializable BeanPostProcessors.
 */
protected Object writeReplace() {
	List<DestructionAwareBeanPostProcessor> serializablePostProcessors = null;
	if (this.beanPostProcessors != null) {
		serializablePostProcessors = new ArrayList<>();
		for (DestructionAwareBeanPostProcessor postProcessor : this.beanPostProcessors) {
			if (postProcessor instanceof Serializable) {
				serializablePostProcessors.add(postProcessor);
			}
		}
	}
	return new DisposableBeanAdapter(this.bean, this.beanName, this.invokeDisposableBean,
			this.nonPublicAccessAllowed, this.destroyMethodName, serializablePostProcessors);
}
 
Example #24
Source File: DisposableBeanAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void destroy() {
	if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
		for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
			processor.postProcessBeforeDestruction(this.bean, this.beanName);
		}
	}

	if (this.invokeDisposableBean) {
		if (logger.isDebugEnabled()) {
			logger.debug("Invoking destroy() on bean with name '" + this.beanName + "'");
		}
		try {
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
					@Override
					public Object run() throws Exception {
						((DisposableBean) bean).destroy();
						return null;
					}
				}, acc);
			}
			else {
				((DisposableBean) bean).destroy();
			}
		}
		catch (Throwable ex) {
			String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
			if (logger.isDebugEnabled()) {
				logger.warn(msg, ex);
			}
			else {
				logger.warn(msg + ": " + ex);
			}
		}
	}

	if (this.destroyMethod != null) {
		invokeCustomDestroyMethod(this.destroyMethod);
	}
	else if (this.destroyMethodName != null) {
		Method methodToCall = determineDestroyMethod();
		if (methodToCall != null) {
			invokeCustomDestroyMethod(methodToCall);
		}
	}
}
 
Example #25
Source File: DisposableBeanAdapter.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy() {
	if (this.beanPostProcessors != null && !this.beanPostProcessors.isEmpty()) {
		for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
			processor.postProcessBeforeDestruction(this.bean, this.beanName);
		}
	}

	if (this.invokeDisposableBean) {
		if (logger.isDebugEnabled()) {
			logger.debug("Invoking destroy() on bean with name '" + this.beanName + "'");
		}
		try {
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
					@Override
					public Object run() throws Exception {
						((DisposableBean) bean).destroy();
						return null;
					}
				}, acc);
			}
			else {
				((DisposableBean) bean).destroy();
			}
		}
		catch (Throwable ex) {
			String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
			if (logger.isDebugEnabled()) {
				logger.warn(msg, ex);
			}
			else {
				logger.warn(msg + ": " + ex);
			}
		}
	}

	if (this.destroyMethod != null) {
		invokeCustomDestroyMethod(this.destroyMethod);
	}
	else if (this.destroyMethodName != null) {
		Method methodToCall = determineDestroyMethod();
		if (methodToCall != null) {
			invokeCustomDestroyMethod(methodToCall);
		}
	}
}
 
Example #26
Source File: DisposableBeanAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void destroy() {
	if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
		for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
			processor.postProcessBeforeDestruction(this.bean, this.beanName);
		}
	}

	if (this.invokeDisposableBean) {
		if (logger.isTraceEnabled()) {
			logger.trace("Invoking destroy() on bean with name '" + this.beanName + "'");
		}
		try {
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
					((DisposableBean) this.bean).destroy();
					return null;
				}, this.acc);
			}
			else {
				((DisposableBean) this.bean).destroy();
			}
		}
		catch (Throwable ex) {
			String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
			if (logger.isDebugEnabled()) {
				logger.info(msg, ex);
			}
			else {
				logger.info(msg + ": " + ex);
			}
		}
	}

	if (this.destroyMethod != null) {
		invokeCustomDestroyMethod(this.destroyMethod);
	}
	else if (this.destroyMethodName != null) {
		Method methodToCall = determineDestroyMethod(this.destroyMethodName);
		if (methodToCall != null) {
			invokeCustomDestroyMethod(methodToCall);
		}
	}
}
 
Example #27
Source File: DisposableBeanAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void destroy() {
	if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
		for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
			processor.postProcessBeforeDestruction(this.bean, this.beanName);
		}
	}

	if (this.invokeDisposableBean) {
		if (logger.isDebugEnabled()) {
			logger.debug("Invoking destroy() on bean with name '" + this.beanName + "'");
		}
		try {
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
					@Override
					public Object run() throws Exception {
						((DisposableBean) bean).destroy();
						return null;
					}
				}, acc);
			}
			else {
				((DisposableBean) bean).destroy();
			}
		}
		catch (Throwable ex) {
			String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
			if (logger.isDebugEnabled()) {
				logger.warn(msg, ex);
			}
			else {
				logger.warn(msg + ": " + ex);
			}
		}
	}

	if (this.destroyMethod != null) {
		invokeCustomDestroyMethod(this.destroyMethod);
	}
	else if (this.destroyMethodName != null) {
		Method methodToCall = determineDestroyMethod();
		if (methodToCall != null) {
			invokeCustomDestroyMethod(methodToCall);
		}
	}
}
 
Example #28
Source File: DisposableBeanAdapter.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void destroy() {
	if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
		for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
			processor.postProcessBeforeDestruction(this.bean, this.beanName);
		}
	}

	if (this.invokeDisposableBean) {
		if (logger.isTraceEnabled()) {
			logger.trace("Invoking destroy() on bean with name '" + this.beanName + "'");
		}
		try {
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
					((DisposableBean) this.bean).destroy();
					return null;
				}, this.acc);
			}
			else {
				((DisposableBean) this.bean).destroy();
			}
		}
		catch (Throwable ex) {
			String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";
			if (logger.isDebugEnabled()) {
				logger.info(msg, ex);
			}
			else {
				logger.info(msg + ": " + ex);
			}
		}
	}

	if (this.destroyMethod != null) {
		invokeCustomDestroyMethod(this.destroyMethod);
	}
	else if (this.destroyMethodName != null) {
		Method methodToCall = determineDestroyMethod(this.destroyMethodName);
		if (methodToCall != null) {
			invokeCustomDestroyMethod(methodToCall);
		}
	}
}