@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 } }
/** * Load bean definitions from the specified properties file. * @param encodedResource the resource descriptor for the properties file, * allowing to specify an encoding to use for parsing the file * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors */ public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { return loadBeanDefinitions(encodedResource, null); }
cas.addIndexedArgumentValue(index, readValue(entry)); pvs.add(property, readValue(entry)); parent, className, getBeanClassLoader()); bd.setScope(scope); bd.setAbstract(isAbstract); bd.setConstructorArgumentValues(cas); bd.setPropertyValues(pvs); getRegistry().registerBeanDefinition(beanName, bd);
logger.trace("Found bean name '" + beanName + "'"); if (!getRegistry().containsBeanDefinition(beanName)) { registerBeanDefinition(beanName, map, prefix + beanName, resourceDescription); ++beanCount;
try (InputStream is = encodedResource.getResource().getInputStream()) { if (encodedResource.getEncoding() != null) { getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding())); getPropertiesPersister().load(props, is); int count = registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription()); if (logger.isDebugEnabled()) { logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory); reader.setDefaultParentBean(this.defaultParentView); for (ResourceBundle bundle : bundles) { reader.registerBeanDefinitions(bundle);
/** * Register bean definitions contained in a resource bundle, * using all property keys (i.e. not filtering by prefix). * @param rb the ResourceBundle to load from * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors * @see #registerBeanDefinitions(java.util.ResourceBundle, String) */ public int registerBeanDefinitions(ResourceBundle rb) throws BeanDefinitionStoreException { return registerBeanDefinitions(rb, null); }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<>(); 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<>(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; }
/** * Create a new JdbcBeanDefinitionReader for the given bean factory, * using a default PropertiesBeanDefinitionReader underneath. * <p>DataSource or JdbcTemplate still need to be set. * @see #setDataSource * @see #setJdbcTemplate */ public JdbcBeanDefinitionReader(BeanDefinitionRegistry beanFactory) { this.propReader = new PropertiesBeanDefinitionReader(beanFactory); }
return registerBeanDefinitions(rb, null); return registerBeanDefinitions(map, null); logger.debug("Found bean name '" + beanName + "'"); if (!getRegistry().containsBeanDefinition(beanName)) { registerBeanDefinition(beanName, map, prefix + beanName, resourceDescription); ++beanCount;
if (key.startsWith(prefix + SEPARATOR)) { String property = key.substring(prefix.length() + SEPARATOR.length()); if (isClassKey(property)) { className = (String) entry.getValue(); else if (isParentKey(property)) { parent = (String) entry.getValue(); className, parent, null, pvs, getBeanClassLoader()); bd.setAbstract(isAbstract); bd.setSingleton(singleton); bd.setLazyInit(lazyInit); getBeanFactory().registerBeanDefinition(beanName, bd);
logger.debug("Found bean name '" + beanName + "'"); if (!getBeanFactory().containsBeanDefinition(beanName)) { registerBeanDefinition(beanName, map, prefix + beanName, resourceDescription); ++beanCount;
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory); reader.setDefaultParentBean(this.defaultParentView); for (ResourceBundle bundle : bundles) { reader.registerBeanDefinitions(bundle);
/** * Register bean definitions contained in a Map, using all property keys (i.e. not * filtering by prefix). * @param map a map of {@code name} to {@code property} (String or Object). Property * values will be strings if coming from a Properties file etc. Property names * (keys) <b>must</b> be Strings. Class keys must be Strings. * @return the number of bean definitions found * @throws BeansException in case of loading or parsing errors * @see #registerBeanDefinitions(java.util.Map, String, String) */ public int registerBeanDefinitions(Map<?, ?> map) throws BeansException { return registerBeanDefinitions(map, null); }
@Override protected ConfigurableApplicationContext createContext() throws Exception { StaticApplicationContext parent = new StaticApplicationContext(); Map<String, String> m = new HashMap<>(); 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; }
try (InputStream is = encodedResource.getResource().getInputStream()) { if (encodedResource.getEncoding() != null) { getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding())); getPropertiesPersister().load(props, is); int count = registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription()); if (logger.isDebugEnabled()) { logger.debug("Loaded " + count + " bean definitions from " + encodedResource);
/** * Creates a new {@link PropertiesBeanDefinitionReader}. * @return a new PropertiesBeanDefinitionReader * @see PropertiesBeanDefinitionReader */ @Override protected BeanDefinitionReader createBeanDefinitionReader(final GenericApplicationContext context) { return new PropertiesBeanDefinitionReader(context); }
logger.trace("Found bean name '" + beanName + "'"); if (!getRegistry().containsBeanDefinition(beanName)) { registerBeanDefinition(beanName, map, prefix + beanName, resourceDescription); ++beanCount;
@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); }
/** * Initialize the BeanFactory from the ResourceBundle, for the given locale. * Synchronized because of access by parallel threads. */ protected synchronized BeanFactory initFactory(Locale locale) throws MissingResourceException, BeansException { BeanFactory parsedBundle = isCache() ? (BeanFactory) this.cachedFactories.get(locale) : null; if (parsedBundle != null) { return parsedBundle; } DefaultListableBeanFactory factory = new DefaultListableBeanFactory(getApplicationContext()); PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory); reader.setDefaultParentBean(this.defaultParentView); for (Iterator i = baseNames.iterator(); i.hasNext();) { ResourceBundle bundle = ResourceBundle.getBundle((String) i.next(), locale, Thread.currentThread().getContextClassLoader()); reader.registerBeanDefinitions(bundle); } factory.registerCustomEditor(Resource.class, (new ResourceEditor(getApplicationContext())).getClass()); if (isCache()) { factory.preInstantiateSingletons(); this.cachedFactories.put(locale, factory); } return factory; }