Java Code Examples for org.springframework.context.support.StaticApplicationContext#registerBeanDefinition()
The following examples show how to use
org.springframework.context.support.StaticApplicationContext#registerBeanDefinition() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AsyncAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void taskExecutorByBeanType() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); context.registerBeanDefinition("postProcessor", processorDefinition); BeanDefinition executorDefinition = new RootBeanDefinition(ThreadPoolTaskExecutor.class); executorDefinition.getPropertyValues().add("threadNamePrefix", "testExecutor"); context.registerBeanDefinition("myExecutor", executorDefinition); BeanDefinition targetDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class); context.registerBeanDefinition("target", targetDefinition); context.refresh(); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertTrue(asyncThread.getName().startsWith("testExecutor")); context.close(); }
Example 2
Source File: AsyncAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void invokedAsynchronouslyOnProxyTarget() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("postProcessor", new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); TestBean tb = new TestBean(); ProxyFactory pf = new ProxyFactory(ITestBean.class, (MethodInterceptor) invocation -> invocation.getMethod().invoke(tb, invocation.getArguments())); context.registerBean("target", ITestBean.class, () -> (ITestBean) pf.getProxy()); context.refresh(); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); Thread mainThread = Thread.currentThread(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertNotSame(mainThread, asyncThread); context.close(); }
Example 3
Source File: ApplicationContextEventTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void listenersInApplicationContextWithPayloadEvents() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("listener", new RootBeanDefinition(MyPayloadListener.class)); context.refresh(); MyPayloadListener listener = context.getBean("listener", MyPayloadListener.class); context.publishEvent("event1"); context.publishEvent("event2"); context.publishEvent("event3"); context.publishEvent("event4"); assertTrue(listener.seenPayloads.contains("event1")); assertTrue(listener.seenPayloads.contains("event2")); assertTrue(listener.seenPayloads.contains("event3")); assertTrue(listener.seenPayloads.contains("event4")); AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class); assertEquals(2, multicaster.retrieverCache.size()); context.close(); }
Example 4
Source File: ApplicationContextEventTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void listenersInApplicationContextWithPayloadEvents() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("listener", new RootBeanDefinition(MyPayloadListener.class)); context.refresh(); MyPayloadListener listener = context.getBean("listener", MyPayloadListener.class); context.publishEvent("event1"); context.publishEvent("event2"); context.publishEvent("event3"); context.publishEvent("event4"); assertTrue(listener.seenPayloads.contains("event1")); assertTrue(listener.seenPayloads.contains("event2")); assertTrue(listener.seenPayloads.contains("event3")); assertTrue(listener.seenPayloads.contains("event4")); AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class); assertEquals(2, multicaster.retrieverCache.size()); context.close(); }
Example 5
Source File: ApplicationContextEventTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void nonSingletonListenerInApplicationContext() { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition listener = new RootBeanDefinition(MyNonSingletonListener.class); listener.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); context.registerBeanDefinition("listener", listener); context.refresh(); MyEvent event1 = new MyEvent(context); context.publishEvent(event1); MyOtherEvent event2 = new MyOtherEvent(context); context.publishEvent(event2); MyEvent event3 = new MyEvent(context); context.publishEvent(event3); MyOtherEvent event4 = new MyOtherEvent(context); context.publishEvent(event4); assertTrue(MyNonSingletonListener.seenEvents.contains(event1)); assertTrue(MyNonSingletonListener.seenEvents.contains(event2)); assertTrue(MyNonSingletonListener.seenEvents.contains(event3)); assertTrue(MyNonSingletonListener.seenEvents.contains(event4)); MyNonSingletonListener.seenEvents.clear(); context.close(); }
Example 6
Source File: ApplicationContextEventTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void nonSingletonListenerInApplicationContext() { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition listener = new RootBeanDefinition(MyNonSingletonListener.class); listener.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); context.registerBeanDefinition("listener", listener); context.refresh(); MyEvent event1 = new MyEvent(context); context.publishEvent(event1); MyOtherEvent event2 = new MyOtherEvent(context); context.publishEvent(event2); MyEvent event3 = new MyEvent(context); context.publishEvent(event3); MyOtherEvent event4 = new MyOtherEvent(context); context.publishEvent(event4); assertTrue(MyNonSingletonListener.seenEvents.contains(event1)); assertTrue(MyNonSingletonListener.seenEvents.contains(event2)); assertTrue(MyNonSingletonListener.seenEvents.contains(event3)); assertTrue(MyNonSingletonListener.seenEvents.contains(event4)); MyNonSingletonListener.seenEvents.clear(); context.close(); }
Example 7
Source File: ApplicationContextEventTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void listenersInApplicationContextWithPayloadEvents() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("listener", new RootBeanDefinition(MyPayloadListener.class)); context.refresh(); MyPayloadListener listener = context.getBean("listener", MyPayloadListener.class); context.publishEvent("event1"); context.publishEvent("event2"); context.publishEvent("event3"); context.publishEvent("event4"); assertTrue(listener.seenPayloads.contains("event1")); assertTrue(listener.seenPayloads.contains("event2")); assertTrue(listener.seenPayloads.contains("event3")); assertTrue(listener.seenPayloads.contains("event4")); AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class); assertEquals(2, multicaster.retrieverCache.size()); context.close(); }
Example 8
Source File: AsyncAnnotationBeanPostProcessorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void taskExecutorByBeanName() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); context.registerBeanDefinition("postProcessor", processorDefinition); BeanDefinition executorDefinition = new RootBeanDefinition(ThreadPoolTaskExecutor.class); executorDefinition.getPropertyValues().add("threadNamePrefix", "testExecutor"); context.registerBeanDefinition("myExecutor", executorDefinition); BeanDefinition executorDefinition2 = new RootBeanDefinition(ThreadPoolTaskExecutor.class); executorDefinition2.getPropertyValues().add("threadNamePrefix", "testExecutor2"); context.registerBeanDefinition("taskExecutor", executorDefinition2); BeanDefinition targetDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class); context.registerBeanDefinition("target", targetDefinition); context.refresh(); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertTrue(asyncThread.getName().startsWith("testExecutor2")); context.close(); }
Example 9
Source File: QueueMessageHandlerTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test void processHandlerMethodException_invocableHandlerMethodNotAvailable_errorMustBeLogged() { // Arrange StaticApplicationContext applicationContext = new StaticApplicationContext(); applicationContext.registerSingleton("sqsListenerWithoutMessageExceptionHandler", SqsListenerWithoutMessageExceptionHandler.class); applicationContext.registerBeanDefinition("queueMessageHandler", getQueueMessageHandlerBeanDefinition()); applicationContext.refresh(); MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class); LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory(); ListAppender<ILoggingEvent> appender = new ListAppender<>(); appender.start(); Logger log = logContext.getLogger(QueueMessageHandler.class); log.setLevel(Level.ERROR); log.addAppender(appender); appender.setContext(log.getLoggerContext()); // Act assertThatThrownBy( () -> messageHandler .handleMessage(MessageBuilder.withPayload("testContent") .setHeader(QueueMessageHandler.LOGICAL_RESOURCE_ID, "receive") .build())).isInstanceOf(MessagingException.class); // Assert assertThat(appender.list).hasSize(1); }
Example 10
Source File: QuartzSupportTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("resource") public void schedulerAutoStartsOnContextRefreshedEventByDefault() throws Exception { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("scheduler", new RootBeanDefinition(SchedulerFactoryBean.class)); Scheduler bean = context.getBean("scheduler", Scheduler.class); assertFalse(bean.isStarted()); context.refresh(); assertTrue(bean.isStarted()); }
Example 11
Source File: WebApplicationObjectSupportTests.java From spring-analysis-note with MIT License | 5 votes |
@Test @SuppressWarnings("resource") public void testWebApplicationObjectSupportWithWrongContext() { StaticApplicationContext ac = new StaticApplicationContext(); ac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class)); WebApplicationObjectSupport wao = (WebApplicationObjectSupport) ac.getBean("test"); try { wao.getWebApplicationContext(); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
Example 12
Source File: ExpressionEvaluatorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void resolveBeanReference() { StaticApplicationContext applicationContext = new StaticApplicationContext(); BeanDefinition beanDefinition = new RootBeanDefinition(String.class); applicationContext.registerBeanDefinition("myBean", beanDefinition); applicationContext.refresh(); EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.NO_RESULT, applicationContext); Object value = new SpelExpressionParser().parseExpression("@myBean.class.getName()").getValue(context); assertThat(value, is(String.class.getName())); }
Example 13
Source File: AsyncAnnotationBeanPostProcessorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void taskExecutorByBeanName() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); context.registerBeanDefinition("postProcessor", processorDefinition); BeanDefinition executorDefinition = new RootBeanDefinition(ThreadPoolTaskExecutor.class); executorDefinition.getPropertyValues().add("threadNamePrefix", "testExecutor"); context.registerBeanDefinition("myExecutor", executorDefinition); BeanDefinition executorDefinition2 = new RootBeanDefinition(ThreadPoolTaskExecutor.class); executorDefinition2.getPropertyValues().add("threadNamePrefix", "testExecutor2"); context.registerBeanDefinition("taskExecutor", executorDefinition2); BeanDefinition targetDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class); context.registerBeanDefinition("target", targetDefinition); context.refresh(); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertTrue(asyncThread.getName().startsWith("testExecutor2")); context.close(); }
Example 14
Source File: ApplicationContextEventTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void listenerAndBroadcasterWithCircularReference() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class)); RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class); listenerDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("broadcaster")); context.registerBeanDefinition("listener", listenerDef); context.refresh(); BeanThatBroadcasts broadcaster = context.getBean("broadcaster", BeanThatBroadcasts.class); context.publishEvent(new MyEvent(context)); assertEquals("The event was not received by the listener", 2, broadcaster.receivedCount); context.close(); }
Example 15
Source File: QueueMessageHandlerTest.java From spring-cloud-aws with Apache License 2.0 | 5 votes |
@Test void receiveAndReplayMessage_withExceptionThrownInSendTo_shouldCallExceptionHandler() { // Arrange StaticApplicationContext applicationContext = new StaticApplicationContext(); applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class); applicationContext.registerBeanDefinition("queueMessageHandler", getQueueMessageHandlerBeanDefinition()); applicationContext.refresh(); MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class); doThrow(new RuntimeException()).when(this.messageTemplate) .convertAndSend(anyString(), Optional.ofNullable(any())); IncomingMessageHandler messageListener = applicationContext .getBean(IncomingMessageHandler.class); messageListener.setExceptionHandlerCalled(false); // Act try { messageHandler.handleMessage(MessageBuilder.withPayload("testContent") .setHeader(QueueMessageHandler.LOGICAL_RESOURCE_ID, "receiveAndReply") .build()); } catch (MessagingException e) { // ignore } // Assert assertThat(messageListener.isExceptionHandlerCalled()).isTrue(); }
Example 16
Source File: ApplicationContextEventTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void innerBeanAsListener() { StaticApplicationContext context = new StaticApplicationContext(); RootBeanDefinition listenerDef = new RootBeanDefinition(TestBean.class); listenerDef.getPropertyValues().add("friends", new RootBeanDefinition(BeanThatListens.class)); context.registerBeanDefinition("listener", listenerDef); context.refresh(); context.publishEvent(new MyEvent(this)); context.publishEvent(new MyEvent(this)); TestBean listener = context.getBean(TestBean.class); assertEquals(3, ((BeanThatListens) listener.getFriends().iterator().next()).getEventCount()); context.close(); }
Example 17
Source File: ScheduledAnnotationBeanPostProcessorTests.java From spring-analysis-note with MIT License | 5 votes |
private void severalFixedRates(StaticApplicationContext context, BeanDefinition processorDefinition, BeanDefinition targetDefinition) { context.registerBeanDefinition("postProcessor", processorDefinition); context.registerBeanDefinition("target", targetDefinition); context.refresh(); ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class); assertEquals(2, postProcessor.getScheduledTasks().size()); Object target = context.getBean("target"); ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar"); @SuppressWarnings("unchecked") List<IntervalTask> fixedRateTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks"); assertEquals(2, fixedRateTasks.size()); IntervalTask task1 = fixedRateTasks.get(0); ScheduledMethodRunnable runnable1 = (ScheduledMethodRunnable) task1.getRunnable(); Object targetObject = runnable1.getTarget(); Method targetMethod = runnable1.getMethod(); assertEquals(target, targetObject); assertEquals("fixedRate", targetMethod.getName()); assertEquals(0, task1.getInitialDelay()); assertEquals(4000L, task1.getInterval()); IntervalTask task2 = fixedRateTasks.get(1); ScheduledMethodRunnable runnable2 = (ScheduledMethodRunnable) task2.getRunnable(); targetObject = runnable2.getTarget(); targetMethod = runnable2.getMethod(); assertEquals(target, targetObject); assertEquals("fixedRate", targetMethod.getName()); assertEquals(2000L, task2.getInitialDelay()); assertEquals(4000L, task2.getInterval()); }
Example 18
Source File: ApplicationContextEventTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void listenerAndBroadcasterWithCircularReference() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class)); RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class); listenerDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("broadcaster")); context.registerBeanDefinition("listener", listenerDef); context.refresh(); BeanThatBroadcasts broadcaster = context.getBean("broadcaster", BeanThatBroadcasts.class); context.publishEvent(new MyEvent(context)); assertEquals("The event was not received by the listener", 2, broadcaster.receivedCount); context.close(); }
Example 19
Source File: ApplicationContextEventTests.java From java-technology-stack with MIT License | 4 votes |
@Test public void listenersInApplicationContext() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("listener1", new RootBeanDefinition(MyOrderedListener1.class)); RootBeanDefinition listener2 = new RootBeanDefinition(MyOrderedListener2.class); listener2.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("listener1")); listener2.setLazyInit(true); context.registerBeanDefinition("listener2", listener2); context.refresh(); assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class); MyOtherEvent event1 = new MyOtherEvent(context); context.publishEvent(event1); assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyEvent event2 = new MyEvent(context); context.publishEvent(event2); assertTrue(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyEvent event3 = new MyEvent(context); context.publishEvent(event3); MyOtherEvent event4 = new MyOtherEvent(context); context.publishEvent(event4); assertTrue(listener1.seenEvents.contains(event1)); assertTrue(listener1.seenEvents.contains(event2)); assertTrue(listener1.seenEvents.contains(event3)); assertTrue(listener1.seenEvents.contains(event4)); listener1.seenEvents.clear(); context.publishEvent(event1); context.publishEvent(event2); context.publishEvent(event3); context.publishEvent(event4); assertTrue(listener1.seenEvents.contains(event1)); assertTrue(listener1.seenEvents.contains(event2)); assertTrue(listener1.seenEvents.contains(event3)); assertTrue(listener1.seenEvents.contains(event4)); AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class); assertEquals(2, multicaster.retrieverCache.size()); context.close(); }
Example 20
Source File: ApplicationContextEventTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void listenersInApplicationContext() { StaticApplicationContext context = new StaticApplicationContext(); context.registerBeanDefinition("listener1", new RootBeanDefinition(MyOrderedListener1.class)); RootBeanDefinition listener2 = new RootBeanDefinition(MyOrderedListener2.class); listener2.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("listener1")); listener2.setLazyInit(true); context.registerBeanDefinition("listener2", listener2); context.refresh(); assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class); MyOtherEvent event1 = new MyOtherEvent(context); context.publishEvent(event1); assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyEvent event2 = new MyEvent(context); context.publishEvent(event2); assertTrue(context.getDefaultListableBeanFactory().containsSingleton("listener2")); MyEvent event3 = new MyEvent(context); context.publishEvent(event3); MyOtherEvent event4 = new MyOtherEvent(context); context.publishEvent(event4); assertTrue(listener1.seenEvents.contains(event1)); assertTrue(listener1.seenEvents.contains(event2)); assertTrue(listener1.seenEvents.contains(event3)); assertTrue(listener1.seenEvents.contains(event4)); listener1.seenEvents.clear(); context.publishEvent(event1); context.publishEvent(event2); context.publishEvent(event3); context.publishEvent(event4); assertTrue(listener1.seenEvents.contains(event1)); assertTrue(listener1.seenEvents.contains(event2)); assertTrue(listener1.seenEvents.contains(event3)); assertTrue(listener1.seenEvents.contains(event4)); AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class); assertEquals(2, multicaster.retrieverCache.size()); context.close(); }