use of org.springframework.beans.factory.config.BeanPostProcessor in project HotswapAgent by HotswapProjects.
the class ResetBeanPostProcessorCaches method reset.
/**
* Reset all post processors associated with a bean factory.
*
* @param beanFactory beanFactory to use
*/
public static void reset(DefaultListableBeanFactory beanFactory) {
Class<?> c = getReflectionUtilsClassOrNull();
if (c != null) {
try {
Method m = c.getDeclaredMethod("clearCache");
m.invoke(c);
} catch (Exception version42Failed) {
try {
// spring 4.0.x, 4.1.x without clearCache method, clear manually
Field declaredMethodsCache = c.getDeclaredField("declaredMethodsCache");
declaredMethodsCache.setAccessible(true);
((Map) declaredMethodsCache.get(null)).clear();
Field declaredFieldsCache = c.getDeclaredField("declaredFieldsCache");
declaredFieldsCache.setAccessible(true);
((Map) declaredFieldsCache.get(null)).clear();
} catch (Exception version40Failed) {
LOGGER.debug("Failed to clear internal method/field cache, it's normal with spring 4.1x or lower", version40Failed);
}
}
LOGGER.trace("Cleared Spring 4.2+ internal method/field cache.");
}
for (BeanPostProcessor bpp : beanFactory.getBeanPostProcessors()) {
if (bpp instanceof AutowiredAnnotationBeanPostProcessor) {
resetAutowiredAnnotationBeanPostProcessorCache((AutowiredAnnotationBeanPostProcessor) bpp);
} else if (bpp instanceof InitDestroyAnnotationBeanPostProcessor) {
resetInitDestroyAnnotationBeanPostProcessorCache((InitDestroyAnnotationBeanPostProcessor) bpp);
}
}
}
use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method beanPostProcessorWithWrappedObjectAndCloseable.
@Test
void beanPostProcessorWithWrappedObjectAndCloseable() {
RootBeanDefinition bd = new RootBeanDefinition(BeanWithCloseable.class);
lbf.registerBeanDefinition("test", bd);
lbf.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return new TestBean();
}
});
BeanWithDisposableBean.closed = false;
lbf.preInstantiateSingletons();
lbf.destroySingletons();
assertThat(BeanWithCloseable.closed).as("Destroy method invoked").isTrue();
}
use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method beanPostProcessorWithWrappedObjectAndDisposableBean.
@Test
void beanPostProcessorWithWrappedObjectAndDisposableBean() {
RootBeanDefinition bd = new RootBeanDefinition(BeanWithDisposableBean.class);
lbf.registerBeanDefinition("test", bd);
lbf.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return new TestBean();
}
});
BeanWithDisposableBean.closed = false;
lbf.preInstantiateSingletons();
lbf.destroySingletons();
assertThat(BeanWithDisposableBean.closed).as("Destroy method invoked").isTrue();
}
use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.
the class TestBean method nullReturningBeanPostProcessor.
@Test
void nullReturningBeanPostProcessor() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AutowiredConfig.class);
context.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return (bean instanceof TestBean ? null : bean);
}
});
context.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
bean.getClass().getName();
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
bean.getClass().getName();
return bean;
}
});
context.refresh();
}
use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.
the class XmlListableBeanFactoryTests method setup.
@BeforeEach
public void setup() {
parent = new DefaultListableBeanFactory();
Map map = new HashMap();
map.put("name", "Albert");
RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
bd1.setPropertyValues(new MutablePropertyValues(map));
parent.registerBeanDefinition("father", bd1);
map = new HashMap();
map.put("name", "Roderick");
RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
bd2.setPropertyValues(new MutablePropertyValues(map));
parent.registerBeanDefinition("rod", bd2);
this.factory = new DefaultListableBeanFactory(parent);
new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
this.factory.addBeanPostProcessor(new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
if (bean instanceof TestBean) {
((TestBean) bean).setPostProcessed(true);
}
if (bean instanceof DummyFactory) {
((DummyFactory) bean).setPostProcessed(true);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
return bean;
}
});
this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
// this.factory.preInstantiateSingletons();
}
Aggregations