Search in sources :

Example 26 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project spring-framework by spring-projects.

the class AutoProxyCreatorTests method testBeanNameAutoProxyCreatorWithFactoryBeanProxy.

@Test
public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testInterceptor", TestInterceptor.class);
    RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
    proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
    proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,&singletonFactoryToBeProxied");
    sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);
    sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
    sac.refresh();
    ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
    assertThat(Proxy.isProxyClass(singletonToBeProxied.getClass())).isTrue();
    TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
    int initialNr = ti.nrOfInvocations;
    singletonToBeProxied.getName();
    assertThat(ti.nrOfInvocations).isEqualTo((initialNr + 1));
    FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
    assertThat(Proxy.isProxyClass(factory.getClass())).isTrue();
    TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
    assertThat(AopUtils.isAopProxy(tb)).isFalse();
    assertThat(ti.nrOfInvocations).isEqualTo((initialNr + 3));
    tb.getAge();
    assertThat(ti.nrOfInvocations).isEqualTo((initialNr + 3));
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Example 27 with FactoryBean

use of org.springframework.beans.factory.FactoryBean in project spring-framework by spring-projects.

the class RequestScopeTests method getFromFactoryBeanInScope.

@Test
public void getFromFactoryBeanInScope() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "requestScopedFactoryBean";
    assertThat(request.getAttribute(name)).isNull();
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    boolean condition = request.getAttribute(name) instanceof FactoryBean;
    assertThat(condition).isTrue();
    assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) CountingTestBean(org.springframework.beans.testfixture.beans.CountingTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FactoryBean(org.springframework.beans.factory.FactoryBean) Test(org.junit.jupiter.api.Test)

Aggregations

FactoryBean (org.springframework.beans.factory.FactoryBean)27 Test (org.junit.jupiter.api.Test)10 SmartFactoryBean (org.springframework.beans.factory.SmartFactoryBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)5 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)5 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)4 Bean (org.springframework.context.annotation.Bean)4 BeansException (org.springframework.beans.BeansException)3 GroovyObject (groovy.lang.GroovyObject)2 PrivilegedAction (java.security.PrivilegedAction)2 ArrayList (java.util.ArrayList)2 HttpClientProperties (org.apereo.cas.configuration.model.core.authentication.HttpClientProperties)2 Test (org.junit.Test)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)2