Search in sources :

Example 36 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class RequiredAnnotationBeanPostProcessorTests method testWithCustomAnnotation.

@Test
public void testWithCustomAnnotation() {
    try {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).getBeanDefinition();
        factory.registerBeanDefinition("testBean", beanDef);
        RequiredAnnotationBeanPostProcessor rabpp = new RequiredAnnotationBeanPostProcessor();
        rabpp.setRequiredAnnotationType(MyRequired.class);
        factory.addBeanPostProcessor(rabpp);
        factory.preInstantiateSingletons();
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        String message = ex.getCause().getMessage();
        assertTrue(message.contains("Property"));
        assertTrue(message.contains("name"));
        assertTrue(message.contains("testBean"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 37 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class RequiredAnnotationBeanPostProcessorTests method testWithAllRequiredPropertiesSpecified.

@Test
public void testWithAllRequiredPropertiesSpecified() {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).addPropertyValue("age", "24").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
    factory.registerBeanDefinition("testBean", beanDef);
    factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
    factory.preInstantiateSingletons();
    RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
    assertEquals(24, bean.getAge());
    assertEquals("Blue", bean.getFavouriteColour());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 38 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project spring-framework by spring-projects.

the class RequiredAnnotationBeanPostProcessorTests method testWithStaticFactoryMethodAndRequiredPropertiesSpecified.

@Test
public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    BeanDefinition beanDef = BeanDefinitionBuilder.genericBeanDefinition(RequiredTestBean.class).setFactoryMethod("create").addPropertyValue("age", "24").addPropertyValue("favouriteColour", "Blue").addPropertyValue("jobTitle", "Grand Poobah").getBeanDefinition();
    factory.registerBeanDefinition("testBean", beanDef);
    factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
    factory.preInstantiateSingletons();
    RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
    assertEquals(24, bean.getAge());
    assertEquals("Blue", bean.getFavouriteColour());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 39 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.

the class CamelNamespaceHandler method registerEndpoint.

private void registerEndpoint(Element childElement, ParserContext parserContext, String contextId) {
    String id = childElement.getAttribute("id");
    // must have an id to be registered
    if (ObjectHelper.isNotEmpty(id)) {
        BeanDefinition definition = endpointParser.parse(childElement, parserContext);
        definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
        // Need to add this dependency of CamelContext for Spring 3.0
        try {
            Method method = definition.getClass().getMethod("setDependsOn", String[].class);
            method.invoke(definition, (Object) new String[] { contextId });
        } catch (Exception e) {
        // Do nothing here
        }
        parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id));
    }
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Method(java.lang.reflect.Method) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) JAXBException(javax.xml.bind.JAXBException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Example 40 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project camel by apache.

the class CamelNamespaceHandler method addDependsOnToRouteBuilder.

private void addDependsOnToRouteBuilder(Element childElement, ParserContext parserContext, String contextId) {
    // setting the depends-on explicitly is required since Spring 3.0
    String routeBuilderName = childElement.getAttribute("ref");
    if (ObjectHelper.isNotEmpty(routeBuilderName)) {
        // set depends-on to the context for a routeBuilder bean
        try {
            BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(routeBuilderName);
            Method getDependsOn = definition.getClass().getMethod("getDependsOn", new Class[] {});
            String[] dependsOn = (String[]) getDependsOn.invoke(definition);
            if (dependsOn == null || dependsOn.length == 0) {
                dependsOn = new String[] { contextId };
            } else {
                String[] temp = new String[dependsOn.length + 1];
                System.arraycopy(dependsOn, 0, temp, 0, dependsOn.length);
                temp[dependsOn.length] = contextId;
                dependsOn = temp;
            }
            Method method = definition.getClass().getMethod("setDependsOn", String[].class);
            method.invoke(definition, (Object) dependsOn);
        } catch (Exception e) {
        // Do nothing here
        }
    }
}
Also used : Method(java.lang.reflect.Method) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) JAXBException(javax.xml.bind.JAXBException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)593 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)117 Test (org.junit.jupiter.api.Test)105 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)73 Test (org.junit.Test)70 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)54 ManagedList (org.springframework.beans.factory.support.ManagedList)40 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)35 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)33 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)33 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)29 ArrayList (java.util.ArrayList)28 Method (java.lang.reflect.Method)27 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)27