Java Code Examples for org.springframework.beans.factory.support.PropertiesBeanDefinitionReader#loadBeanDefinitions()

The following examples show how to use org.springframework.beans.factory.support.PropertiesBeanDefinitionReader#loadBeanDefinitions() . 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: 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 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: 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 5
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 6
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;
}
 
Example 7
Source File: BeanMetadataConfigurationDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // 实例化基于 Properties 资源 BeanDefinitionReader
    PropertiesBeanDefinitionReader beanDefinitionReader = new PropertiesBeanDefinitionReader(beanFactory);
    String location = "META-INF/user.properties";
    // 基于 ClassPath 加载 properties 资源
    Resource resource = new ClassPathResource(location);
    // 指定字符编码 UTF-8
    EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
    int beanNumbers = beanDefinitionReader.loadBeanDefinitions(encodedResource);
    System.out.println("已加载 BeanDefinition 数量:" + beanNumbers);
    // 通过 Bean Id 和类型进行依赖查找
    User user = beanFactory.getBean("user", User.class);
    System.out.println(user);
}
 
Example 8
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 9
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 10
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 11
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 12
Source File: SpringInitializer.java    From Mycat-openEP with Apache License 2.0 5 votes vote down vote up
private static void initPropertiesConf(GenericApplicationContext context){
	String classPath=ProcessInfo.getPropertiesClassPath();
	if(Help.isNotEmpty(classPath)){
		PropertiesBeanDefinitionReader reader=new PropertiesBeanDefinitionReader(context);
		reader.loadBeanDefinitions(classPath);
	}
}
 
Example 13
Source File: PropertyFileApplicationContext.java    From Spring with Apache License 2.0 4 votes vote down vote up
public PropertyFileApplicationContext(String fileName) {
	final PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(this);
	final int i = reader.loadBeanDefinitions(fileName);
	System.out.println("found " + i + " beans");
	refresh();
}