@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener) ; parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1"); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2"); return sac; }
@Test @SuppressWarnings("deprecation") public void configureExporterParametersWithEncodingFromPropertiesFile() throws Exception { GenericWebApplicationContext ac = new GenericWebApplicationContext(); ac.setServletContext(new MockServletContext()); BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac); reader.loadBeanDefinitions(new ClassPathResource("view.properties", getClass())); ac.refresh(); AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report"); String encoding = (String) view.getConvertedExporterParameters().get( net.sf.jasperreports.engine.export.JRHtmlExporterParameter.CHARACTER_ENCODING); assertEquals("UTF-8", encoding); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac); view.render(getModel(), request, response); assertEquals("Response content type is incorrect", "text/html;charset=UTF-8", response.getContentType()); }
@Test public void testLazyInitialization() { KnowsIfInstantiated.clearInstantiationRecord(); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName()); p.setProperty("x1.(lazy-init)", "true"); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); lbf.preInstantiateSingletons(); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); lbf.getBean("x1"); assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated()); }
@Test public void testFactoryBeanDidNotCreatePrototype() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.singleton", "false"); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); assertEquals(TestBean.class, lbf.getType("x1")); lbf.preInstantiateSingletons(); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); lbf.getBean("x1"); assertEquals(TestBean.class, lbf.getType("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated()); }
@Test public void testSimpleReference() { String PREFIX = "beans."; DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName()); p.setProperty(PREFIX + "rod.name", "Rod"); p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName()); p.setProperty(PREFIX + "kerry.name", "Kerry"); p.setProperty(PREFIX + "kerry.age", "35"); p.setProperty(PREFIX + "kerry.spouse(ref)", "rod"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX); assertTrue("2 beans registered, not " + count, count == 2); TestBean kerry = lbf.getBean("kerry", TestBean.class); assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName())); ITestBean spouse = kerry.getSpouse(); assertTrue("Kerry spouse is non null", spouse != null); assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName())); }
@Test public void testUnresolvedReference() { String PREFIX = "beans."; DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); try { p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName()); p.setProperty(PREFIX + "kerry.name", "Kerry"); p.setProperty(PREFIX + "kerry.age", "35"); p.setProperty(PREFIX + "kerry.spouse(ref)", "rod"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX); lbf.getBean("kerry"); fail("Unresolved reference should have been detected"); } catch (BeansException ex) { // cool } }
@Test public void testPrototypeCircleLeadsToException() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("kerry.(class)", TestBean.class.getName()); p.setProperty("kerry.(singleton)", "false"); p.setProperty("kerry.age", "35"); p.setProperty("kerry.spouse", "*rod"); p.setProperty("rod.(class)", TestBean.class.getName()); p.setProperty("rod.(singleton)", "false"); p.setProperty("rod.age", "34"); p.setProperty("rod.spouse", "*kerry"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); try { lbf.getBean("kerry"); fail("Should have thrown BeanCreationException"); } catch (BeanCreationException ex) { // expected assertTrue(ex.contains(BeanCurrentlyInCreationException.class)); } }
@Test public void testRegisterExistingSingletonWithReference() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); p.setProperty("test.age", "48"); p.setProperty("test.spouse(ref)", "singletonObject"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); assertTrue(lbf.isSingleton("singletonObject")); assertEquals(TestBean.class, lbf.getType("singletonObject")); TestBean test = (TestBean) lbf.getBean("test"); assertEquals(singletonObject, lbf.getBean("singletonObject")); assertEquals(singletonObject, test.getSpouse()); Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true); assertEquals(2, beansOfType.size()); assertTrue(beansOfType.containsValue(test)); assertTrue(beansOfType.containsValue(singletonObject)); beansOfType = lbf.getBeansOfType(null, false, true); assertEquals(2, beansOfType.size()); }
public static void main(String[] args) { GenericApplicationContext ctx = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx); xmlReader.loadBeanDefinitions(new ClassPathResource("spring-context.xml")); PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx); propReader.loadBeanDefinitions(new ClassPathResource("otherBeans.properties")); ctx.refresh(); Mars mars = ctx.getBean(Mars.class); System.out.println(mars.getCnName()); }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); StaticMessageSource messageSource = sac.getStaticMessageSource(); Map<String, String> usMessages = new HashMap<String, String>(3); usMessages.put("message.format.example1", MSG_TXT1_US); usMessages.put("message.format.example2", MSG_TXT2_US); usMessages.put("message.format.example3", MSG_TXT3_US); messageSource.addMessages(usMessages, Locale.US); messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK); return sac; }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m)); parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, TestApplicationEventMulticaster.class, null); parent.refresh(); parent.addApplicationListener(parentListener) ; parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1"); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); Resource resource = new ClassPathResource("testBeans.properties", getClass()); reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1")); sac.refresh(); sac.addApplicationListener(listener); sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2"); return sac; }
@Test public void testUnreferencedSingletonWasInstantiated() { KnowsIfInstantiated.clearInstantiationRecord(); DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName()); assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated()); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.preInstantiateSingletons(); assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated()); }
@Test public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.(singleton)", "false"); p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(0, beanNames.length); beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); assertEquals(0, beanNames.length); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertFalse(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertTrue(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))); assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))); assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); }
@Test public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.(singleton)", "false"); p.setProperty("x1.singleton", "true"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(0, beanNames.length); beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); assertEquals(0, beanNames.length); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertFalse(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertTrue(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))); assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))); assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); }
@Test public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(0, beanNames.length); beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class); assertEquals(0, beanNames.length); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertTrue(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertFalse(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class))); assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class))); assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class))); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); }
@Test public void testEmptyPropertiesPopulation() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("No beans defined after ignorable invalid", lbf.getBeanDefinitionCount() == 0); }
@Test public void testHarmlessIgnorableRubbish() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("foo", "bar"); p.setProperty("qwert", "er"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, "test"); assertTrue("No beans defined after harmless ignorable rubbish", lbf.getBeanDefinitionCount() == 0); }
@Test public void testPropertiesPopulationWithNullPrefix() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); p.setProperty("test.age", "48"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("1 beans registered, not " + count, count == 1); testSingleTestBean(lbf); }
@Test public void testPropertiesPopulationWithPrefix() { String PREFIX = "beans."; DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty(PREFIX + "test.(class)", TestBean.class.getName()); p.setProperty(PREFIX + "test.name", "Tony"); p.setProperty(PREFIX + "test.age", "0x30"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX); assertTrue("1 beans registered, not " + count, count == 1); testSingleTestBean(lbf); }
@Test public void testPropertiesWithDotsInKey() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("tb.(class)", TestBean.class.getName()); p.setProperty("tb.someMap[my.key]", "my.value"); int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("1 beans registered, not " + count, count == 1); assertEquals(1, lbf.getBeanDefinitionCount()); TestBean tb = lbf.getBean("tb", TestBean.class); assertEquals("my.value", tb.getSomeMap().get("my.key")); }
@Test public void testNameAlreadyBound() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("kerry.(class)", TestBean.class.getName()); p.setProperty("kerry.age", "35"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); try { (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); } catch (BeanDefinitionStoreException ex) { assertEquals("kerry", ex.getBeanName()); // expected } }
@Test public void testBeanReferenceWithNewSyntax() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("r.(class)", TestBean.class.getName()); p.setProperty("r.name", "rod"); p.setProperty("k.(class)", TestBean.class.getName()); p.setProperty("k.name", "kerry"); p.setProperty("k.spouse", "*r"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); TestBean k = (TestBean) lbf.getBean("k"); TestBean r = (TestBean) lbf.getBean("r"); assertTrue(k.getSpouse() == r); }
@Test public void testCanEscapeBeanReferenceSyntax() { String name = "*name"; DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("r.(class)", TestBean.class.getName()); p.setProperty("r.name", "*" + name); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); TestBean r = (TestBean) lbf.getBean("r"); assertTrue(r.getName().equals(name)); }
@Test public void testRegisterExistingSingletonWithReference() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); p.setProperty("test.age", "48"); p.setProperty("test.spouse(ref)", "singletonObject"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); assertTrue(lbf.isSingleton("singletonObject")); assertEquals(TestBean.class, lbf.getType("singletonObject")); TestBean test = (TestBean) lbf.getBean("test"); assertEquals(singletonObject, lbf.getBean("singletonObject")); assertEquals(singletonObject, test.getSpouse()); Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true); assertEquals(2, beansOfType.size()); assertTrue(beansOfType.containsValue(test)); assertTrue(beansOfType.containsValue(singletonObject)); beansOfType = lbf.getBeansOfType(null, false, true); assertEquals(2, beansOfType.size()); Iterator<String> beanNames = lbf.getBeanNamesIterator(); assertEquals("test", beanNames.next()); assertEquals("singletonObject", beanNames.next()); assertFalse(beanNames.hasNext()); assertTrue(lbf.containsSingleton("test")); assertTrue(lbf.containsSingleton("singletonObject")); assertTrue(lbf.containsBeanDefinition("test")); assertFalse(lbf.containsBeanDefinition("singletonObject")); }
@Test public void testRegisterExistingSingletonWithNameOverriding() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("test.(class)", TestBean.class.getName()); p.setProperty("test.name", "Tony"); p.setProperty("test.age", "48"); p.setProperty("test.spouse(ref)", "singletonObject"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); lbf.registerBeanDefinition("singletonObject", new RootBeanDefinition(PropertiesFactoryBean.class)); Object singletonObject = new TestBean(); lbf.registerSingleton("singletonObject", singletonObject); lbf.preInstantiateSingletons(); assertTrue(lbf.isSingleton("singletonObject")); assertEquals(TestBean.class, lbf.getType("singletonObject")); TestBean test = (TestBean) lbf.getBean("test"); assertEquals(singletonObject, lbf.getBean("singletonObject")); assertEquals(singletonObject, test.getSpouse()); Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true); assertEquals(2, beansOfType.size()); assertTrue(beansOfType.containsValue(test)); assertTrue(beansOfType.containsValue(singletonObject)); beansOfType = lbf.getBeansOfType(null, false, true); Iterator<String> beanNames = lbf.getBeanNamesIterator(); assertEquals("test", beanNames.next()); assertEquals("singletonObject", beanNames.next()); assertFalse(beanNames.hasNext()); assertEquals(2, beansOfType.size()); assertTrue(lbf.containsSingleton("test")); assertTrue(lbf.containsSingleton("singletonObject")); assertTrue(lbf.containsBeanDefinition("test")); assertTrue(lbf.containsBeanDefinition("singletonObject")); }
private static void initPropertiesConf(GenericApplicationContext context){ String classPath=ProcessInfo.getPropertiesClassPath(); if(Help.isNotEmpty(classPath)){ PropertiesBeanDefinitionReader reader=new PropertiesBeanDefinitionReader(context); reader.loadBeanDefinitions(classPath); } }
/** Run for each test */ @Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); StaticMessageSource messageSource = sac.getStaticMessageSource(); Map<String, String> usMessages = new HashMap<String, String>(3); usMessages.put("message.format.example1", MSG_TXT1_US); usMessages.put("message.format.example2", MSG_TXT2_US); usMessages.put("message.format.example3", MSG_TXT3_US); messageSource.addMessages(usMessages, Locale.US); messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK); return sac; }
/** Run for each test */ @Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m)); parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, TestApplicationEventMulticaster.class, null); parent.refresh(); parent.addApplicationListener(parentListener) ; parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1"); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); Resource resource = new ClassPathResource("testBeans.properties", getClass()); reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1")); sac.refresh(); sac.addApplicationListener(listener); sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2"); return sac; }
/** Run for each test */ @Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<String, String>(); m.put("name", "Roderick"); parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m)); m.put("name", "Albert"); parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m)); parent.refresh(); parent.addApplicationListener(parentListener) ; parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1"); this.sac = new StaticApplicationContext(parent); sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues()); sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues()); sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory()); reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass())); sac.refresh(); sac.addApplicationListener(listener); sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2"); return sac; }
public void testConfigureExporterParametersWithEncodingFromPropertiesFile() throws Exception { GenericWebApplicationContext ac = new GenericWebApplicationContext(); ac.setServletContext(new MockServletContext()); BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac); reader.loadBeanDefinitions(new ClassPathResource("view.properties", getClass())); ac.refresh(); AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report"); String encoding = (String) view.getConvertedExporterParameters().get(JRHtmlExporterParameter.CHARACTER_ENCODING); assertEquals("UTF-8", encoding); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac); view.render(getModel(), request, response); assertEquals("Response content type is incorrect", "text/html;charset=UTF-8", response.getContentType()); }
@Test public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.(singleton)", "false"); p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(0, beanNames.length); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertFalse(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertTrue(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); }
@Test public void testPrototypeSingletonFactoryBeanIgnoredByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.(singleton)", "false"); p.setProperty("x1.singleton", "true"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(0, beanNames.length); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertFalse(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertTrue(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); }
@Test public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); Properties p = new Properties(); p.setProperty("x1.(class)", DummyFactory.class.getName()); // Reset static state DummyFactory.reset(); p.setProperty("x1.singleton", "false"); (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false); assertEquals(0, beanNames.length); assertFalse(lbf.containsSingleton("x1")); assertTrue(lbf.containsBean("x1")); assertTrue(lbf.containsBean("&x1")); assertFalse(lbf.isSingleton("x1")); assertTrue(lbf.isSingleton("&x1")); assertTrue(lbf.isPrototype("x1")); assertFalse(lbf.isPrototype("&x1")); assertTrue(lbf.isTypeMatch("x1", TestBean.class)); assertFalse(lbf.isTypeMatch("&x1", TestBean.class)); assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class)); assertEquals(TestBean.class, lbf.getType("x1")); assertEquals(DummyFactory.class, lbf.getType("&x1")); assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated()); }