Search in sources :

Example 16 with BeanDefinition

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

the class AbstractListenerContainerParser method parseListener.

private void parseListener(Element containerEle, Element listenerEle, ParserContext parserContext, PropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {
    RootBeanDefinition listenerDef = new RootBeanDefinition();
    listenerDef.setSource(parserContext.extractSource(listenerEle));
    listenerDef.setBeanClassName("org.springframework.jms.listener.adapter.MessageListenerAdapter");
    String ref = listenerEle.getAttribute(REF_ATTRIBUTE);
    if (!StringUtils.hasText(ref)) {
        parserContext.getReaderContext().error("Listener 'ref' attribute contains empty value.", listenerEle);
    } else {
        listenerDef.getPropertyValues().add("delegate", new RuntimeBeanReference(ref));
    }
    String method = null;
    if (listenerEle.hasAttribute(METHOD_ATTRIBUTE)) {
        method = listenerEle.getAttribute(METHOD_ATTRIBUTE);
        if (!StringUtils.hasText(method)) {
            parserContext.getReaderContext().error("Listener 'method' attribute contains empty value.", listenerEle);
        }
    }
    listenerDef.getPropertyValues().add("defaultListenerMethod", method);
    PropertyValue messageConverterPv = commonContainerProperties.getPropertyValue("messageConverter");
    if (messageConverterPv != null) {
        listenerDef.getPropertyValues().addPropertyValue(messageConverterPv);
    }
    BeanDefinition containerDef = createContainer(containerEle, listenerEle, parserContext, commonContainerProperties, specificContainerProperties);
    containerDef.getPropertyValues().add("messageListener", listenerDef);
    if (listenerEle.hasAttribute(RESPONSE_DESTINATION_ATTRIBUTE)) {
        String responseDestination = listenerEle.getAttribute(RESPONSE_DESTINATION_ATTRIBUTE);
        Boolean pubSubDomain = (Boolean) commonContainerProperties.getPropertyValue("replyPubSubDomain").getValue();
        listenerDef.getPropertyValues().add(pubSubDomain ? "defaultResponseTopicName" : "defaultResponseQueueName", responseDestination);
        if (containerDef.getPropertyValues().contains("destinationResolver")) {
            listenerDef.getPropertyValues().add("destinationResolver", containerDef.getPropertyValues().getPropertyValue("destinationResolver").getValue());
        }
    }
    String containerBeanName = listenerEle.getAttribute(ID_ATTRIBUTE);
    // If no bean id is given auto generate one using the ReaderContext's BeanNameGenerator
    if (!StringUtils.hasText(containerBeanName)) {
        containerBeanName = parserContext.getReaderContext().generateBeanName(containerDef);
    }
    // Register the listener and fire event
    parserContext.registerBeanComponent(new BeanComponentDefinition(containerDef, containerBeanName));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 17 with BeanDefinition

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

the class JeeNamespaceHandlerTests method testComplexRemoteSlsb.

@Test
public void testComplexRemoteSlsb() throws Exception {
    BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complexRemoteEjb");
    assertEquals(SimpleRemoteStatelessSessionProxyFactoryBean.class.getName(), beanDefinition.getBeanClassName());
    assertPropertyValue(beanDefinition, "businessInterface", ITestBean.class.getName());
    assertPropertyValue(beanDefinition, "jndiName", "ejb/MyRemoteBean");
    assertPropertyValue(beanDefinition, "cacheHome", "true");
    assertPropertyValue(beanDefinition, "lookupHomeOnStartup", "true");
    assertPropertyValue(beanDefinition, "resourceRef", "true");
    assertPropertyValue(beanDefinition, "jndiEnvironment", "foo=bar");
    assertPropertyValue(beanDefinition, "homeInterface", "org.springframework.tests.sample.beans.ITestBean");
    assertPropertyValue(beanDefinition, "refreshHomeOnConnectFailure", "true");
    assertPropertyValue(beanDefinition, "cacheSessionBean", "true");
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) SimpleRemoteStatelessSessionProxyFactoryBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 18 with BeanDefinition

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

the class JeeNamespaceHandlerTests method testComplexLocalSlsb.

@Test
public void testComplexLocalSlsb() throws Exception {
    BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complexLocalEjb");
    assertEquals(LocalStatelessSessionProxyFactoryBean.class.getName(), beanDefinition.getBeanClassName());
    assertPropertyValue(beanDefinition, "businessInterface", ITestBean.class.getName());
    assertPropertyValue(beanDefinition, "jndiName", "ejb/MyLocalBean");
    assertPropertyValue(beanDefinition, "cacheHome", "true");
    assertPropertyValue(beanDefinition, "lookupHomeOnStartup", "true");
    assertPropertyValue(beanDefinition, "resourceRef", "true");
    assertPropertyValue(beanDefinition, "jndiEnvironment", "foo=bar");
}
Also used : LocalStatelessSessionProxyFactoryBean(org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 19 with BeanDefinition

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

the class ScheduledAnnotationBeanPostProcessorTests method expressionWithCron.

@Test
public void expressionWithCron() {
    String businessHoursCronExpression = "0 0 9-17 * * MON-FRI";
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(ExpressionWithCronTestBean.class);
    context.registerBeanDefinition("postProcessor", processorDefinition);
    context.registerBeanDefinition("target", targetDefinition);
    Map<String, String> schedules = new HashMap<>();
    schedules.put("businessHours", businessHoursCronExpression);
    context.getBeanFactory().registerSingleton("schedules", schedules);
    context.refresh();
    Object postProcessor = context.getBean("postProcessor");
    Object target = context.getBean("target");
    ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
    @SuppressWarnings("unchecked") List<CronTask> cronTasks = (List<CronTask>) new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
    assertEquals(1, cronTasks.size());
    CronTask task = cronTasks.get(0);
    ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
    Object targetObject = runnable.getTarget();
    Method targetMethod = runnable.getMethod();
    assertEquals(target, targetObject);
    assertEquals("x", targetMethod.getName());
    assertEquals(businessHoursCronExpression, task.getExpression());
}
Also used : HashMap(java.util.HashMap) CronTask(org.springframework.scheduling.config.CronTask) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Test(org.junit.Test)

Example 20 with BeanDefinition

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

the class ScheduledAnnotationBeanPostProcessorTests method propertyPlaceholderWithFixedDelay.

@Test
public void propertyPlaceholderWithFixedDelay() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
    Properties properties = new Properties();
    properties.setProperty("fixedDelay", "5000");
    properties.setProperty("initialDelay", "1000");
    placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
    BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedDelayTestBean.class);
    context.registerBeanDefinition("postProcessor", processorDefinition);
    context.registerBeanDefinition("placeholder", placeholderDefinition);
    context.registerBeanDefinition("target", targetDefinition);
    context.refresh();
    Object postProcessor = context.getBean("postProcessor");
    Object target = context.getBean("target");
    ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
    @SuppressWarnings("unchecked") List<IntervalTask> fixedDelayTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
    assertEquals(1, fixedDelayTasks.size());
    IntervalTask task = fixedDelayTasks.get(0);
    ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
    Object targetObject = runnable.getTarget();
    Method targetMethod = runnable.getMethod();
    assertEquals(target, targetObject);
    assertEquals("fixedDelay", targetMethod.getName());
    assertEquals(1000L, task.getInitialDelay());
    assertEquals(5000L, task.getInterval());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Properties(java.util.Properties) Test(org.junit.Test)

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