Skip to content

Commit c63d72b

Browse files
Retrieve Environment from RegisteredBean.
1 parent 5aa7f58 commit c63d72b

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
import org.springframework.context.EnvironmentAware;
3434
import org.springframework.core.ResolvableType;
3535
import org.springframework.core.env.Environment;
36+
import org.springframework.core.env.EnvironmentCapable;
3637
import org.springframework.core.env.StandardEnvironment;
3738
import org.springframework.data.domain.ManagedTypes;
3839
import org.springframework.data.util.Lazy;
3940
import org.springframework.data.util.TypeCollector;
4041
import org.springframework.data.util.TypeContributor;
4142
import org.springframework.data.util.TypeUtils;
4243
import org.springframework.util.ClassUtils;
44+
import org.springframework.util.ObjectUtils;
4345
import org.springframework.util.StringUtils;
4446

4547
/**
@@ -54,7 +56,8 @@ public class ManagedTypesBeanRegistrationAotProcessor implements BeanRegistratio
5456

5557
private final Log logger = LogFactory.getLog(getClass());
5658
private @Nullable String moduleIdentifier;
57-
private Lazy<Environment> environment = Lazy.of(StandardEnvironment::new);
59+
private static final Lazy<Environment> DEFAULT_ENVIRONMENT = Lazy.of(StandardEnvironment::new);
60+
private @Nullable Environment environment = null;
5861

5962
public void setModuleIdentifier(@Nullable String moduleIdentifier) {
6063
this.moduleIdentifier = moduleIdentifier;
@@ -67,7 +70,7 @@ public String getModuleIdentifier() {
6770

6871
@Override
6972
public void setEnvironment(Environment environment) {
70-
this.environment = Lazy.of(environment);
73+
this.environment = environment;
7174
}
7275

7376
@Override
@@ -77,7 +80,7 @@ public void setEnvironment(Environment environment) {
7780
return null;
7881
}
7982

80-
DefaultAotContext aotContext = new DefaultAotContext(registeredBean.getBeanFactory(), environment.get());
83+
DefaultAotContext aotContext = new DefaultAotContext(registeredBean.getBeanFactory(), getConfiguredEnvironmentOrTryToResolveOne(registeredBean));
8184
return contribute(aotContext, resolveManagedTypes(registeredBean), registeredBean);
8285
}
8386

@@ -183,4 +186,21 @@ protected boolean matchesByType(@Nullable Class<?> beanType) {
183186
protected boolean matchesPrefix(@Nullable String beanName) {
184187
return StringUtils.startsWithIgnoreCase(beanName, getModuleIdentifier());
185188
}
189+
190+
protected Environment getConfiguredEnvironmentOrTryToResolveOne(RegisteredBean registeredBean) {
191+
192+
if (this.environment != null) {
193+
return this.environment;
194+
}
195+
196+
if (registeredBean.getBeanFactory() instanceof EnvironmentCapable ec) {
197+
return ec.getEnvironment();
198+
} else {
199+
String[] beanNamesForType = registeredBean.getBeanFactory().getBeanNamesForType(Environment.class);
200+
if (!ObjectUtils.isEmpty(beanNamesForType)) {
201+
return registeredBean.getBeanFactory().getBean(beanNamesForType[0], Environment.class);
202+
}
203+
}
204+
return DEFAULT_ENVIRONMENT.get();
205+
}
186206
}

src/test/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessorUnitTests.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@
4141
import org.springframework.beans.factory.support.RootBeanDefinition;
4242
import org.springframework.context.annotation.Bean;
4343
import org.springframework.context.annotation.Configuration;
44+
import org.springframework.core.env.Environment;
4445
import org.springframework.data.aot.ManagedTypesRegistrationAotContribution.ManagedTypesInstanceCodeFragment;
4546
import org.springframework.data.domain.ManagedTypes;
4647
import org.springframework.javapoet.MethodSpec;
4748
import org.springframework.javapoet.MethodSpec.Builder;
49+
import org.springframework.mock.env.MockEnvironment;
4850
import org.springframework.test.util.ReflectionTestUtils;
4951

5052
/**
@@ -198,15 +200,15 @@ void generatesInstanceSupplierCodeFragmentToAvoidDuplicateInvocations() {
198200
@Test // GH-2680
199201
void generatesInstanceSupplierCodeFragmentToAvoidDuplicateInvocationsForEmptyManagedTypes() {
200202

201-
beanFactory.registerBeanDefinition("commons.managed-types", BeanDefinitionBuilder.rootBeanDefinition(EmptyManagedTypes.class).getBeanDefinition());
203+
beanFactory.registerBeanDefinition("commons.managed-types",
204+
BeanDefinitionBuilder.rootBeanDefinition(EmptyManagedTypes.class).getBeanDefinition());
202205
RegisteredBean registeredBean = RegisteredBean.of(beanFactory, "commons.managed-types");
203206

204207
BeanRegistrationAotContribution contribution = createPostProcessor("commons")
205208
.processAheadOfTime(RegisteredBean.of(beanFactory, "commons.managed-types"));
206209

207210
AotTestCodeContributionBuilder.withContextFor(this.getClass()).writeContentFor(contribution).compile(it -> {
208211

209-
210212
InstanceSupplier<ManagedTypes> types = ReflectionTestUtils
211213
.invokeMethod(it.getAllCompiledClasses().iterator().next(), "instance");
212214
try {
@@ -266,6 +268,40 @@ void canGenerateCodeReturnsFalseIfNoFactoryMethodPresent() {
266268
assertThat(fragment.canGenerateCode()).isFalse();
267269
}
268270

271+
@Test // GH-3414
272+
void usesConfiguredEnvironment() {
273+
274+
MockEnvironment env = spy(new MockEnvironment());
275+
ManagedTypesBeanRegistrationAotProcessor processor = createPostProcessor("commons");
276+
processor.setEnvironment(env);
277+
278+
beanFactory.registerBeanDefinition("commons.managed-types", managedTypesDefinition);
279+
280+
BeanRegistrationAotContribution contribution = processor
281+
.processAheadOfTime(RegisteredBean.of(beanFactory, "commons.managed-types"));
282+
283+
contribution.applyTo(new TestGenerationContext(Object.class), null);
284+
285+
verify(env).getProperty(eq("spring.aot.data.accessors.enabled"), eq(Boolean.class), eq(true));
286+
}
287+
288+
@Test // GH-3414
289+
void usesUsesEnvironmentFromBeanIfNotSet() {
290+
291+
MockEnvironment env = spy(new MockEnvironment());
292+
ManagedTypesBeanRegistrationAotProcessor processor = createPostProcessor("commons");
293+
294+
beanFactory.registerBeanDefinition("commons.managed-types", managedTypesDefinition);
295+
beanFactory.registerBeanDefinition("environment", new RootBeanDefinition(Environment.class, () -> env));
296+
297+
BeanRegistrationAotContribution contribution = processor
298+
.processAheadOfTime(RegisteredBean.of(beanFactory, "commons.managed-types"));
299+
300+
contribution.applyTo(new TestGenerationContext(Object.class), null);
301+
302+
verify(env).getProperty(eq("spring.aot.data.accessors.enabled"), eq(Boolean.class), eq(true));
303+
}
304+
269305
private ManagedTypesBeanRegistrationAotProcessor createPostProcessor(String moduleIdentifier) {
270306
ManagedTypesBeanRegistrationAotProcessor postProcessor = new ManagedTypesBeanRegistrationAotProcessor();
271307
postProcessor.setModuleIdentifier(moduleIdentifier);

0 commit comments

Comments
 (0)