Example usage for org.springframework.beans.factory.xml XmlBeanDefinitionReader setEntityResolver

List of usage examples for org.springframework.beans.factory.xml XmlBeanDefinitionReader setEntityResolver

Introduction

In this page you can find the example usage for org.springframework.beans.factory.xml XmlBeanDefinitionReader setEntityResolver.

Prototype

public void setEntityResolver(@Nullable EntityResolver entityResolver) 

Source Link

Document

Set a SAX entity resolver to be used for parsing.

Usage

From source file:org.wildfly.extension.camel.SpringCamelContextFactory.java

private static CamelContext createSpringCamelContext(Resource resource, ClassLoader classLoader)
        throws Exception {
    GenericApplicationContext appContext = new GenericApplicationContext();
    appContext.setClassLoader(classLoader);
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext) {
        @Override/*from   w  ww  .  j av  a  2 s .c  o  m*/
        protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
            NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver();
            return new CamelNamespaceHandlerResolver(defaultResolver);
        }
    };
    xmlReader.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            InputStream inputStream = null;
            if (CAMEL_SPRING_SYSTEM_ID.equals(systemId)) {
                inputStream = SpringCamelContext.class.getResourceAsStream("/camel-spring.xsd");
            } else if (SPRING_BEANS_SYSTEM_ID.equals(systemId)) {
                inputStream = XmlBeanDefinitionReader.class.getResourceAsStream("spring-beans-3.1.xsd");
            }
            InputSource result = null;
            if (inputStream != null) {
                result = new InputSource();
                result.setSystemId(systemId);
                result.setByteStream(inputStream);
            }
            return result;
        }
    });
    xmlReader.loadBeanDefinitions(resource);
    appContext.refresh();
    return SpringCamelContext.springCamelContext(appContext);
}

From source file:com.griddynamics.banshun.StrictContextParentBean.java

private BeanDefinitionRegistry getBeanFactory(String location) {
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
            new SimpleBeanDefinitionRegistry());
    beanDefinitionReader.setResourceLoader(context);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(context));
    beanDefinitionReader.loadBeanDefinitions(location);

    return beanDefinitionReader.getBeanFactory();
}

From source file:com.brienwheeler.lib.spring.beans.SmartClassPathXmlApplicationContext.java

/**
 * Since this method in the parent class doesn't allow customization of the
 * bean definition reader class, we copy the logic here.
 */// w w  w .j ava2  s  . c om
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    XmlBeanDefinitionReader beanDefinitionReader = new SmartXmlBeanDefinitionReader(beanFactory);

    // Configure the bean definition reader with this context's
    // resource loading environment.
    beanDefinitionReader.setResourceLoader(this);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    // Allow a subclass to provide custom initialization of the reader,
    // then proceed with actually loading the bean definitions.
    initBeanDefinitionReader(beanDefinitionReader);
    loadBeanDefinitions(beanDefinitionReader);
}

From source file:org.solmix.runtime.support.spring.ContainerApplicationContext.java

void setEntityResolvers(XmlBeanDefinitionReader reader) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    reader.setEntityResolver(
            new ContainerEntityResolver(cl, new BeansDtdResolver(), new PluggableSchemaResolver(cl)));
}

From source file:org.sakaiproject.util.SakaiApplicationContext.java

protected void loadBeanDefinitions(ConfigurableListableBeanFactory beanFactory) throws IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
            (BeanDefinitionRegistry) beanFactory);
    beanDefinitionReader.setBeanClassLoader(Thread.currentThread().getContextClassLoader());
    beanDefinitionReader.setResourceLoader(this);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    if (configLocations != null) {
        beanDefinitionReader.loadBeanDefinitions(configLocations);
    }//from   w  w w  .  jav  a2  s.co  m
}

From source file:com.mtgi.analytics.aop.config.TemplateBeanDefinitionParser.java

