@Test public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); }
@Test public void withScopedProxy() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxy.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); // test serializability assertThat(bean.foo(1), equalTo("bar")); FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean); assertThat(deserialized, notNullValue()); assertThat(deserialized.foo(1), equalTo("bar")); }
@Test public void earlyProxyCreationAndBeanRegistrationLifecycle() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FeatureConfig.class); // // see additional assertions in FeatureConfig#feature() // // sanity check that all the bean definitions we expecting are present assertThat(ctx.getBeanFactory().containsBeanDefinition("lazyHelperBean"), is(true)); assertThat(ctx.getBeanFactory().containsBeanDefinition("eagerHelperBean"), is(true)); assertThat(ctx.getBeanFactory().containsBeanDefinition("lazyPassthroughBean"), is(true)); assertThat(ctx.getBeanFactory().containsBeanDefinition("eagerPassthroughBean"), is(true)); // the lazy helper bean had methods invoked during feature method execution. it should be // registered assertThat(ctx.getBeanFactory().containsSingleton("lazyHelperBean"), is(true)); // the eager helper bean had methods invoked but should be registered in any case is it is // non-lazy assertThat(ctx.getBeanFactory().containsSingleton("eagerHelperBean"), is(true)); // the lazy passthrough bean was referenced in the feature method, but never invoked. it should // not be registered assertThat(ctx.getBeanFactory().containsSingleton("lazyPassthroughBean"), is(false)); // the eager passthrough bean should be registered in any case as it is non-lazy assertThat(ctx.getBeanFactory().containsSingleton("eagerPassthroughBean"), is(true)); // now actually fetch all the beans. none should be proxies assertThat(ctx.getBean("lazyHelperBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); assertThat(ctx.getBean("eagerHelperBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); assertThat(ctx.getBean("lazyPassthroughBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); assertThat(ctx.getBean("eagerPassthroughBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); }