本文整理匯總了Java中org.springframework.beans.factory.DisposableBean類的典型用法代碼示例。如果您正苦於以下問題:Java DisposableBean類的具體用法?Java DisposableBean怎麽用?Java DisposableBean使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DisposableBean類屬於org.springframework.beans.factory包,在下文中一共展示了DisposableBean類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: ManagedFactoryDisposableInvoker
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* Constructs a new <code>ManagedFactoryDisposableAdapter</code> instance.
*
* @param methodName destruction method name
*/
public ManagedFactoryDisposableInvoker(Class<?> beanClass, String methodName) {
this.isDisposable = DisposableBean.class.isAssignableFrom(beanClass);
if (StringUtils.hasText(methodName)) {
this.customSpringMethod = detectCustomSpringMethod(beanClass, methodName);
if (customSpringMethod != null) {
Class<?>[] types = customSpringMethod.getParameterTypes();
this.customSpringMethodArgs = ((types.length == 1 && types[0].equals(boolean.class)) ? new Object[] { Boolean.TRUE }
: null);
}
else {
this.customSpringMethodArgs = null;
}
this.customOsgiDestructionMethod = detectCustomOsgiMethod(beanClass, methodName);
}
else {
this.customSpringMethod = null;
this.customSpringMethodArgs = null;
this.customOsgiDestructionMethod = null;
}
}
示例2: destroy
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
public void destroy(String beanName, Object beanInstance, DestructionCodes code) {
// first invoke disposable bean
if (isDisposable) {
if (log.isDebugEnabled()) {
log.debug("Invoking destroy() on bean with name '" + beanName + "'");
}
try {
((DisposableBean) beanInstance).destroy();
}
catch (Throwable ex) {
String msg = "Invocation of destroy method failed on bean with name '" + beanName + "'";
if (log.isDebugEnabled()) {
log.warn(msg, ex);
}
else {
log.warn(msg + ": " + ex);
}
}
}
// custom callback (no argument)
invokeCustomMethod(beanName, beanInstance);
// custom callback (int argument)
invokeCustomMethod(beanName, beanInstance, code);
}
示例3: testAppContextClassHierarchy
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
public void testAppContextClassHierarchy() {
Class<?>[] clazz =
ClassUtils.getClassHierarchy(OsgiBundleXmlApplicationContext.class, ClassUtils.ClassSet.ALL_CLASSES);
//Closeable.class,
Class<?>[] expected =
new Class<?>[] { OsgiBundleXmlApplicationContext.class,
AbstractDelegatedExecutionApplicationContext.class, AbstractOsgiBundleApplicationContext.class,
AbstractRefreshableApplicationContext.class, AbstractApplicationContext.class,
DefaultResourceLoader.class, ResourceLoader.class,
AutoCloseable.class,
DelegatedExecutionOsgiBundleApplicationContext.class,
ConfigurableOsgiBundleApplicationContext.class, ConfigurableApplicationContext.class,
ApplicationContext.class, Lifecycle.class, Closeable.class, EnvironmentCapable.class, ListableBeanFactory.class,
HierarchicalBeanFactory.class, ApplicationEventPublisher.class, ResourcePatternResolver.class,
MessageSource.class, BeanFactory.class, DisposableBean.class };
assertTrue(compareArrays(expected, clazz));
}
示例4: cleanupAttributes
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
示例5: inferDestroyMethodIfNecessary
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* If the current value of the given beanDefinition's "destroyMethodName" property is
* {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
* Candidate methods are currently limited to public, no-arg methods named "close"
* (whether declared locally or inherited). The given BeanDefinition's
* "destroyMethodName" is updated to be null if no such method is found, otherwise set
* to the name of the inferred method. This constant serves as the default for the
* {@code @Bean#destroyMethod} attribute and the value of the constant may also be
* used in XML within the {@code <bean destroy-method="">} or {@code
* <beans default-destroy-method="">} attributes.
* <p>Also processes the {@link java.io.Closeable} and {@link java.lang.AutoCloseable}
* interfaces, reflectively calling the "close" method on implementing beans as well.
*/
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
if (AbstractBeanDefinition.INFER_METHOD.equals(beanDefinition.getDestroyMethodName()) ||
(beanDefinition.getDestroyMethodName() == null && closeableInterface.isInstance(bean))) {
// Only perform destroy method inference or Closeable detection
// in case of the bean not explicitly implementing DisposableBean
if (!(bean instanceof DisposableBean)) {
try {
return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
}
catch (NoSuchMethodException ex) {
try {
return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
}
catch (NoSuchMethodException ex2) {
// no candidate destroy method found
}
}
}
return null;
}
return beanDefinition.getDestroyMethodName();
}
示例6: destroyPrototypeInstance
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* Subclasses should call this method to destroy an obsolete prototype instance.
* @param target the bean instance to destroy
*/
protected void destroyPrototypeInstance(Object target) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
}
if (getBeanFactory() instanceof ConfigurableBeanFactory) {
((ConfigurableBeanFactory) getBeanFactory()).destroyBean(getTargetBeanName(), target);
}
else if (target instanceof DisposableBean) {
try {
((DisposableBean) target).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of bean with name '" + getTargetBeanName() + "'", ex);
}
}
}
示例7: inferDestroyMethodIfNecessary
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* If the current value of the given beanDefinition's "destroyMethodName" property is
* {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
* Candidate methods are currently limited to public, no-arg methods named "close" or
* "shutdown" (whether declared locally or inherited). The given BeanDefinition's
* "destroyMethodName" is updated to be null if no such method is found, otherwise set
* to the name of the inferred method. This constant serves as the default for the
* {@code @Bean#destroyMethod} attribute and the value of the constant may also be
* used in XML within the {@code <bean destroy-method="">} or {@code
* <beans default-destroy-method="">} attributes.
* <p>Also processes the {@link java.io.Closeable} and {@link java.lang.AutoCloseable}
* interfaces, reflectively calling the "close" method on implementing beans as well.
*/
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
String destroyMethodName = beanDefinition.getDestroyMethodName();
if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName) ||
(destroyMethodName == null && closeableInterface.isInstance(bean))) {
// Only perform destroy method inference or Closeable detection
// in case of the bean not explicitly implementing DisposableBean
if (!(bean instanceof DisposableBean)) {
try {
return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
}
catch (NoSuchMethodException ex) {
try {
return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
}
catch (NoSuchMethodException ex2) {
// no candidate destroy method found
}
}
}
return null;
}
return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
}
示例8: contextDestroyed
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
DisambiguationMainService.getInstance().shutDownDisambiguationService();
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(event
.getServletContext());
}
ServletContext sc = event.getServletContext();
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
} catch (Throwable ex) {
Logger.getRootLogger().fatal(ex.getMessage());
}
}
}
}
}
示例9: close
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* Tries to "close" an object.
* <p>
* This invokes the close method if it is present.
*
* @param obj the object, null ignored
*/
public static void close(final Object obj) {
if (obj != null) {
try {
if (obj instanceof Closeable) {
((Closeable) obj).close();
} else if (obj instanceof Lifecycle) {
((Lifecycle) obj).stop();
} else if (obj instanceof DisposableBean) {
((DisposableBean) obj).destroy();
} else {
invokeNoArgsNoException(obj, "close");
invokeNoArgsNoException(obj, "stop");
invokeNoArgsNoException(obj, "shutdown");
}
} catch (Exception ex) {
// ignored
}
}
}
示例10: waitForTaskExecutorToFinish
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* If an AsyncTaskExecutor is used for loading options/services/transforms, we need to wait for the tasks to complete
* before we e.g. release the DatabaseClient.
*/
public void waitForTaskExecutorToFinish() {
if (shutdownTaskExecutorAfterLoadingModules) {
if (taskExecutor instanceof ExecutorConfigurationSupport) {
((ExecutorConfigurationSupport) taskExecutor).shutdown();
taskExecutor = null;
} else if (taskExecutor instanceof DisposableBean) {
try {
((DisposableBean) taskExecutor).destroy();
} catch (Exception ex) {
logger.warn("Unexpected exception while calling destroy() on taskExecutor: " + ex.getMessage(), ex);
}
taskExecutor = null;
}
} else if (logger.isDebugEnabled()) {
logger.debug("shutdownTaskExecutorAfterLoadingModules is set to false, so not shutting down taskExecutor");
}
}
示例11: onTearDown
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
*/
protected void onTearDown() throws Exception {
if (this.HotBeanModuleRepository1 instanceof AbstractHotBeanModuleRepository) ((AbstractHotBeanModuleRepository) this.HotBeanModuleRepository1)
.destroy();
else if (this.HotBeanModuleRepository1 instanceof DisposableBean)
((DisposableBean) this.HotBeanModuleRepository1).destroy();
if (this.HotBeanModuleRepository2 instanceof AbstractHotBeanModuleRepository) ((AbstractHotBeanModuleRepository) this.HotBeanModuleRepository2)
.destroy();
else if (this.HotBeanModuleRepository2 instanceof DisposableBean)
((DisposableBean) this.HotBeanModuleRepository2).destroy();
logger.info("Cleaning up files.");
// Clean up
// FileDeletor.deleteTreeImpl("test/junit/hotModules");
logger.info("Clean up complete.");
}
示例12: cleanupAttributes
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = (String) attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
示例13: createCache_withMockedRedisConnectionFactory_createsAndDestroysConnectionFactory
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
@Test
public void createCache_withMockedRedisConnectionFactory_createsAndDestroysConnectionFactory() throws Exception {
//Arrange
RedisConnectionFactory connectionFactory = Mockito.mock(RedisConnectionFactory.class, Mockito.withSettings().extraInterfaces(DisposableBean.class));
RedisCacheFactory redisCacheFactory = new RedisCacheFactory() {
@Override
protected RedisConnectionFactory createConnectionClient(String hostName, int port) {
assertEquals("someHost", hostName);
assertEquals(4711, port);
return connectionFactory;
}
};
//Act
Cache cache = redisCacheFactory.createCache("test", "someHost", 4711);
redisCacheFactory.destroy();
//Assert
assertNotNull(cache);
assertEquals("test", cache.getName());
assertNotNull(cache.getNativeCache());
DisposableBean disposableBean = (DisposableBean) connectionFactory;
Mockito.verify(disposableBean, Mockito.times(1)).destroy();
}
示例14: testAppContextClassHierarchy
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
public void testAppContextClassHierarchy() {
Class[] clazz = ClassUtils.getClassHierarchy(OsgiBundleXmlApplicationContext.class,
ClassUtils.INCLUDE_ALL_CLASSES);
Class[] expected = new Class[] { OsgiBundleXmlApplicationContext.class,
AbstractDelegatedExecutionApplicationContext.class, DelegatedExecutionOsgiBundleApplicationContext.class,
ConfigurableOsgiBundleApplicationContext.class, ConfigurableApplicationContext.class, ApplicationContext.class,
Lifecycle.class, Closeable.class, EnvironmentCapable.class, ListableBeanFactory.class,
HierarchicalBeanFactory.class, MessageSource.class, ApplicationEventPublisher.class,
ResourcePatternResolver.class, BeanFactory.class, ResourceLoader.class, AutoCloseable.class,
AbstractOsgiBundleApplicationContext.class, AbstractRefreshableApplicationContext.class,
AbstractApplicationContext.class, DisposableBean.class, DefaultResourceLoader.class };
String msg = "Class: ";
for (int i=0;i<clazz.length;i++) {
msg += clazz[i].getSimpleName() + " ";
}
assertTrue(msg, compareArrays(expected, clazz));
}
示例15: destroy
import org.springframework.beans.factory.DisposableBean; //導入依賴的package包/類
@Override
public void destroy() throws Exception {
if (executor instanceof DisposableBean) {
DisposableBean bean = (DisposableBean) executor;
bean.destroy();
}
}