public void testServiceMappings() { StaticApplicationContext ctx = new StaticApplicationContext(); ctx.registerPrototype("testService1", TestService.class, new MutablePropertyValues()); ctx.registerPrototype("testService2", ExtendedTestService.class, new MutablePropertyValues()); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class); mpv.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2"); ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv); ctx.refresh(); TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory"); TestService testBean1 = factory.getTestService(); TestService testBean2 = factory.getTestService("testService1"); TestService testBean3 = factory.getTestService(1); TestService testBean4 = factory.getTestService(2); assertNotSame(testBean1, testBean2); assertNotSame(testBean1, testBean3); assertNotSame(testBean1, testBean4); assertNotSame(testBean2, testBean3); assertNotSame(testBean2, testBean4); assertNotSame(testBean3, testBean4); assertFalse(testBean1 instanceof ExtendedTestService); assertFalse(testBean2 instanceof ExtendedTestService); assertFalse(testBean3 instanceof ExtendedTestService); assertTrue(testBean4 instanceof ExtendedTestService); }
@Test public void testCustomAutoProxyCreator() { StaticApplicationContext sac = new StaticApplicationContext(); sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class); sac.registerSingleton("singletonNoInterceptor", TestBean.class); sac.registerSingleton("singletonToBeProxied", TestBean.class); sac.registerPrototype("prototypeToBeProxied", TestBean.class); sac.refresh(); MessageSource messageSource = (MessageSource) sac.getBean("messageSource"); ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor"); ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied"); ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied"); assertFalse(AopUtils.isCglibProxy(messageSource)); assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor)); assertTrue(AopUtils.isCglibProxy(singletonToBeProxied)); assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied)); TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator"); assertEquals(0, tapc.testInterceptor.nrOfInvocations); singletonNoInterceptor.getName(); assertEquals(0, tapc.testInterceptor.nrOfInvocations); singletonToBeProxied.getAge(); assertEquals(1, tapc.testInterceptor.nrOfInvocations); prototypeToBeProxied.getSpouse(); assertEquals(2, tapc.testInterceptor.nrOfInvocations); }
public void testCombinedLocatorInterface() { StaticApplicationContext ctx = new StaticApplicationContext(); ctx.registerPrototype("testService", TestService.class, new MutablePropertyValues()); ctx.registerAlias("testService", "1"); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class); ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv); ctx.refresh(); TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory"); TestService testBean1 = factory.getTestService(); TestService testBean2 = factory.getTestService("testService"); TestService testBean3 = factory.getTestService(1); TestService testBean4 = factory.someFactoryMethod(); assertNotSame(testBean1, testBean2); assertNotSame(testBean1, testBean3); assertNotSame(testBean1, testBean4); assertNotSame(testBean2, testBean3); assertNotSame(testBean2, testBean4); assertNotSame(testBean3, testBean4); assertTrue(factory.toString().indexOf("TestServiceLocator3") != -1); }