Spring(13)——PropertyPlaceholderConfigurer(二)之namespace

13.6 Specifying the Load Order

Sometimes we may need or want to define multiple PropertyPlaceholderConfigurers. At this time, we can use the setOrder() method to specify the processing order of the PropertyPlaceholderConfigurer. The smaller the value, the first. In this way, there are two problems to be considered. When a property variable can be replaced by multiple PropertyPlaceholderConfigurers, the one processed first will be replaced first. After the replacement, for the post-processed PropertyPlaceholderConfigurer, the corresponding variable no longer exists and can no longer be performed. replaced. The second problem to consider is that when some property variables can only be replaced by some PropertyPlaceholderConfigurers, if the default setting is used, the PropertyPlaceholderConfigurer processed before it will throw an exception when it encounters property variables that cannot be replaced, so we need to Specifies that the PropertyPlaceholderConfigurer defined earlier ignores cases where property variables cannot be resolved.

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		< property  name = " location "  value = " classpath:t1.properties " /> 
		 <!-- Ignore cases where variables cannot be resolved --> 
		< property  name = " ignoreUnresolvablePlaceholders "  value = " true " />
		 <!-- Specify processing order --> 
		< property  name = " order "  value = " 1 " />
	</bean>
	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		< property  name = " location "  value = " classpath:t2.properties " /> 
		 <!-- Ignore cases where variables cannot be resolved --> 
		< property  name = " ignoreUnresolvablePlaceholders "  value = " true " />
		 <!-- Specify processing order --> 
		< property  name = " order "  value = " 2 " />
	</bean>

13.7 Using namespaces to define

By introducing the context namespace into the Spring configuration file, and then defining the default property-placeholder element of the namespace, a corresponding bean of the PropertySourcesPlaceholderConfigurer type can be generated in the corresponding bean container. Before Spring 3.1, use <context :property-placeholder/> will generate a bean of type PropertyPlaceholderConfigurer. The location of the corresponding external property file can be specified through the location attribute of this element, and multiple files are separated by commas. You can use the properties-ref attribute to specify the corresponding bean that needs to be associated with the internal property definition. The local-override attribute can be used to specify whether the internal attribute definition needs to override the attribute defined in the external attribute file when the same attribute exists in the internal attribute definition and the external attribute file definition. The default is false.

The strategy for system properties can be specified through the system-properties-mode property. For different strategies, Spring will generate different types of beans at the bottom for processing. When the specified value is "ENVIRONMENT", a bean of type PropertySourcesPlaceholderConfigurer will be generated for corresponding variables. The other three types will generate a bean of type PropertyPlaceholderConfigurer to replace the corresponding variable.

  • ENVIRONMENT indicates that the PropertySourcesPlaceholderConfigurer will be used to handle the corresponding variable substitution, which will replace the corresponding variables according to the current Spring Environment and a series of PropertySources, which is the default value. These PropertySources include the external property file specified by the element's location attribute and the corresponding bean-defined property defined by the internal property definition specified by the properties-ref attribute.
  • NEVER means never use the system property to replace the corresponding variable.
  • FALLBACK means that a replacement is attempted using a system property only if no property matches the current variable.
  • OVERRIDE means that the system property will be used first to replace the corresponding variable, that is, the property defined by the external file or the internal property will be used only when the system property cannot replace the corresponding variable.

When neither location nor properties-ref is specified, it means that only the system properties are used to replace variables.

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
	
	<context:property-placeholder
	location="classpath:t1.properties,classpath:t2.properties"
	properties-ref="innerProps" system-properties-mode="ENVIRONMENT" />
	
	<bean id="innerProps" class="java.util.Properties">
		<constructor-arg>
			<props>
				<prop key="p1">v1</prop>
				<prop key="p2">v2</prop>
			</props>
		</constructor-arg>
	</bean>

</beans>

You can also use the ignore-resource-not-found property to specify whether to ignore the problem that the corresponding property file cannot be found. The default is false, which will throw an exception. The ignore-unresolvable attribute specifies whether to ignore the property variable that cannot be resolved or cannot be replaced. The default value is false, that is, it is not ignored. An exception will be thrown when an unresolvable property variable is encountered.

	<context:property-placeholder
		location="classpath:t1.properties,classpath:t2.properties"
		properties-ref="innerProps" system-properties-mode="ENVIRONMENT"
		ignore-resource-not-found="false" ignore-unresolvable="false" />

You can also specify the loading order of the current BeanFactoryPostProcessor through the order property.

Through the above introduction, we know that the functions of PropertySourcesPlaceholderConfigurer and PropertyPlaceholderConfigurer are actually similar, and they have a common parent class, PlaceholderConfigurerSupport. We can also define a bean of type PropertySourcesPlaceholderConfigurer in the Spring configuration file to replace the corresponding variable. Compared with PropertyPlaceholderConfigurer, it is basically the same as PropertyPlaceholderConfigurer except that the systemPropertiesMode property cannot be specified. Also, as their name suggests, PropertyPlaceholderConfigurer internally uses java.util.Properties as the source of property resources, and PropertySourcesPlaceholderConfigurer internally uses org.springframework.core.env.PropertySourcesas the source of property resources. In addition, PropertySourcesPlaceholderConfigurer can also use the current Spring Environment object getProperty() method to obtain the value corresponding to the property variable for replacement. See Spring's API documentation for more information.

(Note: This article is written based on Spring 4.1.0)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394325&siteId=291194637