1.类Person
1 package com.xzl.properties;
2
3 public class Person {
4
5 private String name;
6 private int age;
7 private String home;
8 private String country;
9 private String tel;
10 public Person() {
11 super();
12 // TODO Auto-generated constructor stub
13 }
14 /**
15 * @return Returns the age.
16 */
17 public int getAge() {
18 return age;
19 }
20 /**
21 * @param age The age to set.
22 */
23 public void setAge(int age) {
24 this.age = age;
25 }
26 /**
27 * @return Returns the country.
28 */
29 public String getCountry() {
30 return country;
31 }
32 /**
33 * @param country The country to set.
34 */
35 public void setCountry(String country) {
36 this.country = country;
37 }
38 /**
39 * @return Returns the home.
40 */
41 public String getHome() {
42 return home;
43 }
44 /**
45 * @param home The home to set.
46 */
47 public void setHome(String home) {
48 this.home = home;
49 }
50 /**
51 * @return Returns the name.
52 */
53 public String getName() {
54 return name;
55 }
56 /**
57 * @param name The name to set.
58 */
59 public void setName(String name) {
60 this.name = name;
61 }
62 /**
63 * @return Returns the tel.
64 */
65 public String getTel() {
66 return tel;
67 }
68 /**
69 * @param tel The tel to set.
70 */
71 public void setTel(String tel) {
72 this.tel = tel;
73 }
74
75 public void go(){
76 System.out.println("name:"+this.name);
77 System.out.println("age:"+this.age);
78 System.out.println("home:"+this.home);
79 System.out.println("country:"+this.country);
80 System.out.println("tel:"+this.tel);
81 }
82 }
83
2.person.properties
1 person.name = super2
2 person.age = 24
3 person.home = 哈尔滨
4 person.country = 中国
5 person.tel = 12345678
3.applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3
4 <beans>
5 <bean id = "datasource" class = "com.xzl.properties.Person" destroy-method = "close">
6 <property name = "name">
7 <value>
8 ${person.name}
9 </value>
10 </property>
11
12 <property name = "age">
13 <value>
14 ${person.age}
15 </value>
16 </property>
17
18 <property name = "home">
19 <value>
20 ${person.home}
21 </value>
22 </property>
23
24 <property name = "country">
25 <value>
26 ${person.country}
27 </value>
28 </property>
29
30 <property name = "tel">
31 <value>
32 ${person.tel}
33 </value>
34 </property>
35 </bean>
36
37 </beans>
4.测试PTest.java
1 /**
2 *
3 */
4 package com.xzl.properties;
5
6 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
7 import org.springframework.beans.factory.xml.XmlBeanFactory;
8 import org.springframework.core.io.FileSystemResource;
9
10 /**
11 * @author 许忠亮
12 *
13 */
14 public class PTest {
15
16 /**
17 *
18 */
19 public PTest() {
20 super();
21 // TODO Auto-generated constructor stub
22 }
23
24 /**
25 * @param args
26 */
27 public static void main(String[] args) {
28 // TODO Auto-generated method stub
29 XmlBeanFactory factory =
30 new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));
31 PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
32 cfg.setLocation(new FileSystemResource("person.properties"));
33 cfg.postProcessBeanFactory(factory);
34 Person p = (Person)factory.getBean("datasource");
35 p.go();
36 }
37
38 }
39
5.结果:
name:super2
age:24
home:哈尔滨
country:中国
tel:12346578
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程