org.springframework.beans.factory.support.PropertiesBeanDefinitionReader源码实例Demo

类org.springframework.beans.factory.support.PropertiesBeanDefinitionReader源码实例Demo

下面列出了org.springframework.beans.factory.support.PropertiesBeanDefinitionReader 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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;
}
 
@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());
}
 
@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()));
}
 
@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
	}
}
 
@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));
	}
}
 
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);
}
 
@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;
}
 
@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());
}
 
@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());
}
 
@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()));
}
 
@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
	}
}
 
@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));
	}
}
 
@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;
}
 
@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());
}
 
@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());
}
 
@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());
}
 
@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()));
}
 
@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
	}
}
 
@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));
	}
}
 
@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;
}
 
@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;
}
 
@Test
public void testUnreferencedSingletonWasInstantiated() {
	KnowsIfInstantiated.clearInstantiationRecord();
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	lbf.preInstantiateSingletons();
	assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
 
源代码23 项目: Mycat-openEP   文件: SpringInitializer.java
private static void initPropertiesConf(GenericApplicationContext context){
	String classPath=ProcessInfo.getPropertiesClassPath();
	if(Help.isNotEmpty(classPath)){
		PropertiesBeanDefinitionReader reader=new PropertiesBeanDefinitionReader(context);
		reader.loadBeanDefinitions(classPath);
	}
}
 
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
@Test
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	p.setProperty("x1.singleton", "true");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
	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");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertTrue(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
@Test
public void testEmptyPropertiesPopulation() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("No beans defined after ignorable invalid", lbf.getBeanDefinitionCount() == 0);
}
 
@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);
}
 
@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);
}
 
@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);
}
 
源代码评论
动弹
沙发等你来抢
 类方法
 同包方法