Example usage for org.springframework.beans.factory.xml NamespaceHandlerResolver NamespaceHandlerResolver

List of usage examples for org.springframework.beans.factory.xml NamespaceHandlerResolver NamespaceHandlerResolver

Introduction

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

Prototype

NamespaceHandlerResolver

Source Link

Usage

From source file:com.github.yihtserns.jaxbean.unmarshaller.api.SpringBeanHandlerTest.java

@Override
protected <T> T unmarshal(String xml, Class<T> rootType, Class<?>... allTypes) throws Exception {
    JaxbeanUnmarshaller unmarshaller = JaxbeanUnmarshaller.newInstance(merge(rootType, allTypes));
    final UnmarshallerNamespaceHandler unmarshallerNamespaceHandler = new UnmarshallerNamespaceHandler(
            unmarshaller);//from   w  w w .j a v a  2  s .  c  om

    GenericApplicationContext appContext = new GenericApplicationContext();
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext) {

        @Override
        protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
            final NamespaceHandlerResolver defaultResolver = super.createDefaultNamespaceHandlerResolver();
            return new NamespaceHandlerResolver() {

                public NamespaceHandler resolve(String namespaceUri) {
                    if (namespaceUri.equals("http://example.com/jaxb")) {
                        return unmarshallerNamespaceHandler;
                    }
                    return defaultResolver.resolve(namespaceUri);
                }
            };
        }
    };
    xmlReader.setValidating(false);
    xmlReader.loadBeanDefinitions(new InputSource(new StringReader(xml)));
    appContext.refresh();

    return appContext.getBean(rootType);
}