In this post we will see how to read values from properties files using Spring @PropertySource
& @Value
annotations. We will also discuss about Spring Environment
interface. We will see corresponding XML configuration as well for side-by-side comparison.
Spring @PropertySource annotations is mainly used to read from properties file using Spring’s Environment interface. This annotation is in practice, placed on @Configuration
classes. Spring @Value annotation can be used to specify expression on field or methods. Common use case is to specify the property from a .properties file along with default value. Let’s see complete example below.
- Spring Boot+AngularJS+Spring Data+Hibernate+MySQL CRUD App
- Spring Boot REST API Tutorial
- Spring Boot WAR deployment example
- Spring Boot Introduction + Hello World Example
- Secure Spring REST API using OAuth2
- AngularJS+Spring Security using Basic Authentication
- Secure Spring REST API using Basic Authentication
- Spring 4 Caching Annotations Tutorial
- Spring 4 Cache Tutorial with EhCache
- Spring MVC 4+AngularJS Example
- Spring 4 Email Template Library Example
- Spring 4 MVC+JPA2+Hibernate Many-to-many Example
- Spring 4 Email With Attachment Tutorial
- Spring 4 Email Integration Tutorial
- Spring MVC 4+JMS+ActiveMQ Integration Example
- Spring 4+JMS+ActiveMQ @JmsLister @EnableJms Example
- Spring 4+JMS+ActiveMQ Integration Example
- Spring MVC 4+Hibernate 4 Many-to-many JSP Example
- Spring MVC 4+Hibernate 4+MySQL+Maven integration example using annotations
- Spring MVC4 FileUpload-Download Hibernate+MySQL Example
- TestNG Mockito Integration Example Stubbing Void Methods
- Maven surefire plugin and TestNG Example
- Spring MVC 4 Form Validation and Resource Handling
Following technologies being used:
- Spring 4.0.6.RELEASE
- Maven 3
- JDK 1.6
- Eclipse JUNO Service Release 2
Project directory structure
Following will be the final project directory structure for this example:
Let’s add the content mentioned in above directory structure.
Step 1: Provide Spring dependencies in Maven pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.websystique.spring</groupId> <artifactId>Spring4PropertySourceExample</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>Spring4PropertySourceExample</name> <properties> <springframework.version>4.0.6.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
Step 2: Create Spring Configuration Class
Spring configuration class are the ones annotated with @Configuration
. These classes contains methods annotated with @Bean
. These @Bean annotated methods generates beans managed by Spring container.
package com.websystique.spring.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @ComponentScan(basePackages = "com.websystique.spring") @PropertySource(value = { "classpath:application.properties" }) public class AppConfig { /* * PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}") annotations. * Remove this bean if you are not using @Value annotations for injecting properties. */ @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
@PropertySource(value = { “classpath:application.properties” }) annotation makes the properties available from named property file[s] (referred by value attribute) to Spring Envirronment
. Environment interface provides getter methods to read the individual property in application.
Notice the PropertySourcesPlaceholderConfigurer
bean method. This bean is required only for resolving ${…} placeholders in @Value annotations. In case you don’t use ${…} placeholders, you can remove this bean altogether.
Above Configuration can be expressed in XML based approach as follows (let’s name it app-config.xml):
<?xml version="1.0" encoding="UTF-8"?> <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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.websystique.spring"/> <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:application.properties</value> </list> </property> </bean> </beans>
Step 3: Create Sample properties file
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/websystique jdbc.username = myuser jdbc.password = mypassword hibernate.dialect = org.hibernate.dialect.MySQLDialect hibernate.show_sql = false hibernate.format_sql = false sourceLocation = /dev/input
We will read the properties from this file using above mentioned configuration in our sample service class.
Step 4: Create Sample service class
package com.websystique.spring.service; public interface FileService { void readValues(); }
package com.websystique.spring.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; @Service("fileService") public class FileServiceImpl implements FileService { @Value("${sourceLocation:c:/temp/input}") private String source; @Value("${destinationLocation:c:/temp/output}") private String destination; @Autowired private Environment environment; public void readValues() { System.out.println("Getting property via Spring Environment :" + environment.getProperty("jdbc.driverClassName")); System.out.println("Source Location : " + source); System.out.println("Destination Location : " + destination); } }
First point to notice is Environment got auto-wired by Spring. Thanks to @PropertySoruce annotation , this Environment will get access to all the properties declared in specified .properties file. You can get the value of specif property using getProperty method. Several methods are defined in Environment interface.
Other interesting point here is @Value
annotation. Format of value annotation is
@value("${key:default") private String var;
Above declaration instruct Spring to find a property with key named ‘key’ (from .properties file e.g.) and assign it’s value to variable var.In case property ‘key’ not found, assign value ‘default’ to variable var.
Note that above ${…} placeholder will only be resolved when we have registered PropertySourcesPlaceholderConfigurer
bean (which we have already done above) else the @Value annotation will always assign default values to variable var.
Step 5: Create Main to run as Java Application
package com.websystique.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import com.websystique.spring.configuration.AppConfig; import com.websystique.spring.service.FileService; public class AppMain { public static void main(String args[]){ AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); FileService service = (FileService) context.getBean("fileService"); service.readValues(); context.close(); } }
Run above program , you will see following output:
Getting property via Spring Environment :com.mysql.jdbc.Driver Source Location : /dev/input Destination Location : c:/temp/output
Since destinationLocation property was not found in application.properties, it’s got the default value.
That’s it.
For XML based configuration , replace
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
with
AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
in above main, no other changes. Run the program and you will see same output.
Download Source Code
References
If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help improve further our learning process.