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