Spring gets the value in the Propertise file

Source: Internet
Author: User
Tags testng

Spring Get propertise the value in the file

The way Spring acquires Propertise, in addition to the annotations injected using @value mentioned in the previous blog post, can also be obtained by means of encoding, which is mainly about using the Embeddedvalueresolveraware interface.

First, prepare Propertise file

Under the Resources folder set up to test the required app.propertise files, which write a few test data, the main data of this article.

Second, prepare the 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" xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><context:component-scan base- Package= "Com.lilin" ></context:component-scan> <!--Spring's property loader, loads properties in property file--<bean id= "Propert Yconfigurer "class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Location" > <value>classpath:app.properties</value> </property> <property name= "fileencoding "Value=" Utf-8 "/> </bean></beans>

The configuration file mainly contains two parts, are used for testing, one is the definition of the path of the annotation scan, so that the following Test class, under the current scan path, you can let spring management be; One is the load configuration of the Propertise file Propertyplaceholderconfigurer, the read path of the configuration file and the encoding method.

Third, prepare the tool class
 PackageCom.lilin.maven.service.dynamicBean;ImportOrg.springframework.context.EmbeddedValueResolverAware;ImportOrg.springframework.stereotype.Service;Importorg.springframework.util.StringValueResolver; @Service Public classPropertiesutilsImplementsEmbeddedvalueresolveraware {PrivateStringvalueresolver Stringvalueresolver; @Override Public voidsetembeddedvalueresolver (stringvalueresolver resolver) {Stringvalueresolver=Resolver; }     Publicstring Getpropertiesvalue (string name) {Name= "${" + Name + "}"; returnStringvalueresolver.resolvestringvalue (name); }}

The tool class implements the Embeddedvalueresolveraware interface, implements the necessary Setembeddedvalueresolver method, assigns the resolver parameter of the method to the private property of the current tool class, At the same time exposes the external extraction function Getpropertiesvalue, obtains the current corresponding value by the name. It is particularly important to note that the name parameter accepted by the Resolvestringvalue function must be converted to "${name}" , otherwise the value will not be taken.

Iv. Preparation of test classes
/** *  */ PackageCom.lilin.maven.service.dynamicBean;ImportJavax.annotation.Resource;Importorg.springframework.test.context.ContextConfiguration;Importorg.testng.annotations.Test;Importcom.lilin.maven.service.BaseTest;/** * @authorLilin **/@ContextConfiguration (Locations= {"Classpath:/config/spring/spring-test.xml" }) Public classDynamicbeantestextendsbasetest {@ResourcePrivatepropertiesutils propertiesutils; @Test Public voidTest () {String beanname= Propertiesutils.getpropertiesvalue ("a");        System.out.println (Beanname); String beanName1= Propertiesutils.getpropertiesvalue ("b");    System.out.println (BEANNAME1); }}

Test class, mainly using TestNG test method, testng use in the previous blog post has been introduced, please see for yourself. In the test class, the tool class is injected into the propertiesutils, and the corresponding parameter values are obtained by passing in the tool class the parameter names that need to be obtained.

Spring gets the value in the Propertise file

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.