Spring(20)——@PropertySource

20 @PropertySource

In the previous introduction <context:property-placeholder/>, it was mentioned that it will use PropertySourcesPlaceholderConfigurer by default to perform corresponding property replacement, and the bottom layer uses PropertySource. @PropertySource is used to register a PropertySource. PropertySource is used to represent a resource with a name/value attribute pairing, which can be simply understood as the familiar Properties.

Environment can hold a series of PropertySources, and then when getting properties from them, it will look for the corresponding PropertySources in turn, including system properties and environment variables of course. An Environment contains two PropertySources by default, corresponding to system properties and environment variables. That is, by default, when there are only two ProperySources corresponding to system properties and environment variables, if we get a property from Environment, we will first get it from the system property, and then get it from the environment variable if we don't get it. Therefore, in the following example, we directly obtain the value of the attribute "user.dir" from the Environment, which is the value of the system attribute "user.dir".

	@Test
	public void testPropertySource() {
		ConfigurableApplicationContext context = new GenericApplicationContext();
		ConfigurableEnvironment env = context.getEnvironment();
		String userDir = env.getProperty("user.dir");
		System.out.println(userDir);
		context.close();
	}

We can also add a PropertySource object to the Environment, and the added PropertySource object can be used to obtain the corresponding properties. In the following example, we add a ResourcePropertySource based on the init.properties file in the classpath to the Environment, and it is added before all the PropertySources, because when acquiring properties from the Environment, it will be preferentially obtained from the previous PropertySource. Then if we define a user.dir property in the init.properies file, the user.dir obtained in the following example will be the one we specified in the init.properties file.

	@Test 
	public  void testPropertySource() throws IOException {
		 ConfigurableApplicationContext context =  new  GenericApplicationContext ();
		 // Get the Environment object 
		ConfigurableEnvironment env = context . getEnvironment();
		 // Get the PropertySources of the Environment 
		MutablePropertySources propertySources = env . getPropertySources();
		 // new A Resource-based PropertySource 
		ResourcePropertySource rps =  new  ResourcePropertySource( new  ClassPathResource ( " init.properties " ));
		 // Add a PropertySource object to the current Environment object env 
		propertySources . addFirst(rps);
		 String userDir = env . getProperty( " user. dir " );
		 System . out . println( userDir);
		context.close();
	}

Next, let's introduce @PropertySource. @PropertySource needs to be used with @Configuration. It allows us to easily encapsulate the external resource definition into a PropertySource object and add it to the corresponding Environment. In the following example, we introduce an init.properties file under the classpath through the @PropertySource annotation on the configuration class marked with @Configuration and add it to the current Environment as a PropertySource (when the specified resource does not specify a prefix, it defaults to will be treated as ClassPathResource). After that, when we create a bean, we can use the Environment object to obtain the property value corresponding to the property defined in the PropertySource owned by it. As in the following example, we obtain the value of the property "hello.name" from the Environment when we create hello.

@Configuration
@PropertySource("init.properties")
public class SpringConfig {

	@Autowired
	private Environment env;
	
	@Bean
	public Hello hello() {
		String helloName = env.getProperty("hello.name");
		Hello hello = new Hello(helloName);
		return hello;
	}
	
}

If multiple external files need to be added to the corresponding Environment as PropertySource at the same time, we can specify multiple resources through the value attribute of @PropertySource, which corresponds to an array. In the following example, we add the init.properties and init2.properties files in the classpath as PropertySource to the current Environment at the same time.

@Configuration
@PropertySource({"init.properties", "init2.properties"})
public class SpringConfig {
	
}

We can also define multiple @PropertySources through the @PropertySources annotation to add multiple PropertySources to the Environment.

@Configuration
@PropertySources({@PropertySource("init.properties"), @PropertySource("init2.properties")})
public class SpringConfig {

}

By default, the resource specified by @PropertySource must exist, otherwise Spring will throw an exception. Of course, we can also specify whether to ignore the resource not found through the ignoreResourceNotFound property of @PropertySource. The default is false, which means not to ignore. In the following example, we specify to ignore the absence of the init.properties file through ignoreResourceNotFound.

@Configuration
@PropertySource(value="init.properties", ignoreResourceNotFound=true)
public class SpringConfig {

}

(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=326285242&siteId=291194637