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));
}
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);
}
Aggregations