Using properties in Spring configuration file

In some previous tutorial, I already introduce with you all about how to read properties file in Spring using PropertyPlaceholderConfigure object, namespace util or namespace context. So, how we can use these properties in Spring configuration file. In this tutorial, I will guide you all about that.

First of all, I will create new Maven project for example:

Using properties in Spring configuration file

Spring framework dependency:

  • HelloWorld class:

  • Application class:

  • configuration.properties

  • spring.xml

Here, I used namespace context to read the configuration.properties.

To use these properties in Spring configuration file, we need declare them with following syntax:

With this declaration, Spring will ingest value of the property into beans in Spring configuration file automatically.

Example, I will declare HelloWorld object as a bean in Spring configuration file with the value of name attribute is the value of property name in configuration.properties, as below:

Result:

Using properties in Spring configuration file

Note, if a property was declared in multiple properties file then the value of this property will be in the last declaration properties file.

Example, I add more properties file, named configuration-override.properties

Using properties in Spring configuration filewith following content:

and declare using this properties in namespace context:

then when running, result as below:

Using properties in Spring configuration file

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment