private SpringContainedBean<?> createBean( String name, Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { try { if (lifecycleOptions.useJpaCompliantCreation()) { Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); this.beanFactory.applyBeanPropertyValues(bean, name); bean = this.beanFactory.initializeBean(bean, name); return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance)); } else { return new SpringContainedBean<>(this.beanFactory.getBean(name, beanType)); } } catch (BeansException ex) { if (logger.isDebugEnabled()) { logger.debug("Falling back to Hibernate's default producer after bean creation failure for " + beanType + ": " + ex); } return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(name, beanType)); } }
beanFactory.initializeBean(beanInstance, (beanName != null ? beanName : ""));
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholders() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); context.getBeanFactory().initializeBean(formattingService, "formattingService"); formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
beanFactory.initializeBean(beanInstance, (beanName != null ? beanName : ""));
private SpringContainedBean<?> createBean( String name, Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { try { if (lifecycleOptions.useJpaCompliantCreation()) { Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false); this.beanFactory.applyBeanPropertyValues(bean, name); bean = this.beanFactory.initializeBean(bean, name); return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance)); } else { return new SpringContainedBean<>(this.beanFactory.getBean(name, beanType)); } } catch (BeansException ex) { if (logger.isDebugEnabled()) { logger.debug("Falling back to Hibernate's default producer after bean creation failure for " + beanType + ": " + ex); } return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(name, beanType)); } }
private <T> T initialiseObject(ConfigurableApplicationContext applicationContext, String key, T object) throws LifecycleException { applicationContext.getBeanFactory().autowireBean(object); T initialised = (T) applicationContext.getBeanFactory().initializeBean(object, key); return initialised; }
this.beanFactory.registerSingleton(messageSourceBeanName, methodInvokingMessageSource); messageSource = (MessageSource<?>) this.beanFactory .initializeBean(methodInvokingMessageSource, messageSourceBeanName); if (this.disposables != null) { this.disposables.add(methodInvokingMessageSource);
private void initialize() { BeanFactory beanFactory = getBeanFactory(); if (beanFactory instanceof ConfigurableListableBeanFactory) { ((ConfigurableListableBeanFactory) beanFactory).initializeBean(this.gatewayProxyFactoryBean, getComponentName() + "#gpfb"); } try { this.exchanger = (RequestReplyExchanger) this.gatewayProxyFactoryBean.getObject(); } catch (Exception e) { throw new BeanCreationException("Can't instantiate the GatewayProxyFactoryBean: " + this, e); } if (this.running) { // We must stop gatewayProxyFactoryBean because after the normal start its "gatewayMap" is still empty this.gatewayProxyFactoryBean.stop(); this.gatewayProxyFactoryBean.start(); } }
protected AbstractEndpoint createEndpoint(MessageHandler handler, Method method, List<Annotation> annotations) { AbstractEndpoint endpoint = null; String inputChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, getInputChannelAttribute(), String.class); if (StringUtils.hasText(inputChannelName)) { MessageChannel inputChannel; try { inputChannel = this.channelResolver.resolveDestination(inputChannelName); } catch (DestinationResolutionException e) { if (e.getCause() instanceof NoSuchBeanDefinitionException) { inputChannel = new DirectChannel(); this.beanFactory.registerSingleton(inputChannelName, inputChannel); inputChannel = (MessageChannel) this.beanFactory.initializeBean(inputChannel, inputChannelName); if (this.disposables != null) { this.disposables.add((DisposableBean) inputChannel); } } else { throw e; } } Assert.notNull(inputChannel, "failed to resolve inputChannel '" + inputChannelName + "'"); endpoint = doCreateEndpoint(handler, inputChannel, annotations); } return endpoint; }
beanFactory.initializeBean(embeddedKafkaBroker, EmbeddedKafkaBroker.BEAN_NAME); beanFactory.registerSingleton(EmbeddedKafkaBroker.BEAN_NAME, embeddedKafkaBroker); ((DefaultSingletonBeanRegistry) beanFactory).registerDisposableBean(EmbeddedKafkaBroker.BEAN_NAME, embeddedKafkaBroker);
@Test public void testDynamicMessageChannelBeanWithAutoGlobalChannelInterceptor() { DirectChannel testChannel = new DirectChannel(); ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory(); beanFactory.initializeBean(testChannel, "testChannel"); List<ChannelInterceptor> channelInterceptors = testChannel.getChannelInterceptors(); assertEquals(2, channelInterceptors.size()); assertThat(channelInterceptors.get(0), instanceOf(SampleInterceptor.class)); assertThat(channelInterceptors.get(0), instanceOf(SampleInterceptor.class)); }
endpoint.setBeanName(endpointBeanName); getBeanFactory().registerSingleton(endpointBeanName, endpoint); getBeanFactory().initializeBean(endpoint, endpointBeanName);
@Test public void testAutoCreateChannel() { ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class, withSettings().extraInterfaces(BeanDefinitionRegistry.class)); given(beanFactory.getBean("channel", MessageChannel.class)).willThrow(NoSuchBeanDefinitionException.class); willAnswer(invocation -> invocation.getArgument(0)) .given(beanFactory).initializeBean(any(DirectChannel.class), eq("channel")); willAnswer(invocation -> invocation.getArgument(0)) .given(beanFactory).initializeBean(any(MessageHandler.class), eq("foo.foo.serviceActivator.handler")); MessagingAnnotationPostProcessor mapp = new MessagingAnnotationPostProcessor(); mapp.setBeanFactory(beanFactory); mapp.afterPropertiesSet(); mapp.postProcessAfterInitialization(new Foo(), "foo"); verify(beanFactory).registerSingleton(eq("channel"), any(DirectChannel.class)); }
@Test public void testSourcePollingChannelAdapterOutputChannelLateBinding() { QueueChannel testChannel = new QueueChannel(); ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) this.context.getAutowireCapableBeanFactory(); beanFactory.registerSingleton("lateBindingChannel", testChannel); beanFactory.initializeBean(testChannel, "lateBindingChannel"); this.autoCreatedChannelMessageSourceAdapter.start(); Message<?> receive = testChannel.receive(10000); assertNotNull(receive); assertEquals("bar", receive.getPayload()); this.autoCreatedChannelMessageSourceAdapter.stop(); }
ConfigurableBeanFactory.SCOPE_PROTOTYPE.equals( this.beanFactory.getBeanDefinition(beanNameToUse).getScope())) { this.beanFactory.initializeBean(componentToUse, beanNameToUse);
Object result = this.beanFactory.initializeBean(beanInstance, bwi.getBeanName()); checkExposedObject(result, beanInstance);
@Test public void testDontCreateChannelWhenChannelHasBadDefinition() { ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class, withSettings().extraInterfaces(BeanDefinitionRegistry.class)); given(beanFactory.getBean("channel", MessageChannel.class)).willThrow(BeanCreationException.class); willAnswer(invocation -> invocation.getArgument(0)) .given(beanFactory).initializeBean(any(DirectChannel.class), eq("channel")); willAnswer(invocation -> invocation.getArgument(0)) .given(beanFactory).initializeBean(any(MessageHandler.class), eq("foo.foo.serviceActivator.handler")); MessagingAnnotationPostProcessor mapp = new MessagingAnnotationPostProcessor(); mapp.setBeanFactory(beanFactory); mapp.afterPropertiesSet(); try { mapp.postProcessAfterInitialization(new Foo(), "foo"); fail("Expected a DestinationResolutionException"); } catch (DestinationResolutionException e) { assertThat(e.getMessage(), containsString("A bean definition with name 'channel' exists, but failed to be created")); } }
handler = (MessageHandler) this.beanFactory.initializeBean(handler, handlerBeanName); if (handler instanceof DisposableBean && this.disposables != null) { this.disposables.add((DisposableBean) handler);
private <T> T initialiseObject(ConfigurableApplicationContext applicationContext, String key, T object) throws LifecycleException { applicationContext.getBeanFactory().autowireBean(object); T initialised = (T) applicationContext.getBeanFactory().initializeBean(object, key); return initialised; }
@Override public void registerSingleton(String beanName, Object bean, Class<?> type) { log.debug("registering bean '{}' of type '{}'", beanName, type.getName()); context.getBeanFactory().initializeBean(bean, beanName); context.getBeanFactory().registerSingleton(beanName, bean); // beans that get pushed straight into the context need to be attached to destructive bean post processors context.getDefaultListableBeanFactory().registerDisposableBean( beanName, new DisposableBeanAdapter(bean, beanName, context)); }