Skip to content

Instantly share code, notes, and snippets.

@bobbyjam99-zz
Created March 28, 2011 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbyjam99-zz/890075 to your computer and use it in GitHub Desktop.
Save bobbyjam99-zz/890075 to your computer and use it in GitHub Desktop.
org.springframework.beans.factory.config.PropertyPlaceholderConfigurerをプログラム上で読み込ませる
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("sample.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new ClassPathResource("person.properties"));
cfg.postProcessBeanFactory(factory);
GenericApplicationContext context = new GenericApplicationContext(factory);
Person person = (Person) context.getBean("sample");
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
person.name=John
person.age=21
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sample" class="Person">
<property name="name" value="${person.name}"/>
<property name="age"><value>${person.age}</value></property>
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment