Search in sources :

Example 1 with InitializingBean

use of org.springframework.beans.factory.InitializingBean in project tutorials by eugenp.

the class SpringSecurityActivitiApplication method usersAndGroupsInitializer.

@Bean
InitializingBean usersAndGroupsInitializer(IdentityService identityService) {
    return new InitializingBean() {

        public void afterPropertiesSet() throws Exception {
            User user = identityService.newUser("activiti_user");
            user.setPassword("pass");
            identityService.saveUser(user);
            Group group = identityService.newGroup("user");
            group.setName("ROLE_USER");
            group.setType("USER");
            identityService.saveGroup(group);
            identityService.createMembership(user.getId(), group.getId());
        }
    };
}
Also used : Group(org.activiti.engine.identity.Group) User(org.activiti.engine.identity.User) InitializingBean(org.springframework.beans.factory.InitializingBean) Bean(org.springframework.context.annotation.Bean) InitializingBean(org.springframework.beans.factory.InitializingBean)

Example 2 with InitializingBean

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

the class AmqpChannelFactoryBean method createInstance.

@Override
protected AbstractAmqpChannel createInstance() throws Exception {
    if (this.messageDriven) {
        AbstractMessageListenerContainer container = this.createContainer();
        if (this.amqpTemplate instanceof InitializingBean) {
            ((InitializingBean) this.amqpTemplate).afterPropertiesSet();
        }
        if (this.isPubSub) {
            PublishSubscribeAmqpChannel pubsub = new PublishSubscribeAmqpChannel(this.beanName, container, this.amqpTemplate, this.outboundHeaderMapper, this.inboundHeaderMapper);
            if (this.exchange != null) {
                pubsub.setExchange(this.exchange);
            }
            if (this.maxSubscribers != null) {
                pubsub.setMaxSubscribers(this.maxSubscribers);
            }
            this.channel = pubsub;
        } else {
            PointToPointSubscribableAmqpChannel p2p = new PointToPointSubscribableAmqpChannel(this.beanName, container, this.amqpTemplate, this.outboundHeaderMapper, this.inboundHeaderMapper);
            if (StringUtils.hasText(this.queueName)) {
                p2p.setQueueName(this.queueName);
            }
            if (this.maxSubscribers != null) {
                p2p.setMaxSubscribers(this.maxSubscribers);
            }
            this.channel = p2p;
        }
    } else {
        Assert.isTrue(!this.isPubSub, "An AMQP 'publish-subscribe-channel' must be message-driven.");
        PollableAmqpChannel pollable = new PollableAmqpChannel(this.beanName, this.amqpTemplate, this.outboundHeaderMapper, this.inboundHeaderMapper);
        if (this.amqpAdmin != null) {
            pollable.setAmqpAdmin(this.amqpAdmin);
        }
        if (StringUtils.hasText(this.queueName)) {
            pollable.setQueueName(this.queueName);
        }
        this.channel = pollable;
    }
    if (!CollectionUtils.isEmpty(this.interceptors)) {
        this.channel.setInterceptors(this.interceptors);
    }
    this.channel.setBeanName(this.beanName);
    if (this.getBeanFactory() != null) {
        this.channel.setBeanFactory(this.getBeanFactory());
    }
    if (this.defaultDeliveryMode != null) {
        this.channel.setDefaultDeliveryMode(this.defaultDeliveryMode);
    }
    if (this.extractPayload != null) {
        this.channel.setExtractPayload(this.extractPayload);
    }
    this.channel.setHeadersMappedLast(this.headersLast);
    this.channel.afterPropertiesSet();
    return this.channel;
}
Also used : PointToPointSubscribableAmqpChannel(org.springframework.integration.amqp.channel.PointToPointSubscribableAmqpChannel) PollableAmqpChannel(org.springframework.integration.amqp.channel.PollableAmqpChannel) InitializingBean(org.springframework.beans.factory.InitializingBean) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) PublishSubscribeAmqpChannel(org.springframework.integration.amqp.channel.PublishSubscribeAmqpChannel)

Example 3 with InitializingBean

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

the class AbstractSimpleMessageHandlerFactoryBean method createHandlerInternal.

protected final H createHandlerInternal() {
    synchronized (this.initializationMonitor) {
        if (this.initialized) {
            // There was a problem when this method was called already
            return null;
        }
        this.handler = createHandler();
        if (this.handler instanceof ApplicationContextAware && this.applicationContext != null) {
            ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext);
        }
        if (this.handler instanceof BeanFactoryAware && getBeanFactory() != null) {
            ((BeanFactoryAware) this.handler).setBeanFactory(getBeanFactory());
        }
        if (this.handler instanceof BeanNameAware && this.beanName != null) {
            ((BeanNameAware) this.handler).setBeanName(this.beanName);
        }
        if (this.handler instanceof ApplicationEventPublisherAware && this.applicationEventPublisher != null) {
            ((ApplicationEventPublisherAware) this.handler).setApplicationEventPublisher(this.applicationEventPublisher);
        }
        if (this.handler instanceof MessageProducer && this.outputChannel != null) {
            ((MessageProducer) this.handler).setOutputChannel(this.outputChannel);
        }
        Object actualHandler = extractTarget(this.handler);
        if (actualHandler == null) {
            actualHandler = this.handler;
        }
        if (actualHandler instanceof IntegrationObjectSupport) {
            if (this.componentName != null) {
                ((IntegrationObjectSupport) actualHandler).setComponentName(this.componentName);
            }
            if (this.channelResolver != null) {
                ((IntegrationObjectSupport) actualHandler).setChannelResolver(this.channelResolver);
            }
        }
        if (!CollectionUtils.isEmpty(this.adviceChain)) {
            if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
                ((AbstractReplyProducingMessageHandler) actualHandler).setAdviceChain(this.adviceChain);
            } else if (this.logger.isDebugEnabled()) {
                String name = this.componentName;
                if (name == null && actualHandler instanceof NamedComponent) {
                    name = ((NamedComponent) actualHandler).getComponentName();
                }
                this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler" + (name == null ? "" : (", " + name)) + ".");
            }
        }
        if (this.async != null) {
            if (actualHandler instanceof AbstractMessageProducingHandler) {
                ((AbstractMessageProducingHandler) actualHandler).setAsync(this.async);
            }
        }
        if (this.handler instanceof Orderable && this.order != null) {
            ((Orderable) this.handler).setOrder(this.order);
        }
        this.initialized = true;
    }
    if (this.handler instanceof InitializingBean) {
        try {
            ((InitializingBean) this.handler).afterPropertiesSet();
        } catch (Exception e) {
            throw new BeanInitializationException("failed to initialize MessageHandler", e);
        }
    }
    return this.handler;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) ApplicationContextAware(org.springframework.context.ApplicationContextAware) IntegrationObjectSupport(org.springframework.integration.context.IntegrationObjectSupport) Orderable(org.springframework.integration.context.Orderable) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) NamedComponent(org.springframework.integration.support.context.NamedComponent) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeansException(org.springframework.beans.BeansException) BeanNameAware(org.springframework.beans.factory.BeanNameAware) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) AbstractMessageProducingHandler(org.springframework.integration.handler.AbstractMessageProducingHandler) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageProducer(org.springframework.integration.core.MessageProducer) InitializingBean(org.springframework.beans.factory.InitializingBean)

Example 4 with InitializingBean

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

the class RedisQueueMessageDrivenEndpointTests method testInt3196Recovery.

