Skip to content

Commit

Permalink
Treat directly registered Class as configuration 'lite' mode candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 23, 2023
1 parent e92c9c5 commit 2352d26
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
Expand Up @@ -255,6 +255,7 @@ private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name,
return;
}

abd.setAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_CANDIDATE_ATTRIBUTE, Boolean.TRUE);
abd.setInstanceSupplier(supplier);
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
Expand Down
Expand Up @@ -56,6 +56,9 @@ public abstract class ConfigurationClassUtils {

static final String CONFIGURATION_CLASS_LITE = "lite";

static final String CONFIGURATION_CLASS_CANDIDATE_ATTRIBUTE =
Conventions.getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClassCandidate");

static final String CONFIGURATION_CLASS_ATTRIBUTE =
Conventions.getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClass");

Expand Down Expand Up @@ -136,7 +139,8 @@ else if (beanDef instanceof AbstractBeanDefinition abstractBd && abstractBd.hasB
if (config != null && !Boolean.FALSE.equals(config.get("proxyBeanMethods"))) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
}
else if (config != null || isConfigurationCandidate(metadata)) {
else if (config != null || Boolean.TRUE.equals(beanDef.getAttribute(CONFIGURATION_CLASS_CANDIDATE_ATTRIBUTE)) ||
isConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
}
else {
Expand Down
@@ -0,0 +1,54 @@
/*
* 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 org.junit.jupiter.api.Test;

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

/**
* @author Sam Brannen
*/
class BeanLiteModeTests {

@Test
void test() {
try (var context = new AnnotationConfigApplicationContext(Config.class)) {
var bean1 = context.getBean("bean1", String.class);
var bean2 = context.getBean("bean2", String.class);

assertThat(bean1).isEqualTo("bean1");
assertThat(bean2).isEqualTo("bean2");
}
}

static class Config extends BaseConfig {
}

static class BaseConfig {
@Bean
String bean1() {
return "bean1";
}

@Bean
String bean2() {
return "bean2";
}
}

}

0 comments on commit 2352d26

Please sign in to comment.