Skip to content

Commit

Permalink
Hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Jul 20, 2022
1 parent 3ae1b9b commit 35da93c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.context.support;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
Expand Down Expand Up @@ -47,6 +48,7 @@
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.core.metrics.StartupStep;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;

/**
* Delegate for AbstractApplicationContext's post-processor handling.
Expand Down Expand Up @@ -315,6 +317,7 @@ static <T extends BeanPostProcessor> List<T> loadBeanPostProcessors(
*/
static void invokeMergedBeanDefinitionPostProcessors(DefaultListableBeanFactory beanFactory) {
new MergedBeanDefinitionPostProcessorInvoker(beanFactory).invokeMergedBeanDefinitionPostProcessors();

}

private static void sortPostProcessors(List<?> postProcessors, ConfigurableListableBeanFactory beanFactory) {
Expand Down Expand Up @@ -364,7 +367,7 @@ private static void invokeBeanFactoryPostProcessors(
* Register the given BeanPostProcessor beans.
*/
private static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {
ConfigurableListableBeanFactory beanFactory, List<? extends BeanPostProcessor> postProcessors) {

if (beanFactory instanceof AbstractBeanFactory) {
// Bulk addition is more efficient against our CopyOnWriteArrayList there
Expand Down Expand Up @@ -439,12 +442,19 @@ private void invokeMergedBeanDefinitionPostProcessors() {
Class<?> beanType = resolveBeanType(bd);
postProcessRootBeanDefinition(postProcessors, beanName, beanType, bd);
}
registerBeanPostProcessors(this.beanFactory, postProcessors);
}

private void postProcessRootBeanDefinition(List<MergedBeanDefinitionPostProcessor> postProcessors,
String beanName, Class<?> beanType, RootBeanDefinition bd) {
BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, bd);
postProcessors.forEach(postProcessor -> postProcessor.postProcessMergedBeanDefinition(bd, beanType, beanName));
postProcessors.forEach(postProcessor -> {
postProcessor.postProcessMergedBeanDefinition(bd, beanType, beanName);
// FIXME: logic should move elsewhere to access package private flag
Field postProcessed = ReflectionUtils.findField(bd.getClass(), "postProcessed");
ReflectionUtils.makeAccessible(postProcessed);
ReflectionUtils.setField(postProcessed, bd, true);
});
for (PropertyValue propertyValue : bd.getPropertyValues().getPropertyValueList()) {
Object value = propertyValue.getValue();
if (value instanceof AbstractBeanDefinition innerBd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,24 @@ void refreshForAotProcessingWithConfiguration() {
"annotationConfigApplicationContextTests.Config", "testBean");
}

@Test
void refreshForAotCanInstantiateBeanWithAutowiredApplicationContext() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(BeanD.class);
context.refreshForAotProcessing();
BeanD bean = context.getBean(BeanD.class);
assertThat(bean.applicationContext).isSameAs(context);
}

@Test
void refreshForAotCanInstantiateBeanWithFieldAutowiredApplicationContext() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(BeanB.class);
context.refreshForAotProcessing();
BeanB bean = context.getBean(BeanB.class);
assertThat(bean.applicationContext).isSameAs(context);
}


@Configuration
static class Config {
Expand Down Expand Up @@ -506,6 +524,16 @@ public BeanB() {

static class BeanC {}

static class BeanD {

private final ApplicationContext applicationContext;

public BeanD(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

}

static class NonInstantiatedFactoryBean implements FactoryBean<String> {

NonInstantiatedFactoryBean() {
Expand Down

0 comments on commit 35da93c

Please sign in to comment.