@Test
@RedisAvailable
@Ignore("JedisConnectionFactory doesn't support proper 'destroy()' and allows to create new fresh Redis connection")
public void testInt3196Recovery() throws Exception {
    String queueName = "test.si.Int3196Recovery";
    QueueChannel channel = new QueueChannel();
    final List<ApplicationEvent> exceptionEvents = new ArrayList<>();
    final CountDownLatch exceptionsLatch = new CountDownLatch(2);
    RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
    endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
    endpoint.setApplicationEventPublisher(event -> {
        exceptionEvents.add((ApplicationEvent) event);
        exceptionsLatch.countDown();
    });
    endpoint.setOutputChannel(channel);
    endpoint.setReceiveTimeout(100);
    endpoint.setRecoveryInterval(200);
    endpoint.afterPropertiesSet();
    endpoint.start();
    waitListening(endpoint);
    ((DisposableBean) this.connectionFactory).destroy();
    assertTrue(exceptionsLatch.await(10, TimeUnit.SECONDS));
    for (ApplicationEvent exceptionEvent : exceptionEvents) {
        assertThat(exceptionEvent, Matchers.instanceOf(RedisExceptionEvent.class));
        assertSame(endpoint, exceptionEvent.getSource());
        assertThat(((IntegrationEvent) exceptionEvent).getCause().getClass(), Matchers.isIn(Arrays.asList(RedisSystemException.class, RedisConnectionFailureException.class)));
    }
    ((InitializingBean) this.connectionFactory).afterPropertiesSet();
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(this.getConnectionFactoryForTest());
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();
    String payload = "testing";
    redisTemplate.boundListOps(queueName).leftPush(payload);
    Message<?> receive = channel.receive(10000);
    assertNotNull(receive);
    assertEquals(payload, receive.getPayload());
    endpoint.stop();
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) QueueChannel(org.springframework.integration.channel.QueueChannel) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) ApplicationEvent(org.springframework.context.ApplicationEvent) ArrayList(java.util.ArrayList) RedisExceptionEvent(org.springframework.integration.redis.event.RedisExceptionEvent) CountDownLatch(java.util.concurrent.CountDownLatch) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) DisposableBean(org.springframework.beans.factory.DisposableBean) BeanFactory(org.springframework.beans.factory.BeanFactory) IntegrationEvent(org.springframework.integration.events.IntegrationEvent) InitializingBean(org.springframework.beans.factory.InitializingBean) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with InitializingBean

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

the class TestUtils method registerBean.

private static void registerBean(String beanName, Object bean, BeanFactory beanFactory) {
    Assert.notNull(beanName, "bean name must not be null");
    ConfigurableListableBeanFactory configurableListableBeanFactory = null;
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
    } else if (beanFactory instanceof GenericApplicationContext) {
        configurableListableBeanFactory = ((GenericApplicationContext) beanFactory).getBeanFactory();
    }
    if (bean instanceof BeanNameAware) {
        ((BeanNameAware) bean).setBeanName(beanName);
    }
    if (bean instanceof BeanFactoryAware) {
        ((BeanFactoryAware) bean).setBeanFactory(beanFactory);
    }
    if (bean instanceof InitializingBean) {
        try {
            ((InitializingBean) bean).afterPropertiesSet();
        } catch (Exception e) {
            throw new FatalBeanException("failed to register bean with test context", e);
        }
    }
    // NOSONAR false positive
    configurableListableBeanFactory.registerSingleton(beanName, bean);
}
Also used : BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) InitializingBean(org.springframework.beans.factory.InitializingBean) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) MessagingException(org.springframework.messaging.MessagingException) FatalBeanException(org.springframework.beans.FatalBeanException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanNameAware(org.springframework.beans.factory.BeanNameAware)

Aggregations

InitializingBean (org.springframework.beans.factory.InitializingBean)25 ApplicationContextAware (org.springframework.context.ApplicationContextAware)7 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Bean (org.springframework.context.annotation.Bean)4 DisposableBean (org.springframework.beans.factory.DisposableBean)3 DevelopmentException (com.haulmont.cuba.core.global.DevelopmentException)2 Group (org.activiti.engine.identity.Group)2 User (org.activiti.engine.identity.User)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 BeanNameAware (org.springframework.beans.factory.BeanNameAware)2 ProvisioningException (org.springframework.cloud.stream.provisioning.ProvisioningException)2 SpringSecurityGroupManager (com.baeldung.activiti.security.config.SpringSecurityGroupManager)1 SpringSecurityUserManager (com.baeldung.activiti.security.config.SpringSecurityUserManager)1 LoginException (com.haulmont.cuba.security.global.LoginException)1 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)1 LocalServiceAccessException (com.haulmont.cuba.web.sys.remoting.LocalServiceAccessException)1 PaintException (com.vaadin.server.PaintException)1 AnnotationBasedPluginContextConfigurer (io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer)1