Created
March 28, 2011 06:27
-
-
Save bobbyjam99-zz/890075 to your computer and use it in GitHub Desktop.
org.springframework.beans.factory.config.PropertyPlaceholderConfigurerをプログラム上で読み込ませる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person.name=John | |
person.age=21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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