Skip to content

Commit

Permalink
Populate dependencies metadata for resolved target behind lazy depend…
Browse files Browse the repository at this point in the history
…ency proxy

Closes spring-projectsgh-25562
  • Loading branch information
jhoeller authored and xcl(徐程林) committed Aug 16, 2020
1 parent 5b6a67e commit 556d134
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 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 All @@ -20,12 +20,14 @@
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver;
import org.springframework.beans.factory.config.DependencyDescriptor;
Expand Down Expand Up @@ -73,9 +75,11 @@ protected boolean isLazy(DependencyDescriptor descriptor) {
}

protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final @Nullable String beanName) {
Assert.state(getBeanFactory() instanceof DefaultListableBeanFactory,
BeanFactory beanFactory = getBeanFactory();
Assert.state(beanFactory instanceof DefaultListableBeanFactory,
"BeanFactory needs to be a DefaultListableBeanFactory");
final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
final DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;

TargetSource ts = new TargetSource() {
@Override
public Class<?> getTargetClass() {
Expand All @@ -87,7 +91,8 @@ public boolean isStatic() {
}
@Override
public Object getTarget() {
Object target = beanFactory.doResolveDependency(descriptor, beanName, null, null);
Set<String> autowiredBeanNames = (beanName != null ? new LinkedHashSet<>(1) : null);
Object target = dlbf.doResolveDependency(descriptor, beanName, autowiredBeanNames, null);
if (target == null) {
Class<?> type = getTargetClass();
if (Map.class == type) {
Expand All @@ -102,19 +107,27 @@ else if (Set.class == type || Collection.class == type) {
throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(),
"Optional dependency not present for lazy injection point");
}
if (autowiredBeanNames != null) {
for (String autowiredBeanName : autowiredBeanNames) {
if (dlbf.containsBean(autowiredBeanName)) {
dlbf.registerDependentBean(autowiredBeanName, beanName);
}
}
}
return target;
}
@Override
public void releaseTarget(Object target) {
}
};

ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
Class<?> dependencyType = descriptor.getDependencyType();
if (dependencyType.isInterface()) {
pf.addInterface(dependencyType);
}
return pf.getProxy(beanFactory.getBeanClassLoader());
return pf.getProxy(dlbf.getBeanClassLoader());
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 All @@ -26,9 +26,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.util.ObjectUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand All @@ -49,14 +51,18 @@ private void doTestLazyResourceInjection(Class<? extends TestBeanHolder> annotat
ac.registerBeanDefinition("testBean", tbd);
ac.refresh();

ConfigurableListableBeanFactory bf = ac.getBeanFactory();
TestBeanHolder bean = ac.getBean("annotatedBean", TestBeanHolder.class);
assertThat(ac.getBeanFactory().containsSingleton("testBean")).isFalse();
assertThat(bf.containsSingleton("testBean")).isFalse();
assertThat(bean.getTestBean()).isNotNull();
assertThat(bean.getTestBean().getName()).isNull();
assertThat(ac.getBeanFactory().containsSingleton("testBean")).isTrue();
assertThat(bf.containsSingleton("testBean")).isTrue();
TestBean tb = (TestBean) ac.getBean("testBean");
tb.setName("tb");
assertThat(bean.getTestBean().getName()).isSameAs("tb");

assertThat(ObjectUtils.containsElement(bf.getDependenciesForBean("annotatedBean"), "testBean")).isTrue();
assertThat(ObjectUtils.containsElement(bf.getDependentBeans("testBean"), "annotatedBean")).isTrue();
}

@Test
Expand Down

0 comments on commit 556d134

Please sign in to comment.