Example usage for org.springframework.beans.factory.support BeanDefinitionReader loadBeanDefinitions

List of usage examples for org.springframework.beans.factory.support BeanDefinitionReader loadBeanDefinitions

Introduction

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

Prototype

int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException;

Source Link

Document

Load bean definitions from the specified resource locations.

Usage

From source file:net.kamhon.ieagle.application.Application.java

private static void createGenericApplicationContext() {
    if (appContext == null) {
        appContext = new GenericApplicationContext();

        Class<?> clazz = null;
        if (StringUtils.isNotBlank(clazzPath)) {
            try {
                clazz = Class.forName(clazzPath);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();//  www.ja v  a2 s.c  o m
            }
        }

        ClassPathResource resource = null;
        if (contextPath.startsWith("classpath:")) {
            if (clazz == null) {
                resource = new ClassPathResource(contextPath.substring("classpath:".length()));
            } else {
                resource = new ClassPathResource(contextPath.substring("classpath:".length()), clazz);
            }
        } else {
            if (clazz == null) {
                resource = new ClassPathResource(contextPath);
            } else {
                resource = new ClassPathResource(contextPath, clazz);
            }
        }
        BeanDefinitionReader reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) appContext);
        reader.loadBeanDefinitions(resource);
        ((GenericApplicationContext) appContext).refresh();
    }
}

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

public static synchronized ListableBeanFactory getListableBeanFactory(BeanletConfiguration<?> configuration,
        Element element) {// w w  w  . j  av  a  2 s  . co 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:org.codehaus.grepo.core.context.GrepoTestContextLoader.java

/**
 * {@inheritDoc}//from   w ww. j  av a2s.  co  m
 */
@Override
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
    BeanDefinitionReader bdr = super.createBeanDefinitionReader(context);
    ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
    try {
        Resource[] resources = rpr.getResources("classpath*:/META-INF/grepo/grepo-testcontext.xml");
        logger.debug("Found grepo-testcontexts: {}", ArrayUtils.toString(resources));
        bdr.loadBeanDefinitions(resources);

        String addPattern = getAdditionalConfigPattern();
        if (!StringUtils.isEmpty(addPattern)) {
            Resource[] addResources = rpr.getResources(addPattern);
            if (addResources != null) {
                logger.debug("Found additional spring-configs: {}", ArrayUtils.toString(addResources));
                bdr.loadBeanDefinitions(addResources);
            }
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    return bdr;
}

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;
    if (url.endsWith(".xml")) {
        reader = new XmlBeanDefinitionReader(context);
    } else if (url.endsWith(".properties")) {
        reader = new PropertiesBeanDefinitionReader(context);
    }//from   ww w  .j a  va  2  s  . c om

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

From source file:org.impalaframework.spring.module.loader.BaseApplicationContextLoader.java

private ConfigurableApplicationContext loadApplicationContext(Application application, ClassLoader classLoader,
        SpringModuleLoader moduleLoader, ApplicationContext parent, ModuleDefinition definition) {

    ClassLoader existing = ClassUtils.getDefaultClassLoader();

    try {/*w ww  .  j  a  va 2  s .  c  o m*/
        Thread.currentThread().setContextClassLoader(classLoader);

        if (logger.isDebugEnabled())
            logger.debug("Setting class loader to " + classLoader);

        ConfigurableApplicationContext context = moduleLoader.newApplicationContext(application, parent,
                definition, classLoader);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        addBeanPostProcessors(application, definition, beanFactory);

        final String applicationId = application.getId();
        BeanDefinitionReader reader = moduleLoader.newBeanDefinitionReader(applicationId, context, definition);

        if (reader != null) {
            //if this is null, then we assume moduleLoader or refresh takes care of this

            if (reader instanceof AbstractBeanDefinitionReader) {
                ((AbstractBeanDefinitionReader) reader).setBeanClassLoader(classLoader);
            }

            final Resource[] resources = moduleLoader.getSpringConfigResources(applicationId, definition,
                    classLoader);
            reader.loadBeanDefinitions(resources);
        }

        moduleLoader.handleRefresh(applicationId, context, definition);
        return context;
    } finally {
        Thread.currentThread().setContextClassLoader(existing);
    }
}

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

private void loadBeanDefinitionsFromUrl(String url, GenericApplicationContext context) {
    BeanDefinitionReader reader = null;
    if (url.endsWith(".xml")) {
        reader = new XmlBeanDefinitionReader(context);
    } else if (url.endsWith(".properties")) {
        reader = new PropertiesBeanDefinitionReader(context);
    }// www .  ja va  2s  .c om

    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.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.java

private void loadBeanDefinitionsFromImportedResources(
        Map<String, Class<? extends BeanDefinitionReader>> importedResources) {

    Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<>();

    for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
        String resource = entry.getKey();
        Class<? extends BeanDefinitionReader> readerClass = entry.getValue();

        // Default reader selection necessary?
        if (BeanDefinitionReader.class == readerClass) {
            if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
                // When clearly asking for Groovy, that's what they'll get...
                readerClass = GroovyBeanDefinitionReader.class;
            } else {
                // Primarily ".xml" files but for any other extension as well
                readerClass = XmlBeanDefinitionReader.class;
            }/*from   www  .jav a2s . c  o m*/
        }

        BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
        if (reader == null) {
            try {
                // Instantiate the specified BeanDefinitionReader
                reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
                // Delegate the current ResourceLoader to it if possible
                if (reader instanceof AbstractBeanDefinitionReader) {
                    AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
                    abdr.setResourceLoader(this.resourceLoader);
                    abdr.setEnvironment(this.environment);
                }
                readerInstanceCache.put(readerClass, reader);
            } catch (Throwable ex) {
                throw new IllegalStateException(
                        "Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
            }
        }

        // TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
        reader.loadBeanDefinitions(resource);
    }
}