Using the properties file in Spring

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.