Spring_ Property Placeholder

Source: Internet
Author: User
Tags aop

Patience and perseverance are painful things, but they can gradually bring you benefits. --Ovid

Property placeholder

Spring has always supported the definition of a property to a file of an external property and inserted it into a spring bean using a placeholder value.

The placeholder is in the form of a property name that is wrapped with "${}", in order to use the property placeholder, we must configure a propertyplaceholderconfigurer or Propertysourcesplaceholderconfigurer instance, Starting with Spring 3.0, Propertysourcesplaceholderconfigurer is recommended because it can parse placeholders based on spring environment and its property sources.

1. Using attribute placeholder injection properties in a Java-based configuration

Package Chapter3.prctice6;import Org.springframework.beans.factory.annotation.value;import Org.springframework.context.annotation.bean;import Org.springframework.context.support.propertysourcesplaceholderconfigurer;import org.springframework.stereotype.Component; @Component Public classApplemobile implements Mobile {PrivateString color; PrivateString type;  PublicApplemobile (@Value ("${mobile.color}") String color, @Value ("${mobile.type}") (String type) { This. color =color;  This. Type =type; } @Bean Publicpropertysourcesplaceholderconfigurer Placeholderconfigurer () {return NewPropertysourcesplaceholderconfigurer (); }         Public voidPlay () {System. out. println (color+"-"+type); }}

  2. Using placeholder injection properties in an XML-based configuration

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"Xmlns:util= "Http://www.springframework.org/schema/util"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:jdbc= "Http://www.springframework.org/schema/jdbc"Xmlns:jee= "Http://www.springframework.org/schema/jee"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/jdbc
Http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd Http://www.springframework.org/schema/jee/HTTP Www.springframework.org/schema/jee/spring-jee-4.3.xsd Http://www.springframework.org/schema/beans/HTTP Www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-4.3.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/util/HTTP Www.springframework.org/schema/util/spring-util-4.3.xsd "> <BeanID= "Applemobile"class= "Chapter3.prctice6.AppleMobile"C:color= "${moble.color}"C:type= "${mobile.type}"> </Bean> <Context:property-placeholder/></Beans>

  Parsing an external property can defer processing of a value to run time, and its focus is on resolving properties from spring environment and property sources by name.

Spring_ Property Placeholder

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.