Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultListableBeanFactory.getBean(Class) throws NoSuchBeanDefinitionException on existing bean if getBean(Class) previously tried before registration #24852

Closed
kochetkov-ma opened this issue Apr 3, 2020 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@kochetkov-ma
Copy link

kochetkov-ma commented Apr 3, 2020

Affects: spring-context:5.2.5.RELEASE


I register a new BeanDefinition via context.registerBeanDefinition , but I still have NoSuchBeanDefinitionException on context.getBean because context.getBean cached my previous call before bean registration.

Only if I call context.getBeanFactory().clearMetadataCache() manually this code doesn't throw any exception, but now it does.

import static org.assertj.core.api.Assertions.assertThat;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.support.GenericApplicationContext;

@Slf4j
public class DefaultListableBeanFactoryRedundantCacheTest {

    @Test
    public void noSuchBeanDefinitionExceptionAfterRegisterBean() {
        try (GenericApplicationContext context = new GenericApplicationContext()) {
            context.refresh();
            NoSuchBeanDefinitionException exception = null;
            try {
                context.getBean(Bean.class);
            } catch (NoSuchBeanDefinitionException ex) {
                exception = ex;
                log.error("It's ok. " +
                        "But 'org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType" +
                        "(java.lang.Class<?>, boolean, boolean)' has added bean to cache '#501 - cache.put(type, resolvedBeanNames);'"
                    , ex);
            }
            assertThat(exception).isInstanceOf(NoSuchBeanDefinitionException.class);

            /* No Clear Metadata Cache 'context.getBeanFactory().clearMetadataCache()' */

            final BeanDefinition definition = BeanDefinitionBuilder
                .genericBeanDefinition(Bean.class)
                .getBeanDefinition();
            context.registerBeanDefinition("myBean", definition);

            final Bean newBean = context.getBean(Bean.class);

            assertThat(newBean).isNotNull();
        }
    }
}

I think unsuccessful resolved bean shouldn't be cached in getBeanNamesForType.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 3, 2020
@jhoeller jhoeller self-assigned this Apr 3, 2020
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 3, 2020
@jhoeller jhoeller added this to the 5.2.6 milestone Apr 3, 2020
@jhoeller jhoeller changed the title DefaultListableBeanFactory.getBean(Class) throw NoSuchBeanDefinitionException on existing bean if I had already tried getBean this bean before its registration DefaultListableBeanFactory.getBean(Class) throws NoSuchBeanDefinitionException on existing bean if getBean(Class) previously tried before registration Apr 3, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x labels Apr 3, 2020
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants