Spring provides the tool class for loading the properties file: Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.
When the spring container starts, the property file information is loaded using the built-in bean, which is added as follows in Bean.xml:
<!--Spring's property loader, loads properties in property file-- <bean id= "Propertyconfigurer" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Location " > <value>/WEB-INF/configInfo.properties</value> </property> <property name= "FileEncoding" value= "Utf-8"/> </bean>
One way to use property information after loading is to refer to value directly from the key of the attribute information in other bean definitions, such as the configuration of the Mail sender bean:
<!--Mail send-<bean id= "MailSender" class = "Org.springframe Work.mail.javamail.JavaMailSenderImpl "> <property name=" host "> <VALUE>${EMAIL.HOST}&L t;/value> </property> <property name= "Port" > <value>${email.port}</ value> </property> <property name= "username" > <value>${email.username} </value> </property> <property name= "password" > <value>${email.pass word}</value> </property> <property name= "Javamailproperties" > <props& Gt <prop key= "Mail.smtp.auth" >true </prop> <prop key= "send From ">${email.sendFrom}</prop> </props> </property> </bean>
Another way to use this is to get the configured property information in your code, define a Javabean:ConfigInfo.java, use annotations to inject the attribute information you need to use in your code, and use the following information in your code, as in the properties file:
= d:/test/= d:/test bak/
The corresponding code in Configinfo.java:
@Component ("Configinfo") Public classconfiginfo {@Value ("${file.savepath}") PrivateString Filesavepath; @Value ("${file.backuppath}") PrivateString Filebakpath; PublicString Getfilesavepath () {returnFilesavepath; } PublicString Getfilebakpath () {returnFilebakpath; }} The Business class Bo uses annotations to inject Configinfo objects: @AutowiredPrivateconfiginfo configinfo; you need to add a component scanner to the Bean.xml for automatic injection of annotation methods:<context:component-scan base- Package= "Com.my.model"/>(The Configinfo class is included in the package model above).
Obtain the corresponding property information through the Get method, the advantage is that the code is easy to use, the disadvantage is that if you need new attribute information in the code, the Configinfo.java should be added to the corresponding changes.
Using the properties file in Spring