in Spring

Spring property placeholder


It is very common not to hardcode some of the configuration properties like database password , username and other. But instead to put is in a placeholder and then wire it. lets import properties and use them in the application.

Java configuration

I have created a simple property file named with next content

name=My Knight

Plan is to use value name as a property of Knight bean.

Here i have used @PropertySource annotation to specify property file I want to include. Here is how to include multiple property sources.

Problem with upper example is that it wires Environment just to use one property, this does not make much sense. Solution is @Value annotation.

@Value annotation

Value annotation will use the value of environment property and set the property of the class. In this case “My Knight”

XML Configuration

Same can be achieved with XML configuration using context:property-placeholder.


Spring is awesome!

Write a Comment



  • Spring JPA Repositories tutorial –

    […] For this example I have included using  @PropertySource. Check more about property place holders here. […]

  • Spring Mysql and H2 DataSource with Profiles –

    […] order to use environment properties we need to add spring property placeholder first this is done using @PropertySource […]