In large-scale projects, we tend to manage the configuration information of our system in a general way by configuring configuration information with a cfg.properties file, and then automatically reading the key in the Cfg.properties configuration file when our system is initialized Value (key-value pairs), and then customize the initialization of our system.
?? So in general, we use Java.util.Properties, which is Java's own. Often a problem is that every time we load, we need to manually read this configuration file, one coding trouble, and the code is not elegant, often we will also create a class to read, and store these configuration information.
?? But, in my opinion, these things that spring has, you have to repeat the wheel, the cost of Kung Fu. And not necessarily less convenient to maintain and manage upgrades. Use of the common comparison is poor, each enterprise has a variety of enterprise development norms, so that a child, learning costs are also improved. (This is nonsense).
?? Spring provides a propertyplaceholderconfigurer
?? This class is a subclass of Beanfactorypostprocessor. (Do not understand their own Niang, can understand not understand, below I will say about)
Its main principle is in the. When the spring container is initialized, it reads either XML or annotation to initialize the bean. At initialization time, this propertyplaceholderconfigurer will intercept the initialization of the bean, and the configured ${pname} will be replaced when initialized, based on the configuration in our properties. This enables the substitution of expressions.
After understanding the principles, let's look at some of the ways it's done.
Create a new cfg.properties under our classpath.
#cfg.properties配置文件的内容username=jay password=123
Here is the spring configuration file code
<Beansxmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" ><Beanclass="Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <!--the scenario for reading a configuration file-- > < name="Location" value="classpath:cfg.properties" ></ Property> <!--the processing scheme for reading more than two profiles-- <!--<property name= "Locations" > <list> < Value>classpath:cfg.properties</value> <value>classpath:cfg2.properties</value> </list > </property> </bean></beans>
//test code @RunWith (springjunit4classrunner.class) @ContextConfiguration ( "Classpath:applicationCtx.xml") public class springctxtst { @Value ( "${username}") private String uname; @Value ( "${password}") Private String pwd; @Test public void test () {System.out.println ( "password:" +pwd);}
Console output
Username:jay
Password:123
Of course, spring also provides us with another solution, and we can write the following code directly in the spring configuration file.
<Beansxmlns="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.xsd Http://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-3.2.xsd "> <!--simplifies the configuration file in this way-- < Context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/> </beans>
Important note
?? We know that, whether using propertyplaceholderconfigurer or context:property-placeholder this way, you need to remember that The spring framework not only reads the key-value pairs in our configuration file, but also reads the system initialization information from the JVM. Sometimes, we need to set the configuration key to a set of naming rules, such as
项目名称.组名.功能名=配置值org.team.tfunction=0001
This way to minimize conflicts with system configuration information!
?? At the same time, we can also use the following configuration method to configure, here I am with never meaning is not to read the system configuration information. If
<context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties" system-properties-mode="NEVER"/>
Turning: Talking about Spring's propertyplaceholderconfigurer