Meaning of use of ${} in XML (dollar sign curly braces, for example, Spring, Ibatis, MyBatis)

Source: Internet
Author: User

In the project, you will often see this notation in XML:

<PropertiesResource= "Properties/database.properties"/><DataSourcetype= "simple">      < Propertyvalue= "${driver}"name= "JDBC. Driver "/>      < Propertyvalue= "${url}"name= "JDBC. Connectionurl "/>      < Propertyvalue= "${username}"name= "JDBC. Username "/>      < Propertyvalue= "${password}"name= "JDBC. Password "/>    </DataSource>

See ${} Such expressions, the first impression in my mind is not the El expression in our JSP?!

Ha ha...

However, this is really not an El expression, and spring provides its own El expression, but its format is #{}

So ....

${key} the expression in XML, which represents a variable value corresponding to key in the introduced properties file, is completely parsed by the third-party jar itself.

============== Split Line ===========

Example:

A.ibatis in the configuration file, ${} of the parse, directly on the code:

 Public Staticstring Parsepropertytokens (String string, Properties variables) {FinalString OPEN = "${"; FinalString CLOSE = "}"; String newstring=string; if(NewString! =NULL&& Variables! =NULL) {      intStart =Newstring.indexof (OPEN); intEnd =Newstring.indexof (CLOSE);  while(Start >-1 && end >start) {String prepend= newstring.substring (0, start); String Append= newstring.substring (end +close.length ()); String propname= newstring.substring (start +open.length (), end); String PropValue=Variables.getproperty (propname); if(PropValue = =NULL) {newstring= prepend + propname +append; } Else{newstring= prepend + PropValue +append; } Start=Newstring.indexof (OPEN); End=Newstring.indexof (CLOSE); }    }    returnnewstring; }

Found the wood has, is completely analytic character!!!! Code reference: Com.ibatis.common.xml.NodeletUtils.java

b.spring configuration file, ${} parsing, directly on the code

/*** Resolve placeholder values in the given array of patterns. * @returna new array with updated patterns*/      protectedstring[] Resolveembeddedvaluesinpatterns (string[] patterns) {if( This. Embeddedvalueresolver = =NULL) {              returnpatterns; }          Else{string[] resolvedpatterns=NewString[patterns.length];  for(inti=0; i < patterns.length; i++) {Resolvedpatterns[i]= This. Embeddedvalueresolver.resolvestringvalue (Patterns[i]); }              returnResolvedpatterns; }      }  

The spring code is not very obscure, interested in child shoes research propertyplaceholderconfigurer (and if it does not find the XXX key defined in ${xxx}, it will also go to the JVM System Properties (System.getproperty () ) and Environment variables (system.getenv ()).

Summarize:

XML file, if ${} is used, the work of parsing depends on the third-party jar;

Meaning of use of ${} in XML (dollar sign curly braces, for example, Spring, Ibatis, MyBatis)

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.