當前位置: 首頁>>代碼示例>>Java>>正文


Java StaticApplicationContext.getBean方法代碼示例

本文整理匯總了Java中org.springframework.context.support.StaticApplicationContext.getBean方法的典型用法代碼示例。如果您正苦於以下問題:Java StaticApplicationContext.getBean方法的具體用法?Java StaticApplicationContext.getBean怎麽用?Java StaticApplicationContext.getBean使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.context.support.StaticApplicationContext的用法示例。


在下文中一共展示了StaticApplicationContext.getBean方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: taskExecutorByBeanType

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:AsyncAnnotationBeanPostProcessorTests.java

示例2: testAutoProxyCreatorWithFactoryBeanAndPrototype

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:AutoProxyCreatorTests.java

示例3: listenersInApplicationContextWithPayloadEvents

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:22,代碼來源:ApplicationContextEventTests.java

示例4: schedulerAutoStartsOnContextRefreshedEventByDefault

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@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());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:11,代碼來源:QuartzSupportTests.java

示例5: testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);

	TestBean tb2 = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertSame(tb, tb2);
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	tb2.getAge();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:31,代碼來源:AutoProxyCreatorTests.java

示例6: listenerAndBroadcasterWithCircularReference

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:16,代碼來源:ApplicationContextEventTests.java

示例7: innerBeanAsListener

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:16,代碼來源:ApplicationContextEventTests.java

示例8: schedulerAutoStartupFalse

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
@SuppressWarnings("resource")
public void schedulerAutoStartupFalse() throws Exception {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(
			SchedulerFactoryBean.class).addPropertyValue("autoStartup", false).getBeanDefinition();
	context.registerBeanDefinition("scheduler", beanDefinition);
	Scheduler bean = context.getBean("scheduler", Scheduler.class);
	assertFalse(bean.isStarted());
	context.refresh();
	assertFalse(bean.isStarted());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:13,代碼來源:QuartzSupportTests.java

示例9: testAutoProxyCreatorWithFallbackToTargetClass

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:35,代碼來源:AutoProxyCreatorTests.java

示例10: testExpectedBehavior

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testExpectedBehavior() throws Exception {
	TestBean target = new TestBean();
	final TestListener listener = new TestListener();

	class TestContext extends StaticApplicationContext {
		@Override
		protected void onRefresh() throws BeansException {
			addApplicationListener(listener);
		}
	}

	StaticApplicationContext ctx = new TestContext();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("applicationEventClass", TestEvent.class.getName());
	// should automatically receive applicationEventPublisher reference
	ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
	ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
	ctx.refresh();

	EventPublicationInterceptor interceptor =
			(EventPublicationInterceptor) ctx.getBean("publisher");
	ProxyFactory factory = new ProxyFactory(target);
	factory.addAdvice(0, interceptor);

	ITestBean testBean = (ITestBean) factory.getProxy();

	// invoke any method on the advised proxy to see if the interceptor has been invoked
	testBean.getAge();

	// two events: ContextRefreshedEvent and TestEvent
	assertTrue("Interceptor must have published 2 events", listener.getEventCount() == 2);
	TestListener otherListener = (TestListener) ctx.getBean("&otherListener");
	assertTrue("Interceptor must have published 2 events", otherListener.getEventCount() == 2);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:36,代碼來源:EventPublicationInterceptorTests.java

示例11: testQualifiedByValue

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testQualifiedByValue() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByValueTestBean.class);
	context.refresh();
	QualifiedByValueTestBean testBean = (QualifiedByValueTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("Larry", person.getName());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:12,代碼來源:QualifierAnnotationTests.java

示例12: testCustomAutoProxyCreator

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testCustomAutoProxyCreator() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
	assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:35,代碼來源:AutoProxyCreatorTests.java

示例13: testQualifiedByBeanName

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testQualifiedByBeanName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByBeanNameTestBean.class);
	context.refresh();
	QualifiedByBeanNameTestBean testBean = (QualifiedByBeanNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
	assertTrue(testBean.myProps != null && testBean.myProps.isEmpty());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:13,代碼來源:QualifierAnnotationTests.java

示例14: testQualifiedByFieldName

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testQualifiedByFieldName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByFieldNameTestBean.class);
	context.refresh();
	QualifiedByFieldNameTestBean testBean = (QualifiedByFieldNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:12,代碼來源:QualifierAnnotationTests.java

示例15: testQualifiedByParameterName

import org.springframework.context.support.StaticApplicationContext; //導入方法依賴的package包/類
@Test
public void testQualifiedByParameterName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByParameterNameTestBean.class);
	context.refresh();
	QualifiedByParameterNameTestBean testBean = (QualifiedByParameterNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:12,代碼來源:QualifierAnnotationTests.java


注:本文中的org.springframework.context.support.StaticApplicationContext.getBean方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。