use of org.springframework.aop.scope.ScopedObject in project spring-framework by spring-projects.
the class ClassPathFactoryBeanDefinitionScannerTests method testSingletonScopedFactoryMethod.
@Test
public void testSingletonScopedFactoryMethod() {
GenericApplicationContext context = new GenericApplicationContext();
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
context.getBeanFactory().registerScope("request", new SimpleMapScope());
scanner.scan(BASE_PACKAGE);
context.registerBeanDefinition("clientBean", new RootBeanDefinition(QualifiedClientBean.class));
context.refresh();
FactoryMethodComponent fmc = context.getBean("factoryMethodComponent", FactoryMethodComponent.class);
assertThat(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)).isFalse();
// 2
TestBean tb = (TestBean) context.getBean("publicInstance");
assertThat(tb.getName()).isEqualTo("publicInstance");
// 2
TestBean tb2 = (TestBean) context.getBean("publicInstance");
assertThat(tb2.getName()).isEqualTo("publicInstance");
assertThat(tb).isSameAs(tb2);
// 3
tb = (TestBean) context.getBean("protectedInstance");
assertThat(tb.getName()).isEqualTo("protectedInstance");
assertThat(context.getBean("protectedInstance")).isSameAs(tb);
assertThat(tb.getCountry()).isEqualTo("0");
// 3
tb2 = context.getBean("protectedInstance", TestBean.class);
assertThat(tb2.getName()).isEqualTo("protectedInstance");
assertThat(tb).isSameAs(tb2);
// 4
tb = context.getBean("privateInstance", TestBean.class);
assertThat(tb.getName()).isEqualTo("privateInstance");
assertThat(tb.getAge()).isEqualTo(1);
// 4
tb2 = context.getBean("privateInstance", TestBean.class);
assertThat(tb2.getAge()).isEqualTo(2);
assertThat(tb).isNotSameAs(tb2);
// 5
Object bean = context.getBean("requestScopedInstance");
assertThat(AopUtils.isCglibProxy(bean)).isTrue();
boolean condition = bean instanceof ScopedObject;
assertThat(condition).isTrue();
QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
assertThat(clientBean.testBean).isSameAs(context.getBean("publicInstance"));
assertThat(clientBean.dependencyBean).isSameAs(context.getBean("dependencyBean"));
assertThat(clientBean.applicationContext).isSameAs(context);
}
use of org.springframework.aop.scope.ScopedObject in project spring-framework by spring-projects.
the class ConfigurationClassPostProcessorTests method scopedProxyTargetMarkedAsNonAutowireCandidate.
@Test
void scopedProxyTargetMarkedAsNonAutowireCandidate() {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
beanFactory.addBeanPostProcessor(bpp);
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(ScopedProxyConfigurationClass.class));
beanFactory.registerBeanDefinition("consumer", new RootBeanDefinition(ScopedProxyConsumer.class));
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
pp.postProcessBeanFactory(beanFactory);
ITestBean injected = beanFactory.getBean("consumer", ScopedProxyConsumer.class).testBean;
boolean condition = injected instanceof ScopedObject;
assertThat(condition).isTrue();
assertThat(injected).isSameAs(beanFactory.getBean("scopedClass"));
assertThat(injected).isSameAs(beanFactory.getBean(ITestBean.class));
}
Aggregations