/**
 * <p>Load the template BeanDefinition and call {@link #transform(ConfigurableListableBeanFactory, BeanDefinition, Element, ParserContext)}
 * to apply runtime configuration value to it.  <code>builder</code> will be configured to instantiate the bean
 * in the Spring context that we are parsing.</p>
 * /*  w w  w  .  j ava 2 s.c o m*/
 * <p>During parsing, an instance of {@link TemplateBeanDefinitionParser.TemplateComponentDefinition} is pushed onto <code>ParserContext</code> so
 * that nested tags can access the enclosing template configuration with a call to {@link #findEnclosingTemplateFactory(ParserContext)}.
 * Subclasses can override {@link #newComponentDefinition(String, Object, DefaultListableBeanFactory)} to provide a 
 * subclass of {@link TemplateBeanDefinitionParser.TemplateComponentDefinition} to the parser context if necessary.</p>
 */
@Override
protected final void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {

    //if we have multiple nested bean definitions, we only parse the template factory
    //once.  this allows configuration changes made by enclosing bean parsers to be inherited
    //by contained beans, which is quite useful.
    DefaultListableBeanFactory templateFactory = findEnclosingTemplateFactory(parserContext);
    TemplateComponentDefinition tcd = null;
    if (templateFactory == null) {

        //no nesting -- load the template XML configuration from the classpath.
        final BeanFactory parentFactory = (BeanFactory) parserContext.getRegistry();
        templateFactory = new DefaultListableBeanFactory(parentFactory);

        //load template bean definitions
        DefaultResourceLoader loader = new DefaultResourceLoader();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(templateFactory);
        reader.setResourceLoader(loader);
        reader.setEntityResolver(new ResourceEntityResolver(loader));
        reader.loadBeanDefinitions(templateResource);

        //propagate factory post-processors from the source factory into the template
        //factory.
        BeanDefinition ppChain = new RootBeanDefinition(ChainingBeanFactoryPostProcessor.class);
        ppChain.getPropertyValues().addPropertyValue("targetFactory", templateFactory);
        parserContext.getReaderContext().registerWithGeneratedName(ppChain);

        //push component definition onto the parser stack for the benefit of
        //nested bean definitions.
        tcd = newComponentDefinition(element.getNodeName(), parserContext.extractSource(element),
                templateFactory);
        parserContext.pushContainingComponent(tcd);
    }

    try {
        //allow subclasses to apply overrides to the template bean definition.
        BeanDefinition def = templateFactory.getBeanDefinition(templateId);
        transform(templateFactory, def, element, parserContext);

        //setup our factory bean to instantiate the modified bean definition upon request.
        builder.addPropertyValue("beanFactory", templateFactory);
        builder.addPropertyValue("beanName", templateId);
        builder.getRawBeanDefinition().setAttribute("id", def.getAttribute("id"));

    } finally {
        if (tcd != null)
            parserContext.popContainingComponent();
    }
}

From source file:org.web4thejob.context.DefaultSessionContext.java

@Override
protected void loadBeanDefinitions(final DefaultListableBeanFactory beanFactory)
        throws BeansException, IOException {

    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    final XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

    // Configure the bean definition reader with this context's
    // resource loading environment.
    beanDefinitionReader.setResourceLoader(this);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    // Allow a subclass to provide custom initialization of the reader,
    // then proceed with actually loading the bean definitions.
    // initBeanDefinitionReader(beanDefinitionReader);
    loadBeanDefinitions(beanDefinitionReader);

}

From source file:net.bubble.common.utils.BeanContextUtil.java

/**
 * ?Spring?</br>/*from   w w  w . j  av a2 s . co m*/
 * ???war/ear???????
 * @param configurationLocations spring?
 * @throws CommonException Common?
 */
public void loadCustomizedConfiguration(List<String> configurationLocations) throws CommonException {
    logger.info("Loading custom configuration file ......");
    if (configurationLocations == null || configurationLocations.size() < 1) {
        logger.info("No custom configuration file !");
        return;
    }
    try {
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
                (BeanDefinitionRegistry) applicationContext.getBeanFactory());
        beanDefinitionReader.setResourceLoader(applicationContext);
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(applicationContext));
        for (String localtion : configurationLocations)
            beanDefinitionReader.loadBeanDefinitions(applicationContext.getResources(localtion));
    } catch (BeansException e) {
        logger.error(e.getMessage(), e);
        throw new CommonException("Load custom bean definitions has error !", e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw new CommonException("Read configuration file has error !", e);
    }
    logger.info("Load Custom configuration file is finish !");
}

From source file:com.opengamma.component.factory.AbstractSpringComponentFactory.java

/**
 * Creates the application context.//from  www. j  a  v  a  2s  .  c o  m
 * 
 * @param repo  the component repository, not null
 * @return the Spring application context, not null
 */
protected GenericApplicationContext createApplicationContext(ComponentRepository repo) {
    Resource springFile = getSpringFile();
    try {
        repo.getLogger().logDebug("  Spring file: " + springFile.getURI());
    } catch (Exception ex) {
        // ignore
    }

    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    GenericApplicationContext appContext = new GenericApplicationContext(beanFactory);

    PropertyPlaceholderConfigurer properties = new PropertyPlaceholderConfigurer();
    properties.setLocation(getPropertiesFile());

    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    beanDefinitionReader.setValidating(true);
    beanDefinitionReader.setResourceLoader(appContext);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(appContext));
    beanDefinitionReader.loadBeanDefinitions(springFile);

    appContext.getBeanFactory().registerSingleton("injectedProperties", properties);
    appContext.getBeanFactory().registerSingleton("componentRepository", repo);

    appContext.refresh();
    return appContext;
}

From source file:org.solmix.runtime.support.spring.ContainerApplicationContext.java

@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    beanDefinitionReader.setNamespaceHandlerResolver(nshResolver);

    // Configure the bean definition reader with this context's
    // resource loading environment.
    beanDefinitionReader.setResourceLoader(this);
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    // Allow a subclass to provide custom initialization of the reader,
    // then proceed with actually loading the bean definitions.
    initBeanDefinitionReader(beanDefinitionReader);
    loadBeanDefinitions(beanDefinitionReader);
}