org.springframework.beans.factory.support.PropertiesBeanDefinitionReader Java Examples

The following examples show how to use org.springframework.beans.factory.support.PropertiesBeanDefinitionReader. 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: PropertiesBeanDefinitionReaderDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // 创建 IoC 底层容器
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // 创建面向 Properties 资源的 BeanDefinitionReader 示例
    PropertiesBeanDefinitionReader beanDefinitionReader = new PropertiesBeanDefinitionReader(beanFactory);
    // Properties 资源加载默认通过 ISO-8859-1,实际存储 UTF-8
    ResourceLoader resourceLoader = new DefaultResourceLoader();
    // 通过指定的 ClassPath 获取 Resource 对象
    Resource resource = resourceLoader.getResource("classpath:/META-INF/user-bean-definitions.properties");
    // 转换成带有字符编码 EncodedResource 对象
    EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
    int beanDefinitionsCount = beanDefinitionReader.loadBeanDefinitions(encodedResource);
    System.out.println(String.format("已记载 %d 个 BeanDefinition\n", beanDefinitionsCount));
    // 通过依赖查找获取 User Bean
    User user = beanFactory.getBean("user", User.class);
    System.out.println(user);
}
 
Example #2
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 #3
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 #4
Source File: JasperReportsHtmlViewTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void configureExporterParametersWithEncodingFromPropertiesFile() throws Exception {
	GenericWebApplicationContext ac = new GenericWebApplicationContext();
	ac.setServletContext(new MockServletContext());
	BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac);
	reader.loadBeanDefinitions(new ClassPathResource("view.properties", getClass()));
	ac.refresh();

	AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report");
	String encoding = (String) view.getConvertedExporterParameters().get(
		net.sf.jasperreports.engine.export.JRHtmlExporterParameter.CHARACTER_ENCODING);
	assertEquals("UTF-8", encoding);

	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
	view.render(getModel(), request, response);
	assertEquals("Response content type is incorrect", "text/html;charset=UTF-8", response.getContentType());
}
 
Example #5
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCircleLeadsToException() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("kerry.(class)", TestBean.class.getName());
	p.setProperty("kerry.(singleton)", "false");
	p.setProperty("kerry.age", "35");
	p.setProperty("kerry.spouse", "*rod");
	p.setProperty("rod.(class)", TestBean.class.getName());
	p.setProperty("rod.(singleton)", "false");
	p.setProperty("rod.age", "34");
	p.setProperty("rod.spouse", "*kerry");

	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	try {
		lbf.getBean("kerry");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
	}
}
 
Example #6
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testUnresolvedReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	try {
		p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
		p.setProperty(PREFIX + "kerry.name", "Kerry");
		p.setProperty(PREFIX + "kerry.age", "35");
		p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

		(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);

		lbf.getBean("kerry");
		fail("Unresolved reference should have been detected");
	}
	catch (BeansException ex) {
		// cool
	}
}
 
Example #7
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSimpleReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "rod.name", "Rod");

	p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "kerry.name", "Kerry");
	p.setProperty(PREFIX + "kerry.age", "35");
	p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
	assertTrue("2 beans registered, not " + count, count == 2);

	TestBean kerry = lbf.getBean("kerry", TestBean.class);
	assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
	ITestBean spouse = kerry.getSpouse();
	assertTrue("Kerry spouse is non null", spouse != null);
	assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
 
Example #8
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testLazyInitialization() {
	KnowsIfInstantiated.clearInstantiationRecord();
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
	p.setProperty("x1.(lazy-init)", "true");
	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	lbf.preInstantiateSingletons();

	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	lbf.getBean("x1");
	assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
 
Example #9
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactoryBeanDidNotCreatePrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	assertEquals(TestBean.class, lbf.getType("x1"));
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	lbf.getBean("x1");
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
 
Example #10
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCircleLeadsToException() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("kerry.(class)", TestBean.class.getName());
	p.setProperty("kerry.(singleton)", "false");
	p.setProperty("kerry.age", "35");
	p.setProperty("kerry.spouse", "*rod");
	p.setProperty("rod.(class)", TestBean.class.getName());
	p.setProperty("rod.(singleton)", "false");
	p.setProperty("rod.age", "34");
	p.setProperty("rod.spouse", "*kerry");

	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	try {
		lbf.getBean("kerry");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
	}
}
 
Example #11
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testUnresolvedReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	try {
		p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
		p.setProperty(PREFIX + "kerry.name", "Kerry");
		p.setProperty(PREFIX + "kerry.age", "35");
		p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

		(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);

		lbf.getBean("kerry");
		fail("Unresolved reference should have been detected");
	}
	catch (BeansException ex) {
		// cool
	}
}
 
Example #12
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLazyInitialization() {
	KnowsIfInstantiated.clearInstantiationRecord();
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
	p.setProperty("x1.(lazy-init)", "true");
	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	lbf.preInstantiateSingletons();

	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	lbf.getBean("x1");
	assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
 
Example #13
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testFactoryBeanDidNotCreatePrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	assertEquals(TestBean.class, lbf.getType("x1"));
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	lbf.getBean("x1");
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
 
Example #14
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSimpleReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "rod.name", "Rod");

	p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "kerry.name", "Kerry");
	p.setProperty(PREFIX + "kerry.age", "35");
	p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
	assertTrue("2 beans registered, not " + count, count == 2);

	TestBean kerry = lbf.getBean("kerry", TestBean.class);
	assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
	ITestBean spouse = kerry.getSpouse();
	assertTrue("Kerry spouse is non null", spouse != null);
	assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
 
