Reading the values in the properties file using the Spring annotation method

Source: Internet
Author: User
To reference the properties or to configure in XML, there are two configuration methods for referencing a single properties file, and multiple properties files
One, read a single properties file
In the spring configuration file, add the
Introduce the until namespace:
Xmlns:util= "Http://www.springframework.org/schema/util"
Xsi:schemalocation= "Http://www.springframework.org/schema/util
Http://www.springframework.org/schema/util/spring-util-3.0.xsd "
Read the Properties file
<util:properties id= "Propertiesreader" location= "Classpath:test.properties"/>
Second, read multiple properties files
<bean id= "Propertiesreader"
class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" >
<property name= "Locations" >
<list>
<value>classpath:param.properties</value>
<value>classpath:base.properties</value>
</list>
</property>
</bean>

Both methods are actually the same.


Read the Properties file property in the class

The properties that need to be injected in the class implement setter and Getter methods.

Add @Value annotations before the setter method
@Value ("#{propertiesreader[propertiesname]}")
Propertiesname is the key in the properties file. In this way, Spring will automatically inject values during the container startup process.

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.