Search in sources :

Example 1 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class DefaultNamespaceHandlerResolver method resolve.

/**
	 * Locate the {@link NamespaceHandler} for the supplied namespace URI
	 * from the configured mappings.
	 * @param namespaceUri the relevant namespace URI
	 * @return the located {@link NamespaceHandler}, or {@code null} if none found
	 */
@Override
public NamespaceHandler resolve(String namespaceUri) {
    Map<String, Object> handlerMappings = getHandlerMappings();
    Object handlerOrClassName = handlerMappings.get(namespaceUri);
    if (handlerOrClassName == null) {
        return null;
    } else if (handlerOrClassName instanceof NamespaceHandler) {
        return (NamespaceHandler) handlerOrClassName;
    } else {
        String className = (String) handlerOrClassName;
        try {
            Class<?> handlerClass = ClassUtils.forName(className, this.classLoader);
            if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
                throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri + "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
            }
            NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
            namespaceHandler.init();
            handlerMappings.put(namespaceUri, namespaceHandler);
            return namespaceHandler;
        } catch (ClassNotFoundException ex) {
            throw new FatalBeanException("NamespaceHandler class [" + className + "] for namespace [" + namespaceUri + "] not found", ex);
        } catch (LinkageError err) {
            throw new FatalBeanException("Invalid NamespaceHandler class [" + className + "] for namespace [" + namespaceUri + "]: problem with handler class file or dependent class", err);
        }
    }
}
Also used : FatalBeanException(org.springframework.beans.FatalBeanException)

Example 2 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class ComponentScanBeanDefinitionParser method createTypeFilter.

@SuppressWarnings("unchecked")
protected TypeFilter createTypeFilter(Element element, ClassLoader classLoader, ParserContext parserContext) {
    String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE);
    String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE);
    expression = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(expression);
    try {
        if ("annotation".equals(filterType)) {
            return new AnnotationTypeFilter((Class<Annotation>) classLoader.loadClass(expression));
        } else if ("assignable".equals(filterType)) {
            return new AssignableTypeFilter(classLoader.loadClass(expression));
        } else if ("aspectj".equals(filterType)) {
            return new AspectJTypeFilter(expression, classLoader);
        } else if ("regex".equals(filterType)) {
            return new RegexPatternTypeFilter(Pattern.compile(expression));
        } else if ("custom".equals(filterType)) {
            Class<?> filterClass = classLoader.loadClass(expression);
            if (!TypeFilter.class.isAssignableFrom(filterClass)) {
                throw new IllegalArgumentException("Class is not assignable to [" + TypeFilter.class.getName() + "]: " + expression);
            }
            return (TypeFilter) BeanUtils.instantiateClass(filterClass);
        } else {
            throw new IllegalArgumentException("Unsupported filter type: " + filterType);
        }
    } catch (ClassNotFoundException ex) {
        throw new FatalBeanException("Type filter class not found: " + expression, ex);
    }
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) AspectJTypeFilter(org.springframework.core.type.filter.AspectJTypeFilter) FatalBeanException(org.springframework.beans.FatalBeanException) TypeFilter(org.springframework.core.type.filter.TypeFilter) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter) AspectJTypeFilter(org.springframework.core.type.filter.AspectJTypeFilter) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter) Annotation(java.lang.annotation.Annotation) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Example 3 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testNoSuchInitMethod.

@Test
public void testNoSuchInitMethod() throws Exception {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
    try {
        xbf.getBean("init-method3");
        fail();
    } catch (FatalBeanException ex) {
        // check message is helpful
        assertTrue(ex.getMessage().indexOf("initializers.xml") != -1);
        assertTrue(ex.getMessage().indexOf("init-method3") != -1);
        assertTrue(ex.getMessage().indexOf("init") != -1);
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) FatalBeanException(org.springframework.beans.FatalBeanException) Test(org.junit.Test)

Example 4 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class ServiceLocatorFactoryBeanTests method testRequiresListableBeanFactoryAndChokesOnAnythingElse.

@Test
public void testRequiresListableBeanFactoryAndChokesOnAnythingElse() throws Exception {
    BeanFactory beanFactory = mock(BeanFactory.class);
    try {
        ServiceLocatorFactoryBean factory = new ServiceLocatorFactoryBean();
        factory.setBeanFactory(beanFactory);
    } catch (FatalBeanException ex) {
    // expected
    }
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) FatalBeanException(org.springframework.beans.FatalBeanException) Test(org.junit.Test)

Example 5 with FatalBeanException

use of org.springframework.beans.FatalBeanException in project spring-framework by spring-projects.

the class ContextNamespaceHandlerTests method propertyPlaceholderLocationWithSystemPropertyMissing.

@Test
public void propertyPlaceholderLocationWithSystemPropertyMissing() throws Exception {
    try {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("contextNamespaceHandlerTests-location-placeholder.xml", getClass());
        assertEquals("bar", applicationContext.getBean("foo"));
        assertEquals("foo", applicationContext.getBean("bar"));
        assertEquals("maps", applicationContext.getBean("spam"));
    } catch (FatalBeanException ex) {
        assertTrue(ex.getRootCause() instanceof FileNotFoundException);
    }
}
Also used : GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

FatalBeanException (org.springframework.beans.FatalBeanException)17 Test (org.junit.Test)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 BeansException (org.springframework.beans.BeansException)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 ReversePropertySourcesStandardServletEnvironment (com.hevelian.activemq.support.ReversePropertySourcesStandardServletEnvironment)1 ConfigObject (groovy.util.ConfigObject)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 PostConstruct (javax.annotation.PostConstruct)1 ResourceXmlApplicationContext (org.apache.xbean.spring.context.ResourceXmlApplicationContext)1 BeanInstantiationException (org.springframework.beans.BeanInstantiationException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1