Refine search
/** * Finish the initialization of this context's bean factory, * initializing all remaining singleton beans. */ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // Initialize conversion service for this context. if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } // Register a default embedded value resolver if no bean post-processor // (such as a PropertyPlaceholderConfigurer bean) registered any before: // at this point, primarily for resolution in annotation attribute values. if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); } // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early. String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. beanFactory.setTempClassLoader(null); // Allow for caching all bean definition metadata, not expecting further changes. beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. beanFactory.preInstantiateSingletons(); }
public static void main(String[] args) throws Exception { AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("/META-INF/spring/integration/orderProcessingSample.xml", BookOrderProcessingTestApp.class); MessageChannel messageChannel = (MessageChannel) applicationContext.getBean("ordersChannel"); GenericMessage<Document> orderMessage = createXmlMessageFromResource("META-INF/spring/integration/order.xml"); messageChannel.send(orderMessage); applicationContext.close(); }
public static Ethereum createEthereum(Class ... springConfigs) { logger.info("Starting EthereumJ..."); AbstractApplicationContext context = new AnnotationConfigApplicationContext(springConfigs); context.registerShutdownHook(); return context.getBean(Ethereum.class); } }
/** * Tell the subclass to refresh the internal bean factory. * @return the fresh BeanFactory instance * @see #refreshBeanFactory() * @see #getBeanFactory() */ protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); return getBeanFactory(); }
@Override public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) { assertBeanFactoryActive(); return getBeanFactory().getBeanNamesForAnnotation(annotationType); }
@Override public Object getBean(String name) throws BeansException { assertBeanFactoryActive(); return getBeanFactory().getBean(name); }
new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/expression-advice-context.xml"); context.registerShutdownHook(); SessionFactory<FTPFile> sessionFactory = context.getBean(SessionFactory.class); SourcePollingChannelAdapter fileInbound = context.getBean(SourcePollingChannelAdapter.class); context.close(); System.exit(0);
@Test public void testAsyncMulticaster() throws Exception { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "messageBusWithAsyncEventMulticaster.xml", this.getClass()); context.refresh(); SimpleApplicationEventMulticaster multicaster = (SimpleApplicationEventMulticaster) context.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME); DirectFieldAccessor accessor = new DirectFieldAccessor(multicaster); Object taskExecutor = accessor.getPropertyValue("taskExecutor"); assertEquals(ThreadPoolTaskExecutor.class, taskExecutor.getClass()); context.close(); }
public static void main(String[] args) throws Exception { LOGGER.info("\n=========================================================" + "\n " + "\n Welcome to Spring Integration! " + "\n " + "\n For more information please visit: " + "\n http://www.springsource.org/spring-integration " + "\n " + "\n=========================================================" ); final AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/stateful-retry-advice-context.xml"); context.registerShutdownHook(); LOGGER.info("\n=========================================================" + "\n " + "\n This is the Stateful Sample - " + "\n " + "\n Please enter some text and press return. " + "\n 'fail 2' will fail twice, then succeed " + "\n 'fail 3' will fail and never succeed " + "\n Demo will terminate in 60 seconds. " + "\n " + "\n=========================================================" ); Thread.sleep(60000); context.close(); }
/** * Callback for destruction of this instance, originally attached * to a {@code DisposableBean} implementation (not anymore in 5.0). * <p>The {@link #close()} method is the native way to shut down * an ApplicationContext, which this method simply delegates to. * @deprecated as of Spring Framework 5.0, in favor of {@link #close()} */ @Deprecated public void destroy() { close(); }
@Test public void testProcessMessageWithParameterCoercionToNonPrimitive() throws Exception { class TestTarget { @SuppressWarnings("unused") public String find(Resource[] resources) { return Arrays.toString(resources); } } Expression expression = expressionParser.parseExpression("#target.find(payload)"); ExpressionEvaluatingMessageProcessor<String> processor = new ExpressionEvaluatingMessageProcessor<>(expression); AbstractApplicationContext applicationContext = new GenericApplicationContext(); processor.setBeanFactory(applicationContext); IntegrationEvaluationContextFactoryBean factoryBean = new IntegrationEvaluationContextFactoryBean(); factoryBean.setApplicationContext(applicationContext); applicationContext.getBeanFactory() .registerSingleton(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME, factoryBean.getObject()); applicationContext.refresh(); processor.afterPropertiesSet(); EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class); evaluationContext.setVariable("target", new TestTarget()); String result = processor.processMessage(new GenericMessage<>("classpath*:*-test.xml")); assertTrue("Wrong result: " + result, result.contains("log4j2-test.xml")); }
@Override public String[] getBeanNamesForType(ResolvableType type) { assertBeanFactoryActive(); return getBeanFactory().getBeanNamesForType(type); }
/** * place some orders * @param context spring context * @param count the number of standard orders */ public static void order(AbstractApplicationContext context, int count){ Cafe cafe = (Cafe) context.getBean("cafe"); for (int i = 1; i <= 100; i++) { Order order = new Order(i); order.addItem(DrinkType.LATTE, 2, false); order.addItem(DrinkType.MOCHA, 3, true); cafe.placeOrder(order); } }
@Test public void testBeansStop() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("lifecycleTests.xml", getClass()); context.start(); LifecycleTestBean bean1 = (LifecycleTestBean) context.getBean("bean1"); LifecycleTestBean bean2 = (LifecycleTestBean) context.getBean("bean2"); LifecycleTestBean bean3 = (LifecycleTestBean) context.getBean("bean3"); LifecycleTestBean bean4 = (LifecycleTestBean) context.getBean("bean4"); String startError = "bean was not started"; assertTrue(startError, bean1.isRunning()); assertTrue(startError, bean2.isRunning()); assertTrue(startError, bean3.isRunning()); assertTrue(startError, bean4.isRunning()); context.stop(); String stopError = "bean was not stopped"; assertFalse(stopError, bean1.isRunning()); assertFalse(stopError, bean2.isRunning()); assertFalse(stopError, bean3.isRunning()); assertFalse(stopError, bean4.isRunning()); }
@Test public void testBeansStart() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("lifecycleTests.xml", getClass()); context.start(); LifecycleTestBean bean1 = (LifecycleTestBean) context.getBean("bean1"); LifecycleTestBean bean2 = (LifecycleTestBean) context.getBean("bean2"); LifecycleTestBean bean3 = (LifecycleTestBean) context.getBean("bean3"); LifecycleTestBean bean4 = (LifecycleTestBean) context.getBean("bean4"); String error = "bean was not started"; assertTrue(error, bean1.isRunning()); assertTrue(error, bean2.isRunning()); assertTrue(error, bean3.isRunning()); assertTrue(error, bean4.isRunning()); }
@Override public boolean containsBean(String name) { return getBeanFactory().containsBean(name); }
registrar.registerBeanDefinitions(null, (BeanDefinitionRegistry) context.getBeanFactory()); context.refresh(); handler.setBeanFactory(context); handler.setUriVariablesExpression(new SpelExpressionParser().parseExpression("headers.uriVariables")); context.close();
@Override public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException { assertBeanFactoryActive(); return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit); }
private ServerServiceDefinition bindInterceptors(ServerServiceDefinition serviceDefinition, GRpcService gRpcService, Collection<ServerInterceptor> globalInterceptors) { Stream<? extends ServerInterceptor> privateInterceptors = Stream.of(gRpcService.interceptors()) .map(interceptorClass -> { try { return 0 < applicationContext.getBeanNamesForType(interceptorClass).length ? applicationContext.getBean(interceptorClass) : interceptorClass.newInstance(); } catch (Exception e) { throw new BeanCreationException("Failed to create interceptor instance.", e); } }); List<ServerInterceptor> interceptors = Stream.concat( gRpcService.applyGlobalInterceptors() ? globalInterceptors.stream() : Stream.empty(), privateInterceptors) .distinct() .sorted(serverInterceptorOrderComparator()) .collect(Collectors.toList()); return ServerInterceptors.intercept(serviceDefinition, interceptors); }
recoveryCheckpoint.getCheckpointDir().setBase(getCheckpointsDir()); recoveryCheckpoint.getCheckpointDir().setPath(selectedCheckpoint); recoveryCheckpoint.getCheckpointDir().setConfigurer(appCtx.getBean(ConfigPathConfigurer.class)); recoveryCheckpoint.afterPropertiesSet(); setRecoveryCheckpoint(recoveryCheckpoint); Map<String,Checkpointable> toSetRecovery = appCtx.getBeansOfType(Checkpointable.class);