Skip to content

Commit

Permalink
fix: Clear Spring caches for beans created using createBean
Browse files Browse the repository at this point in the history
Fixes #630
  • Loading branch information
Artur- authored and skybber committed Jan 20, 2025
1 parent a86384c commit 013a4d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,44 @@ public static void resetInitDestroyAnnotationBeanPostProcessorCache(InitDestroyA
}
}

public static boolean isCached(Class<?> clazz, DefaultListableBeanFactory beanFactory) {
for (BeanPostProcessor bpp : beanFactory.getBeanPostProcessors()) {
if (bpp instanceof AutowiredAnnotationBeanPostProcessor) {
if (isCached(clazz, (AutowiredAnnotationBeanPostProcessor) bpp)) {
return true;
}
}
}
return false;
}

private static boolean isCached(Class<?> clazz, AutowiredAnnotationBeanPostProcessor bpp) {
return getAutowiredAnnotationBeanPostProcessorConstructorCache(bpp).containsKey(clazz);
}

// @Autowired cache
public static void resetAutowiredAnnotationBeanPostProcessorCache(AutowiredAnnotationBeanPostProcessor bpp) {
getAutowiredAnnotationBeanPostProcessorConstructorCache(bpp).clear();
LOGGER.trace("Cache cleared: AutowiredAnnotationBeanPostProcessor.candidateConstructorsCache");
resetAnnotationBeanPostProcessorCache(bpp, AutowiredAnnotationBeanPostProcessor.class);
}

private static Map<Class<?>, Constructor<?>[]> getAutowiredAnnotationBeanPostProcessorConstructorCache(
AutowiredAnnotationBeanPostProcessor bpp) {
try {
Field field = AutowiredAnnotationBeanPostProcessor.class.getDeclaredField("candidateConstructorsCache");
field.setAccessible(true);
// noinspection unchecked
Map<Class<?>, Constructor<?>[]> candidateConstructorsCache = (Map<Class<?>, Constructor<?>[]>) field.get(bpp);
candidateConstructorsCache.clear();
LOGGER.trace("Cache cleared: AutowiredAnnotationBeanPostProcessor.candidateConstructorsCache");
return (Map<Class<?>, Constructor<?>[]>) field.get(bpp);
} catch (Exception e) {
throw new IllegalStateException("Unable to clear AutowiredAnnotationBeanPostProcessor.candidateConstructorsCache", e);
throw new IllegalStateException(
"Unable to access AutowiredAnnotationBeanPostProcessor.candidateConstructorsCache", e);
}
resetAnnotationBeanPostProcessorCache(bpp, AutowiredAnnotationBeanPostProcessor.class);
}

/**
* deal injectionMetadataCache field of
*
* @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
* @see org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ private boolean preCheckReload() {
if ((names == null || names.length == 0) && !isFactoryMethod(clazz)) {
LOGGER.trace("the class '{}' is not spring bean or factory class", clazz.getName());
iterator.remove();

// The class might still be cached if it was created using createBean
if (ResetBeanPostProcessorCaches.isCached(clazz, beanFactory)) {
clearSpringCache();
}
} else {
LOGGER.debug("the class '{}' is spring bean or factory class", clazz.getName());
}
Expand Down

0 comments on commit 013a4d9

Please sign in to comment.