|
|
|
@ -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"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -20,10 +20,13 @@ import org.junit.jupiter.api.BeforeEach; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
import org.springframework.aop.support.AopUtils; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.ObjectProvider; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinitionHolder; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinitionHolder; |
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.support.ScopeNotActiveException; |
|
|
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
|
|
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
|
|
|
|
|
|
|
import org.springframework.beans.testfixture.beans.CountingTestBean; |
|
|
|
import org.springframework.beans.testfixture.beans.DerivedTestBean; |
|
|
|
import org.springframework.beans.testfixture.beans.DerivedTestBean; |
|
|
|
import org.springframework.beans.testfixture.beans.ITestBean; |
|
|
|
import org.springframework.beans.testfixture.beans.ITestBean; |
|
|
|
import org.springframework.beans.testfixture.beans.TestBean; |
|
|
|
import org.springframework.beans.testfixture.beans.TestBean; |
|
|
|
@ -32,6 +35,8 @@ import org.springframework.core.io.ClassPathResource; |
|
|
|
import org.springframework.web.testfixture.servlet.MockHttpServletRequest; |
|
|
|
import org.springframework.web.testfixture.servlet.MockHttpServletRequest; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
|
|
|
|
import static org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
@ -51,7 +56,7 @@ public class RequestScopedProxyTests { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testGetFromScope() throws Exception { |
|
|
|
public void testGetFromScope() { |
|
|
|
String name = "requestScopedObject"; |
|
|
|
String name = "requestScopedObject"; |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean(name); |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean(name); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean)).isTrue(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean)).isTrue(); |
|
|
|
@ -76,7 +81,7 @@ public class RequestScopedProxyTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testGetFromScopeThroughDynamicProxy() throws Exception { |
|
|
|
public void testGetFromScopeThroughDynamicProxy() { |
|
|
|
String name = "requestScopedProxy"; |
|
|
|
String name = "requestScopedProxy"; |
|
|
|
ITestBean bean = (ITestBean) this.beanFactory.getBean(name); |
|
|
|
ITestBean bean = (ITestBean) this.beanFactory.getBean(name); |
|
|
|
// assertTrue(AopUtils.isJdkDynamicProxy(bean));
|
|
|
|
// assertTrue(AopUtils.isJdkDynamicProxy(bean));
|
|
|
|
@ -101,7 +106,7 @@ public class RequestScopedProxyTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testDestructionAtRequestCompletion() throws Exception { |
|
|
|
public void testDestructionAtRequestCompletion() { |
|
|
|
String name = "requestScopedDisposableObject"; |
|
|
|
String name = "requestScopedDisposableObject"; |
|
|
|
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); |
|
|
|
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean)).isTrue(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean)).isTrue(); |
|
|
|
@ -127,7 +132,7 @@ public class RequestScopedProxyTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testGetFromFactoryBeanInScope() throws Exception { |
|
|
|
public void testGetFromFactoryBeanInScope() { |
|
|
|
String name = "requestScopedFactoryBean"; |
|
|
|
String name = "requestScopedFactoryBean"; |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean(name); |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean(name); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean)).isTrue(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean)).isTrue(); |
|
|
|
@ -149,7 +154,7 @@ public class RequestScopedProxyTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testGetInnerBeanFromScope() throws Exception { |
|
|
|
public void testGetInnerBeanFromScope() { |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); |
|
|
|
assertThat(AopUtils.isAopProxy(bean)).isFalse(); |
|
|
|
assertThat(AopUtils.isAopProxy(bean)).isFalse(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean.getSpouse())).isTrue(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean.getSpouse())).isTrue(); |
|
|
|
@ -173,7 +178,7 @@ public class RequestScopedProxyTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testGetAnonymousInnerBeanFromScope() throws Exception { |
|
|
|
public void testGetAnonymousInnerBeanFromScope() { |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); |
|
|
|
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); |
|
|
|
assertThat(AopUtils.isAopProxy(bean)).isFalse(); |
|
|
|
assertThat(AopUtils.isAopProxy(bean)).isFalse(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean.getSpouse())).isTrue(); |
|
|
|
assertThat(AopUtils.isCglibProxy(bean.getSpouse())).isTrue(); |
|
|
|
@ -199,4 +204,31 @@ public class RequestScopedProxyTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void scopeNotAvailable() { |
|
|
|
|
|
|
|
assertThatExceptionOfType(ScopeNotActiveException.class).isThrownBy( |
|
|
|
|
|
|
|
() -> this.beanFactory.getBean(CountingTestBean.class).absquatulate()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final ObjectProvider<CountingTestBean> beanProvider = this.beanFactory.getBeanProvider(CountingTestBean.class); |
|
|
|
|
|
|
|
assertThatExceptionOfType(ScopeNotActiveException.class).isThrownBy(() -> beanProvider.getObject().absquatulate()); |
|
|
|
|
|
|
|
beanProvider.ifAvailable(TestBean::absquatulate); |
|
|
|
|
|
|
|
beanProvider.ifUnique(TestBean::absquatulate); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final ObjectProvider<CountingTestBean> provider = |
|
|
|
|
|
|
|
((ProviderBean) this.beanFactory.createBean(ProviderBean.class, AUTOWIRE_CONSTRUCTOR, false)).provider; |
|
|
|
|
|
|
|
assertThatExceptionOfType(ScopeNotActiveException.class).isThrownBy(() -> provider.getObject().absquatulate()); |
|
|
|
|
|
|
|
provider.ifAvailable(TestBean::absquatulate); |
|
|
|
|
|
|
|
provider.ifUnique(TestBean::absquatulate); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class ProviderBean { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ObjectProvider<CountingTestBean> provider; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ProviderBean(ObjectProvider<CountingTestBean> provider) { |
|
|
|
|
|
|
|
this.provider = provider; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|