Example #15
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testFactoryBeanDidNotCreatePrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	assertEquals(TestBean.class, lbf.getType("x1"));
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	lbf.getBean("x1");
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
 
Example #16
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "rod.name", "Rod");

	p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "kerry.name", "Kerry");
	p.setProperty(PREFIX + "kerry.age", "35");
	p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
	assertTrue("2 beans registered, not " + count, count == 2);

	TestBean kerry = lbf.getBean("kerry", TestBean.class);
	assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
	ITestBean spouse = kerry.getSpouse();
	assertTrue("Kerry spouse is non null", spouse != null);
	assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
 
Example #17
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnresolvedReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	try {
		p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
		p.setProperty(PREFIX + "kerry.name", "Kerry");
		p.setProperty(PREFIX + "kerry.age", "35");
		p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

		(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);

		lbf.getBean("kerry");
		fail("Unresolved reference should have been detected");
	}
	catch (BeansException ex) {
		// cool
	}
}
 
Example #18
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrototypeCircleLeadsToException() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("kerry.(class)", TestBean.class.getName());
	p.setProperty("kerry.(singleton)", "false");
	p.setProperty("kerry.age", "35");
	p.setProperty("kerry.spouse", "*rod");
	p.setProperty("rod.(class)", TestBean.class.getName());
	p.setProperty("rod.(singleton)", "false");
	p.setProperty("rod.age", "34");
	p.setProperty("rod.spouse", "*kerry");

	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	try {
		lbf.getBean("kerry");
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
	}
}
 
Example #19
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 #20
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertiesPopulationWithPrefix() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty(PREFIX + "test.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "test.name", "Tony");
	p.setProperty(PREFIX + "test.age", "0x30");
	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
	assertTrue("1 beans registered, not " + count, count == 1);
	testSingleTestBean(lbf);
}
 
Example #21
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testHarmlessIgnorableRubbish() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("foo", "bar");
	p.setProperty("qwert", "er");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, "test");
	assertTrue("No beans defined after harmless ignorable rubbish", lbf.getBeanDefinitionCount() == 0);
}
 
Example #22
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCanEscapeBeanReferenceSyntax() {
	String name = "*name";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("r.(class)", TestBean.class.getName());
	p.setProperty("r.name", "*" + name);
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	TestBean r = (TestBean) lbf.getBean("r");
	assertTrue(r.getName().equals(name));
}
 
Example #23
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanEscapeBeanReferenceSyntax() {
	String name = "*name";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("r.(class)", TestBean.class.getName());
	p.setProperty("r.name", "*" + name);
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	TestBean r = (TestBean) lbf.getBean("r");
	assertTrue(r.getName().equals(name));
}
 
Example #24
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testHarmlessIgnorableRubbish() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("foo", "bar");
	p.setProperty("qwert", "er");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, "test");
	assertTrue("No beans defined after harmless ignorable rubbish", lbf.getBeanDefinitionCount() == 0);
}
 
Example #25
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testBeanReferenceWithNewSyntax() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("r.(class)", TestBean.class.getName());
	p.setProperty("r.name", "rod");
	p.setProperty("k.(class)", TestBean.class.getName());
	p.setProperty("k.name", "kerry");
	p.setProperty("k.spouse", "*r");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	TestBean k = (TestBean) lbf.getBean("k");
	TestBean r = (TestBean) lbf.getBean("r");
	assertTrue(k.getSpouse() == r);
}
 
Example #26
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNameAlreadyBound() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("kerry.(class)", TestBean.class.getName());
	p.setProperty("kerry.age", "35");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	try {
		(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	}
	catch (BeanDefinitionStoreException ex) {
		assertEquals("kerry", ex.getBeanName());
		// expected
	}
}
 
Example #27
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPropertiesWithDotsInKey() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	p.setProperty("tb.(class)", TestBean.class.getName());
	p.setProperty("tb.someMap[my.key]", "my.value");

	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("1 beans registered, not " + count, count == 1);
	assertEquals(1, lbf.getBeanDefinitionCount());

	TestBean tb = lbf.getBean("tb", TestBean.class);
	assertEquals("my.value", tb.getSomeMap().get("my.key"));
}
 
Example #28
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPropertiesPopulationWithPrefix() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty(PREFIX + "test.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "test.name", "Tony");
	p.setProperty(PREFIX + "test.age", "0x30");
	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
	assertTrue("1 beans registered, not " + count, count == 1);
	testSingleTestBean(lbf);
}
 
Example #29
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPropertiesPopulationWithNullPrefix() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("test.(class)", TestBean.class.getName());
	p.setProperty("test.name", "Tony");
	p.setProperty("test.age", "48");
	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("1 beans registered, not " + count, count == 1);
	testSingleTestBean(lbf);
}
 
Example #30
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;
}