Tuesday, December 25, 2012

Spring 3 & Properties

This blog will show you how to load properties using Spring with annotations.

1. Write a class to load and give access to properties:

package com.springtutorial.props;

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

@Component
public class AppConfig {

    @Value("${text.to.print}")
    private String text;

    public String getText() {
        return text;
    }
}


2. Here is your class that needs access to properties:

package com.springtutorial.props;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class App {

   @Autowired
   AppConfig appConfig;

   public static void main(String[] args) {
     ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
     AppConfig app = (AppConfig)ctx.getBean("appConfig");
     System.out.println("Text : " + app.getText());
   }
}


3. Here is what you need in ~/workspace/springtutorial/src/main/resources/application-context.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.springtutorial.props" />
    <context:annotation-config />
    <context:property-placeholder location="classpath:app.properties" />

</beans>


4. Your property file is ~/workspace/springtutorial/src/main/resources/app.properties
text.to.print=Hello World!


5. Finally, this is how you need set up your 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.springtutorial</groupId>
  <artifactId>props</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>props</name>
  <url>http://maven.apache.org</url>

  <properties>
        <junit.version>4.10</junit.version>
        <spring.version>3.0.6.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
  </dependencies>
</project>


5. Time to run App.java as a Java Application to see the results.