SoFunction
Updated on 2025-03-09

Analysis of usage scenarios for @PropertySource annotation in Spring

1. Brief introduction

In daily development, have you ever encountered such a scenario: a project needs to write many configuration files and configure some system information.

At this time, it is often necessary to write special tool classes or methods to read and parse these configuration files, and load the configuration items in the configuration file into the system memory.

When using these configuration items in the future, you can directly obtain the configuration items loaded into memory through the tool class or method.

@PropertySource annotation is an annotation provided in Spring that can load configuration files, and the contents in the configuration files can be stored in Spring's environment variables.

2. Notes

@PropertySource annotation is an annotation provided in Spring that loads the configuration file by specifying the configuration file location, and can store the contents of the configuration file into Spring's environment variables.

In addition to reading configuration items through Spring's environment variables, you can also get the value of configuration items through the @Value annotation.

In addition, Spring also provides a @PropertySourcesAnnotation, in @PropertySourcesIn the annotation, multiple @PropertySource annotations can be introduced.

1. Annotate the source code

@PropertySource and @PropertySource are provided in SpringsTwo annotations to load the configuration file.

① @PropertySource annotation

The @PropertySource annotation can only be annotated to the class, and can load the configuration file by specifying the location of the configuration file. In addition to loading the properties configuration file, the @PropertySource annotation can also load the xml configuration file and yml configuration file. If you load the yml configuration file, you can customize the PropertySourceFactory to implement the parsing operation of the yml configuration file.

For details of the source code of @PropertySource annotation, please refer to:

/**
 * @author Chris Beams
 * @author Juergen Hoeller
 * @author Phillip Webb
 * @author Sam Brannen
 * @since 3.1
 */
@Target()
@Retention()
@Documented
@Repeatable()
public @interface PropertySource {

 String name() default "";
 String[] value();
 /**
  * @since 4.0
  */
 boolean ignoreResourceNotFound() default false;
 /**
  * @since 4.3
  */
 String encoding() default "";
 /**
  * @since 4.3
  */
 Class<? extends PropertySourceFactory> factory() default ;
}

From the source code, we can see that the @PropertySource annotation is annotation provided since Spring 3.1 version. The meaning of each attribute in the annotation is as follows:

  • name: represents the name of the loaded resource. If it is empty, a name will be automatically generated based on the loaded configuration file.
  • value: represents the path of the loaded resource. This path can be a classpath or a file path.
  • ignoreResourceNotFound: Indicates whether to ignore errors that are not found in the file when the configuration file is not found. The default value is false, which means that when the configuration file is not found, Spring will report an error when it starts.
  • encoding: represents the character set encoding used to parse the configuration file.
  • factory: represents the factory class that reads the corresponding configuration file. The default factory class is PropertySourceFactory.

② @PropertySources annotation

In addition to the @PropertySource annotation, Spring also provides a @PropertySourcesannotation.

/**
 * @author Phillip Webb
 * @since 4.0
 */
@Target()
@Retention()
@Documented
public @interface PropertySources {
 PropertySource[] value();
}

From the source code, it can be seen that @PropertySources is an annotation provided since Spring 4.0. In the @PropertySources annotation, only one value property of the PropertySource array type is provided.

Therefore, the @PropertySources annotation can introduce multiple @PropertySource annotations.

2. Annotation usage scenarios

During the Spring-based annotation development project, since Spring's XML file is no longer used for configuration, if the configuration item is written directly into the class, it will cause a tight coupling between the configuration item and the class, and the subsequent modification of the configuration item is very inconvenient, which is not conducive to the maintenance and expansion of the project.

At this time, these configuration items can be written to the properties file or yml file, and the configuration file can be loaded through the @PropertySource annotation.

In addition, if the project itself has a large number of properties configuration files or yml configuration files, it can also be loaded uniformly by Spring's @PropertySource annotation.

3. Use cases

In this section, it mainly implements a loading of properties configuration files through @PropertySource annotation, loading the configuration items in the properties configuration files into Spring environment variables, obtaining the value of the configuration items in the Spring environment variables, and printing them.

The specific implementation steps of the case are as follows.

(1) Add new files

New files are added in the resources directory, and the file contents are as follows:

name=lwk
age=18

(2) Added PropertySourceConfig class

@Configuration
@PropertySource(value = "classpath:")
public class PropertySourceConfig {
}

As you can see, the PropertySourceConfig class is a configuration class of Spring, and the path to the configuration file is specified using the @PropertySource annotation.

(3) Added PropertySourceTest class

public class PropertySourceTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        ConfigurableEnvironment environment = ();
        (("name") + " ====>>> " + ("age"));
    }
}

It can be seen that in the main() method of PropertySourceTest class, the environment variable object environment of the ConfigurableEnvironment type is obtained through the object of the AnnotationConfigApplicationContext class, and then the value of name and age in the configuration file is obtained through the environment object and printed.

(4) Run the PropertySourceTest class

You can see that the values ​​in the configuration file are correctly output.

lwk ====>>> 18

Note: Use the @PropertySource annotation to load the configuration items in the properties configuration file and load the configuration items into Spring's environment variables. The value of the configuration items can be obtained through Spring's environment variables.

This is the article about the analysis of the use scenarios of @PropertySource annotation in Spring. For more information about @PropertySource annotation for Spring, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!