Spring可以直接读取properties属性文件,读取里面的配置信息,一般这个文件保存数据库连接相关的内容,另外再简单了解下Spring表达式获取bean属性信息。
读取properties文件内容
使用xml配置bean,读取文件内容,并将读取结果保存到Properties对象中去。其中location代表读取文件的位置,classpath代表按照类路径去读取,如果资源放置在resources目录下,直接写文件名就可以。
1 <!-- 读取properties文件内容 --> 2 <util:properties id="prop" location="classpath:myConfig.properties"> 3 </util:properties>
myConfig.properties内容为自己添加的,这里略去。
junit测试类
1 //读取properties文件内容 2 @Test 3 public void test() { 4 String path="myValue.xml"; 5 ApplicationContext ac=new ClassPathXmlApplicationContext(path); 6 Properties prop=ac.getBean("prop",Properties.class); 7 System.out.println(prop); 8 }
读取结果
Spring表达式
Spring表达式即SpringEL,其功能很强大,这里只暂时了解获取bean的信息。以读取上文properties的内容,和读取集合中的内容,注入读取到的值到一个对象中去来理解。
Person类,注入值用。
1 package value; 2 3 public class Person { 4 //属性 从properties获得 5 private String name; 6 private int age; 7 private String interest; 8 private int salary; 9 //属性 从配置的集合bean中获得 10 private String city; 11 private String star; 12 private Double score; 13 private String password; 14 //get set方法 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 public String getInterest() { 28 return interest; 29 } 30 public void setInterest(String interest) { 31 this.interest = interest; 32 } 33 public int getSalary() { 34 return salary; 35 } 36 public void setSalary(int salary) { 37 this.salary = salary; 38 } 39 public String getCity() { 40 return city; 41 } 42 public void setCity(String city) { 43 this.city = city; 44 } 45 public String getStar() { 46 return star; 47 } 48 public void setStar(String star) { 49 this.star = star; 50 } 51 public Double getScore() { 52 return score; 53 } 54 public void setScore(Double score) { 55 this.score = score; 56 } 57 public String getPassword() { 58 return password; 59 } 60 public void setPassword(String password) { 61 this.password = password; 62 } 63 //默认构造方法 64 public Person() { 65 System.out.println("新建了一个Person"); 66 } 67 //重写toString方法 68 @Override 69 public String toString() { 70 return "Person [name=" + name + ", age=" + age + ", interest=" + interest + ", salary=" + salary + ", city=" 71 + city + ", star=" + star + ", score=" + score + ", password=" + password + "]"; 72 } 73 74 }
xml配置文件内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" 4 xmlns:jee="http://www.springframework.org /schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 10 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 12 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 13 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 14 15 <!-- 读取properties文件内容 --> 16 <util:properties id="prop" location="classpath:myConfig.properties"> 17 </util:properties> 18 19 <!-- 将集合数据类型配置成一个bean,可以作为ref来注入 --> 20 <!-- list --> 21 <util:list id="listBean"> 22 <value>长沙</value> 23 <value>佛山</value> 24 <value>北京</value> 25 </util:list> 26 <!-- set --> 27 <util:set id="setBean"> 28 <value>梅西</value> 29 <value>C罗</value> 30 <value>亨利</value> 31 </util:set> 32 <!-- map --> 33 <util:map id="mapBean"> 34 <entry key="材料科学与工程" value="59.5"></entry> 35 <entry key="弹性力学" value="61"></entry> 36 <entry key="工程力学" value="55"></entry> 37 </util:map> 38 <!-- properties --> 39 <util:properties id="propertiesBean"> 40 <prop key="name">clyang</prop> 41 <prop key="password">123</prop> 42 </util:properties> 43 44 <!-- 使用spring表达式注入数据到集合 --> 45 <bean id="person" class="value.Person"> 46 <property name="name" value="#{prop.name}"></property> 47 <property name="age" value="#{prop.age}"></property> 48 <property name="interest" value="#{prop.interest}"></property> 49 <property name="salary" value="#{prop.salary}"></property> 50 <property name="city" value="#{listBean[0]}"></property> 51 <property name="star" value="#{setBean[1]}"></property> 52 <property name="score" value="#{mapBean['材料科学与工程']}"></property> 53 <property name="password" value="#{propertiesBean.password}"></property> 54 </bean> 55 </beans>
在使用Spring表达式通过key获取map中的value时,如果key是英文,可以直接用点来获取,即mapBean.key,如果key是中文会报错,需要使用mapBean['key']来获取。
junit测试类
1 //将Spring表达式读取到的内容注入到对象 2 @Test 3 public void testDI() { 4 String path="myValue.xml"; 5 ApplicationContext ac=new ClassPathXmlApplicationContext(path); 6 Person p=ac.getBean("person",Person.class); 7 System.out.println(p); 8 }
测试结果
可以看出能通过Spring表达式,将bean的属性值注入到另外一个bean中,Spring表达式使用了get方法来获取属性值,如最后一个password,使用了getPassword方法获取了值,然后使用setPassword方法将读取的值注入到person中。