Search in sources :

Example 6 with ScopedObject

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);
}
Also used : ScopedObject(org.springframework.aop.scope.ScopedObject) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SimpleMapScope(org.springframework.context.testfixture.SimpleMapScope) TestBean(org.springframework.beans.testfixture.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ScopedObject(org.springframework.aop.scope.ScopedObject) FactoryMethodComponent(org.springframework.context.annotation4.FactoryMethodComponent) Test(org.junit.jupiter.api.Test)

Example 7 with ScopedObject

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));
}
Also used : AutowiredAnnotationBeanPostProcessor(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ScopedObject(org.springframework.aop.scope.ScopedObject) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

ScopedObject (org.springframework.aop.scope.ScopedObject)7 Test (org.junit.jupiter.api.Test)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)3 TestBean (org.springframework.beans.testfixture.beans.TestBean)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1 ExecutionEntity (org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity)1 ProcessInstance (org.camunda.bpm.engine.runtime.ProcessInstance)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor)1 FactoryMethodComponent (org.springframework.context.annotation4.FactoryMethodComponent)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 SimpleMapScope (org.springframework.context.testfixture.SimpleMapScope)1