Search in sources :

Example 46 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project leopard by tanhaichao.

the class LeopardBeanFactoryAware method registerBean.

// @PreDestroy
// public void destroy() {
// beanFactory = null;
// }
/**
 * @desc 向spring容器注册bean
 *
 * @param beanName
 *
 * @param beanDefinition
 */
private static void registerBean(String beanName, BeanDefinition beanDefinition) {
    BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) beanFactory;
    beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 47 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-cloud-config by spring-cloud.

the class CompositeEnvironmentBeanFactoryPostProcessor method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    List<String> typePropertyList = CompositeUtils.getCompositeTypeList(environment);
    for (int i = 0; i < typePropertyList.size(); i++) {
        String type = typePropertyList.get(i);
        String factoryName = CompositeUtils.getFactoryName(type, beanFactory);
        Type[] factoryTypes = CompositeUtils.getEnvironmentRepositoryFactoryTypeParams(beanFactory, factoryName);
        Class<? extends EnvironmentRepositoryProperties> propertiesClass = (Class<? extends EnvironmentRepositoryProperties>) factoryTypes[1];
        EnvironmentRepositoryProperties properties = bindProperties(i, propertiesClass, environment);
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(EnvironmentRepository.class).setFactoryMethodOnBean("build", factoryName).addConstructorArgValue(properties).getBeanDefinition();
        String beanName = String.format("%s-env-repo%d", type, i);
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        registry.registerBeanDefinition(beanName, beanDefinition);
    }
}
Also used : EnvironmentRepository(org.springframework.cloud.config.server.environment.EnvironmentRepository) Type(java.lang.reflect.Type) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) EnvironmentRepositoryProperties(org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 48 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project com.revolsys.open by revolsys.

the class ModuleImport method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (beanFactory instanceof BeanDefinitionRegistry) {
        final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        postProcessBeanDefinitionRegistry(registry);
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry)

Example 49 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-integration by spring-projects.

the class MicrometerMetricsTests method testSend.

@Test
public void testSend() {
    GenericMessage<String> message = new GenericMessage<>("foo");
    this.channel.send(message);
    try {
        this.channel.send(this.source.receive());
        fail("Expected exception");
    } catch (MessagingException e) {
        assertThat(e.getCause().getMessage()).isEqualTo("testErrorCount");
    }
    this.channel2.send(message);
    this.queue.send(message);
    this.queue.send(message);
    this.queue.receive();
    this.badPoll.send(message);
    try {
        this.badPoll.receive();
        fail("Expected exception");
    } catch (RuntimeException e) {
        assertThat(e.getMessage()).isEqualTo("badPoll");
    }
    nullChannel.send(message);
    MeterRegistry registry = this.meterRegistry;
    assertThat(registry.get("spring.integration.channels").gauge().value()).isEqualTo(6);
    assertThat(registry.get("spring.integration.handlers").gauge().value()).isEqualTo(3);
    assertThat(registry.get("spring.integration.sources").gauge().value()).isEqualTo(1);
    assertThat(registry.get("spring.integration.receive").tag("name", "source").tag("result", "success").counter().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.receive").tag("name", "badPoll").tag("result", "failure").counter().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.send").tag("name", "eipBean.handler").tag("result", "success").timer().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.send").tag("name", "eipMethod.handler").tag("result", "success").timer().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.send").tag("name", "channel").tag("result", "success").timer().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.send").tag("name", "channel").tag("result", "failure").timer().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.send").tag("name", "eipMethod.handler").tag("result", "failure").timer().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.receive").tag("name", "queue").tag("result", "success").counter().count()).isEqualTo(1);
    assertThat(registry.get("spring.integration.send").tag("name", "nullChannel").tag("result", "success").timer().count()).isEqualTo(1);
    BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) this.context.getBeanFactory();
    beanFactory.registerBeanDefinition("newChannel", BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class).getRawBeanDefinition());
    DirectChannel newChannel = this.context.getBean("newChannel", DirectChannel.class);
    newChannel.setBeanName("newChannel");
    newChannel.subscribe(m -> {
    });
    newChannel.send(new GenericMessage<>("foo"));
    assertThat(registry.get("spring.integration.send").tag("name", "newChannel").tag("result", "success").timer().count()).isEqualTo(1);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessagingException(org.springframework.messaging.MessagingException) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.Test)

Example 50 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-integration by spring-projects.

the class IntegrationNamespaceUtils method checkAndConfigureFixedSubscriberChannel.

@SuppressWarnings("unchecked")
public static void checkAndConfigureFixedSubscriberChannel(Element element, ParserContext parserContext, String channelName, String handlerBeanName) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    if (registry.containsBeanDefinition(channelName)) {
        BeanDefinition inputChannelDefinition = registry.getBeanDefinition(channelName);
        if (FixedSubscriberChannel.class.getName().equals(inputChannelDefinition.getBeanClassName())) {
            ConstructorArgumentValues constructorArgumentValues = inputChannelDefinition.getConstructorArgumentValues();
            if (constructorArgumentValues.isEmpty()) {
                constructorArgumentValues.addGenericArgumentValue(new RuntimeBeanReference(handlerBeanName));
            } else {
                parserContext.getReaderContext().error("Only one subscriber is allowed for a FixedSubscriberChannel.", element);
            }
        }
    } else {
        BeanDefinition bfppd;
        if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_FIXED_SUBSCRIBER_CHANNEL_BPP_BEAN_NAME)) {
            bfppd = new RootBeanDefinition(FixedSubscriberChannelBeanFactoryPostProcessor.class);
            registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_FIXED_SUBSCRIBER_CHANNEL_BPP_BEAN_NAME, bfppd);
        } else {
            bfppd = registry.getBeanDefinition(IntegrationContextUtils.INTEGRATION_FIXED_SUBSCRIBER_CHANNEL_BPP_BEAN_NAME);
        }
        ManagedMap<String, String> candidates;
        ValueHolder argumentValue = bfppd.getConstructorArgumentValues().getArgumentValue(0, Map.class);
        if (argumentValue == null) {
            candidates = new ManagedMap<String, String>();
            bfppd.getConstructorArgumentValues().addIndexedArgumentValue(0, candidates);
        } else {
            candidates = (ManagedMap<String, String>) argumentValue.getValue();
        }
        candidates.put(handlerBeanName, channelName);
    }
}
Also used : FixedSubscriberChannelBeanFactoryPostProcessor(org.springframework.integration.config.FixedSubscriberChannelBeanFactoryPostProcessor) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Aggregations

BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)86 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)36 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 Test (org.junit.jupiter.api.Test)15 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)13 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)13 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)12 ApplicationContext (org.springframework.context.ApplicationContext)10 Map (java.util.Map)9 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)8 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)7 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)7 Test (org.junit.Test)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)5 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)5 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)4 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4