Skip to content

Commit

Permalink
Retain metadata during bean creation even with cacheBeanMetadata=false
Browse files Browse the repository at this point in the history
Closes gh-23795
Closes gh-25749
  • Loading branch information
jhoeller committed Aug 3, 2023
1 parent c942c04 commit c3e18bc
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ else if (mbd.isPrototype()) {
}
finally {
beanCreation.end();
if (!isCacheBeanMetadata()) {
clearMergedBeanDefinition(beanName);
}
}
}

Expand Down Expand Up @@ -583,7 +586,6 @@ else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
Class<?>[] typesToMatch = (FactoryBean.class == classToMatch ?
new Class<?>[] {classToMatch} : new Class<?>[] {FactoryBean.class, classToMatch});


// Attempt to predict the bean type
Class<?> predictedType = null;

Expand Down Expand Up @@ -1409,7 +1411,7 @@ protected RootBeanDefinition getMergedBeanDefinition(

// Cache the merged bean definition for the time being
// (it might still get re-merged later on in order to pick up metadata changes)
if (containingBd == null && isCacheBeanMetadata()) {
if (containingBd == null && (isCacheBeanMetadata() || isBeanEligibleForMetadataCaching(beanName))) {
this.mergedBeanDefinitions.put(beanName, mbd);
}
}
Expand All @@ -1433,6 +1435,9 @@ private void copyRelevantMergedBeanDefinitionCaches(RootBeanDefinition previous,
mbd.factoryMethodReturnType = previous.factoryMethodReturnType;
mbd.factoryMethodToIntrospect = previous.factoryMethodToIntrospect;
}
if (previous.hasMethodOverrides()) {
mbd.setMethodOverrides(new MethodOverrides(previous.getMethodOverrides()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,7 +53,6 @@ public void setup() {
@Test
public void testWithoutConstructorArg() {
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
assertThat(bean).isNotNull();
Object expected = bean.get();
assertThat(expected.getClass()).isEqualTo(TestBean.class);
assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean);
Expand All @@ -62,7 +61,6 @@ public void testWithoutConstructorArg() {
@Test
public void testWithOverloadedArg() {
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
assertThat(bean).isNotNull();
TestBean expected = bean.get("haha");
assertThat(expected.getClass()).isEqualTo(TestBean.class);
assertThat(expected.getName()).isEqualTo("haha");
Expand All @@ -72,7 +70,6 @@ public void testWithOverloadedArg() {
@Test
public void testWithOneConstructorArg() {
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
assertThat(bean).isNotNull();
TestBean expected = bean.getOneArgument("haha");
assertThat(expected.getClass()).isEqualTo(TestBean.class);
assertThat(expected.getName()).isEqualTo("haha");
Expand All @@ -82,7 +79,6 @@ public void testWithOneConstructorArg() {
@Test
public void testWithTwoConstructorArg() {
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
assertThat(bean).isNotNull();
TestBean expected = bean.getTwoArguments("haha", 72);
assertThat(expected.getClass()).isEqualTo(TestBean.class);
assertThat(expected.getName()).isEqualTo("haha");
Expand All @@ -93,7 +89,6 @@ public void testWithTwoConstructorArg() {
@Test
public void testWithThreeArgsShouldFail() {
AbstractBean bean = (AbstractBean) beanFactory.getBean("abstractBean");
assertThat(bean).isNotNull();
assertThatExceptionOfType(AbstractMethodError.class).as("TestBean has no three arg constructor").isThrownBy(() ->
bean.getThreeArguments("name", 1, 2));
assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean);
Expand All @@ -102,7 +97,6 @@ public void testWithThreeArgsShouldFail() {
@Test
public void testWithEarlyInjection() {
AbstractBean bean = beanFactory.getBean("beanConsumer", BeanConsumer.class).abstractBean;
assertThat(bean).isNotNull();
Object expected = bean.get();
assertThat(expected.getClass()).isEqualTo(TestBean.class);
assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean);
Expand All @@ -115,7 +109,6 @@ public void testWithNullBean() {
beanFactory.registerBeanDefinition("testBean", tbd);

AbstractBean bean = beanFactory.getBean("beanConsumer", BeanConsumer.class).abstractBean;
assertThat(bean).isNotNull();
Object expected = bean.get();
assertThat(expected).isNull();
assertThat(beanFactory.getBean(BeanConsumer.class).abstractBean).isSameAs(bean);
Expand All @@ -128,7 +121,36 @@ public void testWithGenericBean() {
beanFactory.registerBeanDefinition("floatStore", new RootBeanDefinition(FloatStore.class));

NumberBean bean = (NumberBean) beanFactory.getBean("numberBean");
assertThat(bean).isNotNull();
assertThat(beanFactory.getBean(DoubleStore.class)).isSameAs(bean.getDoubleStore());
assertThat(beanFactory.getBean(FloatStore.class)).isSameAs(bean.getFloatStore());
}

@Test
public void testSingletonWithoutMetadataCaching() {
beanFactory.setCacheBeanMetadata(false);

beanFactory.registerBeanDefinition("numberBean", new RootBeanDefinition(NumberBean.class));
beanFactory.registerBeanDefinition("doubleStore", new RootBeanDefinition(DoubleStore.class));
beanFactory.registerBeanDefinition("floatStore", new RootBeanDefinition(FloatStore.class));

NumberBean bean = (NumberBean) beanFactory.getBean("numberBean");
assertThat(beanFactory.getBean(DoubleStore.class)).isSameAs(bean.getDoubleStore());
assertThat(beanFactory.getBean(FloatStore.class)).isSameAs(bean.getFloatStore());
}

@Test
public void testPrototypeWithoutMetadataCaching() {
beanFactory.setCacheBeanMetadata(false);

beanFactory.registerBeanDefinition("numberBean", new RootBeanDefinition(NumberBean.class, BeanDefinition.SCOPE_PROTOTYPE, null));
beanFactory.registerBeanDefinition("doubleStore", new RootBeanDefinition(DoubleStore.class));
beanFactory.registerBeanDefinition("floatStore", new RootBeanDefinition(FloatStore.class));

NumberBean bean = (NumberBean) beanFactory.getBean("numberBean");
assertThat(beanFactory.getBean(DoubleStore.class)).isSameAs(bean.getDoubleStore());
assertThat(beanFactory.getBean(FloatStore.class)).isSameAs(bean.getFloatStore());

bean = (NumberBean) beanFactory.getBean("numberBean");
assertThat(beanFactory.getBean(DoubleStore.class)).isSameAs(bean.getDoubleStore());
assertThat(beanFactory.getBean(FloatStore.class)).isSameAs(bean.getFloatStore());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.context.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.type.AnnotationMetadata;

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

/**
* @author Andy Wilkinson
*/
class FactoryMethodResolutionTests {

@Test
void factoryMethodCanBeResolvedWithBeanMetadataCachingEnabled() {
assertThatFactoryMethodCanBeResolved(true);
}

@Test
void factoryMethodCanBeResolvedWithBeanMetadataCachingDisabled() {
assertThatFactoryMethodCanBeResolved(false);
}

private void assertThatFactoryMethodCanBeResolved(boolean cache) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.getBeanFactory().setCacheBeanMetadata(cache);
context.register(ImportSelectorConfiguration.class);
context.refresh();
BeanDefinition definition = context.getBeanFactory().getMergedBeanDefinition("exampleBean");
assertThat(((RootBeanDefinition)definition).getResolvedFactoryMethod()).isNotNull();
}
}


@Configuration
@Import(ExampleImportSelector.class)
static class ImportSelectorConfiguration {
}


static class ExampleImportSelector implements ImportSelector {

@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] { TestConfiguration.class.getName() };
}
}


@Configuration
static class TestConfiguration {

@Bean
@ExampleAnnotation
public ExampleBean exampleBean() {
return new ExampleBean();
}
}


static class ExampleBean {
}


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface ExampleAnnotation {
}

}

0 comments on commit c3e18bc

Please sign in to comment.