Large-scale projects, we often have configuration information in our system of unified management, the general practice is to set the configuration information and a cfg.properties file, and then when we initialize the system, the system automatically reads the configuration cfg.properties file key value (key-value pairs), and then we customize the system initialization.
So under normal circumstances, java.util.Properties we use, that is, java own. Often there is a problem is that every time a loaded, we all need to read the manual configuration file to a coding trouble, and secondly, the code is not elegant, often we will create a special class of their own to read and store the configuration information.
Spring is provided with a PropertyPlaceholderConfigurer
This class is a subclass of BeanFactoryPostProcessor. (Do not know the degree of his own mother, can not understand can understand, I can speak about the following)
The main principle yes. Spring container initialization time, reads the xml or annotation of Bean initialized. Initialization time, this PropertyPlaceholderConfigurer intercepts Bean is
initialized, which would be configured when the $ {pname} to be replaced, replaced according to our Properties in the configuration. Replacement operation in order to achieve expression.
After complete understanding of the principles, we look at ways of its implementation.
Create a cfg.properties in our classpath
content # cfg.properties configuration file username = jay password = 123
Here is the Spring configuration file code:
<beans xmlns="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"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 对于读取一个配置文件采取的方案 --> <property name="location" value="classpath:cfg.properties"></property> <!- <! -->processing program for reading two or more profiles taken <property name="locations"> <list> <value>classpath:cfg.properties</value> <value>classpath:cfg2.properties</value> </list> </property> --> </bean> </beans>
//测试代码 @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("username:"+uname); System.out.println("password:"+pwd); } }
Console output
username:jay
password:123
Of course, for this purpose Spring also provides us with another solution, we write the following code directly in the Spring configuration file can be achieved
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!--采用这种方式简化配置文件--> <context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/> </beans>
Important note
we know, whether used PropertyPlaceholderConfigurer or by context: property-placeholder be achieved in this way, we need to remember, Spring framework will not only read our profiles of key-value pairs, but also read Jvm initialization information about the system. Sometimes, we need to configure Key given set of naming rules, such as
Project Name The group name Function name = configuration value
org.team.tfunction = 0001
This way to minimize conflict with the system configuration information!
At the same time, we can also use this configuration below to configure, and here I mean not with NEVER read the system configuration information. in case
<context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties" system-properties-mode="NEVER"/>