Description
Typically, the project will have the need to read the configuration file, which can be used for property files, XML files, and so on. Here, Spring uses the object feature to be read, and to write a sample of the property.
Demo two properties indicate items that are frequently used first, the map object of key, value relationship (similar property file), List object
Java objects
Package Eway.flight.utils;import Java.util.list;import Java.util.map;import org.springframework.stereotype.repository;/** * * @author YZP */@Repositorypublic class Systemconfig {private Map ValueMap; Private List UserIDs; Public List Getuserids () {return userids;} public void Setuserids (List userids) {this.userids = UserIDs;} Public Map Getvaluemap () {return valueMap; } public void Setvaluemap (Map valueMap) {this.valuemap = ValueMap; Public Object GetValue (String key) {if (This.getvaluemap (). ContainsKey (key)) {return This.getva Luemap (). get (key); }else{return ""; }} private List searchconfiglist; Public List getsearchconfiglist () {return searchconfiglist; } public void Setsearchconfiglist (List searchconfiglist) {this.searchconfiglist = searchconfiglist; } private Map Sendrangeconfig; Public Map Getsendrangeconfig () {return sendrangeconfig; } public void Setsendrangeconfig (Map sendrangeconfig) {this.sendrangeconfig = Sendrangeconfig; } private List syncuserconfiglist; Public List getsyncuserconfiglist () {return syncuserconfiglist; } public void Setsyncuserconfiglist (List syncuserconfiglist) {this.syncuserconfiglist = syncuserconfiglist; } }
configuration file
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id=" systemconfig "class=" Eway.flight.utils.SystemConfig "> <property name=" ValueMap "> <map> <entr Y key= "mail_message_template" > <value> you have a new email! Sender: {from}; Send: {time}; Title: {title}. </value> </entry> <entry key= "schedule_module_id" > < value>68</value> </entry> <entry key= "Schedule_prompt_message_template" ; <value> your personal schedule reminder: {title} will start at {time}!. </value> </entry> <entry key= "Pm25_import_file_path" > <value>c:\pm25.txt</value> </entry> </map> </property> <property name= "UserIDs" ><bean class= "Org.springframework.beans.factory.config.ListFactoryBean" >< Property Name= "Targetlistclass" ><value>java.util.arraylist</value></property><property Name= "SourceList" ><list><value>usr1</value><value>usr2</value><value> Usr3</value></list></property></bean></property> </bean></beans>
Spring Read configuration file