Tutorial: Using Netbeans 7, Maven, and the Spring Framework to inject values for properties

Post to Twitter

Today I’m going to show you how to use the Spring Framework to set properties on beans from the Spring config file. I’ll be using Netbeans 7, Maven and well as Java.


With Netbeans 7 create a new Maven Java Application called: SpringSetterInjection

Go into the generated Maven POM file and add the dependencies for Spring:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>3.0.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.6.RELEASE</version>
</dependency>

The POM file should look like this:

<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.giantflyingsaucer</groupId>
  <artifactId>SpringSetterInjection</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>      
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Add a new class to the project called: DoSomething

DoSomething.java

package com.giantflyingsaucer.springsetterinjection;


public class DoSomething {
    
    private String property1;
    private String property2;

    public String getProperty1() {
        return property1;
    }

    public void setProperty1(String property1) {
        this.property1 = property1;
    }

    public String getProperty2() {
        return property2;
    }

    public void setProperty2(String property2) {
        this.property2 = property2;
    }
    
    @Override
    public String toString() {
        return "Property 1 = " + this.property1 + "   Property 2 = " + this.property2;
    }
}

Now we can add a Spring config file (you can use Netbeans to do this if you wish):

Make sure you place the spring-config.xml in the following path:

All we need to do now is finish the code for the App.java file:

package com.giantflyingsaucer.springsetterinjection;

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

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath:/META-INF/spring-config.xml");
        DoSomething ds = (DoSomething)appContext.getBean("doSomething");
        System.out.println(ds.toString());
    }
}

Run the project. The results should be a message printed out to the output window (in the case of running the project within Netbeans):

You can get the full project source code from here.

Post to Twitter

This entry was posted in Java, Netbeans, Open Source. Bookmark the permalink.