Loading a property or system property using spring @Value

In the previous tutorial we saw how you can load properties from a file. In this tutorial we will show you how to inject these properties or system properties into a spring bean using the spring @Value annotation.

Example application.properties file

Here are some properties that we want to inject into a bean.

application.environment = PRODUCTION

Load Properties with Spring Property Placeholder

As you saw in the previous tutorial you can load the properties using the <context:property-placeholder> element.

The PropertyPlaceholderConfigurer looks by default not only in the properties file you specify but also searches for Java System properties if it cannot find a property in the specified properties file. If you want you can customize this behavior by setting the system-properties-mode attribute of the property-placeholder with one of the following values:

  • NEVER (0): this will never check the system properties.
  • FALLBACK (1): [default] this will check system properties if no property could be found in the specified properties file.
  • OVERRIDE (2): this will check system properties first, if no suitable system property is found then it’ll search in the specified properties file.
<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:component-scan base-package="com.memorynotfound"/>
    <context:property-placeholder location="classpath:application.properties"/>

</beans>

Injecting a Property or a System Property using spring @Value annotation

The spring @Value annotation can be placed on fields, methods and method/constructor parameters to specify a default value. In this example we are first loading a property from the application.properties file. Then we also load a system property both using the spring expression language.

package com.memorynotfound;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {

    @Value("${application.environment}")
    private String fileProperty;

    @Value("${java.version}")
    private String systemProperty;

    @Override
    public String toString() {
        return "Config{" +
                "fileProperty='" + fileProperty + '\'' +
                ", systemProperty='" + systemProperty + '\'' +
                '}';
    }
}

Bootstrap Spring and test properties

package com.memorynotfound;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String... args){
        ApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
        Config config = context.getBean(Config.class);

        System.out.println(config);
    }
}

Output

Config{fileProperty='PRODUCTION', systemProperty='1.7.0_67'}

References

Download

You may also like...