Example usage for org.springframework.beans.factory.support PropertiesBeanDefinitionReader PropertiesBeanDefinitionReader

List of usage examples for org.springframework.beans.factory.support PropertiesBeanDefinitionReader PropertiesBeanDefinitionReader

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support PropertiesBeanDefinitionReader PropertiesBeanDefinitionReader.

Prototype

public PropertiesBeanDefinitionReader(BeanDefinitionRegistry registry) 

Source Link

Document

Create new PropertiesBeanDefinitionReader for the given bean factory.

Usage

From source file:org.beanlet.springframework.impl.SpringHelper.java

public static synchronized ListableBeanFactory getListableBeanFactory(BeanletConfiguration<?> configuration,
        Element element) {//from w w w . j  a  v  a2  s .  c  o  m
    SpringContext springContext = getSpringContext(configuration, element);
    if (springContext == null) {
        throw new ApplicationContextException("No spring context specified.");
    }
    final ClassLoader loader = configuration.getComponentUnit().getClassLoader();
    Map<SpringContext, ListableBeanFactory> map = factories.get(loader);
    if (map == null) {
        map = new HashMap<SpringContext, ListableBeanFactory>();
        factories.put(loader, map);
    }
    ListableBeanFactory factory = map.get(springContext);
    if (factory == null) {
        ClassLoader org = null;
        try {
            org = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
                    // PERMISSION: java.lang.RuntimePermission getClassLoader
                    ClassLoader org = Thread.currentThread().getContextClassLoader();
                    // PERMISSION: java.lang.RuntimePermission setContextClassLoader
                    Thread.currentThread().setContextClassLoader(loader);
                    return org;
                }
            });
            if (springContext.applicationContext()) {
                factory = new GenericApplicationContext();
            } else {
                factory = new DefaultListableBeanFactory();
            }
            // Do not create spring context in priviliged scope!
            for (SpringResource r : springContext.value()) {
                String path = r.value();
                Resource resource = null;
                BeanDefinitionReader reader = null;
                switch (r.type()) {
                case CLASSPATH:
                    resource = new ClassPathResource(path);
                    break;
                case FILESYSTEM:
                    resource = new FileSystemResource(path);
                    break;
                case URL:
                    resource = new UrlResource(path);
                    break;
                default:
                    assert false : r.type();
                }
                switch (r.format()) {
                case XML:
                    reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
                    break;
                case PROPERTIES:
                    reader = new PropertiesBeanDefinitionReader((BeanDefinitionRegistry) factory);
                    break;
                default:
                    assert false : r.format();
                }
                if (resource != null && resource.exists()) {
                    reader.loadBeanDefinitions(resource);
                }
            }
            if (factory instanceof ConfigurableApplicationContext) {
                ((ConfigurableApplicationContext) factory).refresh();
            }
            map.put(springContext, factory);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new ApplicationContextException("Failed to construct spring "
                    + (springContext.applicationContext() ? "application context" : "bean factory") + ".", e);
        } finally {
            final ClassLoader tmp = org;
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    // PERMISSION: java.lang.RuntimePermission setContextClassLoader
                    Thread.currentThread().setContextClassLoader(tmp);
                    return null;
                }
            });
        }
    }
    return factory;
}

From source file:com.collabnet.ccf.core.recovery.HospitalArtifactReplayer.java

private void loadBeanDefinitionsFromClasspath(String url, GenericApplicationContext context) {
    String resourceName = url.substring(url.indexOf(':') + 1);
    BeanDefinitionReader reader = null;// www .java  2s  . co  m
    if (url.endsWith(".xml")) {
        reader = new XmlBeanDefinitionReader(context);
    } else if (url.endsWith(".properties")) {
        reader = new PropertiesBeanDefinitionReader(context);
    }

    if (reader != null) {
        reader.loadBeanDefinitions(new ClassPathResource(resourceName));
    } else {
        throw new RuntimeException("No BeanDefinitionReader associated with " + url);
    }
}

From source file:com.collabnet.ccf.core.recovery.HospitalArtifactReplayer.java

@SuppressWarnings("deprecation")
private void loadBeanDefinitionsFromUrl(String url, GenericApplicationContext context) {
    BeanDefinitionReader reader = null;/*from  w  ww .  java 2 s.c o m*/
    if (url.endsWith(".xml")) {
        reader = new XmlBeanDefinitionReader(context);
    } else if (url.endsWith(".properties")) {
        reader = new PropertiesBeanDefinitionReader(context);
    }

    if (reader != null) {
        try {
            UrlResource urlResource = new UrlResource(url);
            InputStream is = urlResource.getInputStream();
            Document document = builder.parse(is);
            Element routerElement = this.getRouterElement(document);
            this.stripOffProcessors(routerElement);
            this.addGAImportComponents(document, routerElement);
            DOMImplementationRegistry registry = null;
            try {
                registry = DOMImplementationRegistry.newInstance();
            } catch (ClassCastException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            } catch (ClassNotFoundException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            } catch (InstantiationException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            } catch (IllegalAccessException e) {
                log.error("error", e);
                throw new CCFRuntimeException("error", e);
            }
            String originalConfigFileAbsolutePath = urlResource.getFile().getAbsolutePath();
            String componentName = entry.getSourceComponent();
            String configComponentIdentifier = "{" + originalConfigFileAbsolutePath + "}" + componentName;

            File outputFile = null;
            if (componentConfigFileMap.containsKey(configComponentIdentifier)) {
                outputFile = componentConfigFileMap.get(configComponentIdentifier);
            } else {
                outputFile = File.createTempFile(componentName, ".xml", replayWorkDir);
                DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
                LSSerializer writer = impl.createLSSerializer();
                LSOutput output = impl.createLSOutput();
                FileOutputStream bas = new FileOutputStream(outputFile.getAbsolutePath());
                output.setByteStream(bas);
                writer.write(document, output);
                bas.flush();
                bas.close();
                componentConfigFileMap.put(configComponentIdentifier, outputFile);
            }

            // FIXME Use of deprecated method
            UrlResource newUrlResource = new UrlResource(outputFile.toURL().toString());
            ((XmlBeanDefinitionReader) reader).registerBeanDefinitions(document, newUrlResource);
        } catch (BeansException e) {
            log.error("error", e);
            throw new RuntimeException("BeansException : " + e.getMessage(), e);
        } catch (MalformedURLException e) {
            log.error("error", e);
            throw new RuntimeException("MalformedUrlException : " + e.getMessage(), e);
        } catch (IOException e) {
            log.error("error", e);
            throw new RuntimeException("IOExceptionException : " + e.getMessage(), e);
        } catch (SAXException e) {
            log.error("error", e);
            throw new RuntimeException("SAXException : " + e.getMessage(), e);
        }
    } else {
        throw new RuntimeException("No BeanDefinitionReader associated with " + url);
    }
}

From source file:org.openadaptor.spring.SpringAdaptor.java

private void loadBeanDefinitionsFromUrl(String url, GenericApplicationContext context) {
    BeanDefinitionReader reader = null;/* w ww .j  a v a  2s.c o  m*/
    if (url.endsWith(".xml")) {
        reader = new XmlBeanDefinitionReader(context);
    } else if (url.endsWith(".properties")) {
        reader = new PropertiesBeanDefinitionReader(context);
    }

    if (reader != null) {
        try {
            reader.loadBeanDefinitions(new UrlResource(url));
        } catch (BeansException e) {
            log.error("error", e);
            throw new RuntimeException("BeansException : " + e.getMessage());
        } catch (MalformedURLException e) {
            log.error("error", e);
            throw new RuntimeException("MalformedUrlException : " + e.getMessage());
        }
    } else {
        throw new RuntimeException("No BeanDefinitionReader associated with " + url);
    }
}

From source file:org.sakaiproject.metaobj.utils.mvc.impl.beans.AddableResourceBundleViewResolver.java

/**
 * Initialize the BeanFactory from the ResourceBundle, for the given locale.
 * Synchronized because of access by parallel threads.
 *///from  w w w .  ja v  a 2 s  . c  o m
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()));

    if (isCache()) {
        factory.preInstantiateSingletons();
        this.cachedFactories.put(locale, factory);
    }
    return factory;
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@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();//from   w  ww.  j  a v  a 2s. c om
    assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@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();/*from   w ww.j  ava 2 s .com*/

    assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
    lbf.getBean("x1");
    assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testFactoryBeanDidNotCreatePrototype() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();/*from  w  w  w . j  a  va 2  s.c  o  m*/
    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());
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();/*w  w  w. j av a2  s  . co m*/
    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());
}