org.springframework.context.BeanThatListens Java Examples

The following examples show how to use org.springframework.context.BeanThatListens. 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: StaticApplicationContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();
	Map<String, String> m = new HashMap<>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
	parent.refresh();
	parent.addApplicationListener(parentListener) ;

	parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");

	this.sac = new StaticApplicationContext(parent);
	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
	sac.refresh();
	sac.addApplicationListener(listener);

	sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

	return sac;
}
 
Example #2
Source File: StaticApplicationContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();
	Map<String, String> m = new HashMap<String, String>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
	parent.refresh();
	parent.addApplicationListener(parentListener) ;

	parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");

	this.sac = new StaticApplicationContext(parent);
	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
	sac.refresh();
	sac.addApplicationListener(listener);

	sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

	return sac;
}
 
Example #3
Source File: StaticApplicationContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();
	Map<String, String> m = new HashMap<>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
	parent.refresh();
	parent.addApplicationListener(parentListener) ;

	parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");

	this.sac = new StaticApplicationContext(parent);
	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
	sac.refresh();
	sac.addApplicationListener(listener);

	sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

	return sac;
}
 
Example #4
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #5
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example #6
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #7
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #8
Source File: StaticApplicationContextMulticasterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();
	Map<String, String> m = new HashMap<String, String>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
	parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			TestApplicationEventMulticaster.class, null);
	parent.refresh();
	parent.addApplicationListener(parentListener) ;

	parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");

	this.sac = new StaticApplicationContext(parent);
	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	Resource resource = new ClassPathResource("testBeans.properties", getClass());
	reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
	sac.refresh();
	sac.addApplicationListener(listener);

	sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

	return sac;
}
 
Example #9
Source File: StaticMessageSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();

	Map<String, String> m = new HashMap<String, String>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));

	parent.refresh();
	parent.addApplicationListener(parentListener);

	this.sac = new StaticApplicationContext(parent);

	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());

	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());

	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());

	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
	sac.refresh();
	sac.addApplicationListener(listener);

	StaticMessageSource messageSource = sac.getStaticMessageSource();
	Map<String, String> usMessages = new HashMap<String, String>(3);
	usMessages.put("message.format.example1", MSG_TXT1_US);
	usMessages.put("message.format.example2", MSG_TXT2_US);
	usMessages.put("message.format.example3", MSG_TXT3_US);
	messageSource.addMessages(usMessages, Locale.US);
	messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK);

	return sac;
}
 
Example #10
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example #11
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #12
Source File: StaticMessageSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();

	Map<String, String> m = new HashMap<>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));

	parent.refresh();
	parent.addApplicationListener(parentListener);

	this.sac = new StaticApplicationContext(parent);

	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());

	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());

	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());

	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
	sac.refresh();
	sac.addApplicationListener(listener);

	StaticMessageSource messageSource = sac.getStaticMessageSource();
	Map<String, String> usMessages = new HashMap<>(3);
	usMessages.put("message.format.example1", MSG_TXT1_US);
	usMessages.put("message.format.example2", MSG_TXT2_US);
	usMessages.put("message.format.example3", MSG_TXT3_US);
	messageSource.addMessages(usMessages, Locale.US);
	messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK);

	return sac;
}
 
Example #13
Source File: StaticApplicationContextMulticasterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();
	Map<String, String> m = new HashMap<>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
	parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			TestApplicationEventMulticaster.class, null);
	parent.refresh();
	parent.addApplicationListener(parentListener) ;

	parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");

	this.sac = new StaticApplicationContext(parent);
	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	Resource resource = new ClassPathResource("testBeans.properties", getClass());
	reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
	sac.refresh();
	sac.addApplicationListener(listener);

	sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

	return sac;
}
 
Example #14
Source File: StaticMessageSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();

	Map<String, String> m = new HashMap<>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", org.springframework.tests.sample.beans.TestBean.class, new MutablePropertyValues(m));

	parent.refresh();
	parent.addApplicationListener(parentListener);

	this.sac = new StaticApplicationContext(parent);

	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());

	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());

	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());

	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	reader.loadBeanDefinitions(new ClassPathResource("testBeans.properties", getClass()));
	sac.refresh();
	sac.addApplicationListener(listener);

	StaticMessageSource messageSource = sac.getStaticMessageSource();
	Map<String, String> usMessages = new HashMap<>(3);
	usMessages.put("message.format.example1", MSG_TXT1_US);
	usMessages.put("message.format.example2", MSG_TXT2_US);
	usMessages.put("message.format.example3", MSG_TXT3_US);
	messageSource.addMessages(usMessages, Locale.US);
	messageSource.addMessage("message.format.example1", Locale.UK, MSG_TXT1_UK);

	return sac;
}
 
Example #15
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example #16
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #18
Source File: StaticApplicationContextMulticasterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected ConfigurableApplicationContext createContext() throws Exception {
	StaticApplicationContext parent = new StaticApplicationContext();
	Map<String, String> m = new HashMap<>();
	m.put("name", "Roderick");
	parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
	m.put("name", "Albert");
	parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
	parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			TestApplicationEventMulticaster.class, null);
	parent.refresh();
	parent.addApplicationListener(parentListener) ;

	parent.getStaticMessageSource().addMessage("code1", Locale.getDefault(), "message1");

	this.sac = new StaticApplicationContext(parent);
	sac.registerSingleton("beanThatListens", BeanThatListens.class, new MutablePropertyValues());
	sac.registerSingleton("aca", ACATester.class, new MutablePropertyValues());
	sac.registerPrototype("aca-prototype", ACATester.class, new MutablePropertyValues());
	PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(sac.getDefaultListableBeanFactory());
	Resource resource = new ClassPathResource("testBeans.properties", getClass());
	reader.loadBeanDefinitions(new EncodedResource(resource, "ISO-8859-1"));
	sac.refresh();
	sac.addApplicationListener(listener);

	sac.getStaticMessageSource().addMessage("code2", Locale.getDefault(), "message2");

	return sac;
}