Tutorial: A working Spring Framework 3.0 example in 5 minutes

Post to Twitter

The Spring Framework has permeated the entire Java ecosystem and has grown to include web frameworks and even integrating into GWT. Today we will use some core Spring functionality.


[ad name=”Google Adsense”]

What you need to complete this tutorial:
1. The Java JDK 1.6
2. Eclipse 3.5 or 3.6, older versions will probably work as well. I’m using Eclipse 3.6 (Helios)
3. The Spring Framework 3.0 (I used version 3.04)

Open up Eclipse and create a new Java project.

Assuming you’ve already downloaded the Spring Framework, you need to extract the file to somewhere accessible.

Going back inside in the Eclipse project add a new folder called “lib”.

Once the lib folder is created you can copy all the jar files from the Spring “dist” folder into the “lib” folder. We don’t actually need all these jars, but for the sake of speed and simplicity we will just copy them all.

Now click on all the newly copied jar files in Eclipse and right-click and select: Build Path -> Add to Build Path

One other jar file we need is the Commons Logging. I grabbed the 1.1.1 version of the from here. Once you have it downloaded grab the “commons-logging-1.1.1.jar” and copy it into the “lib” folder where all the Spring jars are at. Then just like before right-click on the new file and add it to the build path.

Now right-click on the “src” folder in your project and select: New -> Class

The Java code will be short and simple:

package helloworld;

import org.springframework.stereotype.Service;

@Service
public class HelloSpringWorld
{
	public void greeting(String name)
	{
		System.out.println("Hello and welcome to Spring: " + name);
	}
}

Now right-click on the “src” package again and create another new class:

The code for the new file is as follows:

package run;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import helloworld.HelloSpringWorld;

public class RunSpring
{
	public static void main(String[] args)
	{
		ApplicationContext appContext = new ClassPathXmlApplicationContext("bean-data.xml");
		BeanFactory beanFactory = appContext;
		HelloSpringWorld instance = (HelloSpringWorld) beanFactory.getBean("helloSpringWorld");
		instance.greeting("Chad");
	}
}

Finally, right-click on the “src” folder and add a new XML file called “bean-data.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="helloworld" />

</beans>

Your project structure should look like like the following.

Open up the “RunSpring.java” file in Eclipse and run the project. You should see the following in the console.

Very simple, but there you have it, a working Spring example.

You can find the source code here.

[ad name=”Google Adsense – large sqr rounded”]

Post to Twitter

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

7 Responses to Tutorial: A working Spring Framework 3.0 example in 5 minutes

  1. RBNSN says:

    HI,
    I have followed your tutorial and created(or tried to create) the HelloSpringWorld project there is some problem when I try to run the application. The error message that is posted goes like this>

    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@286651: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
    Exception in thread “Main Thread” org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘helloSpringWorld’ is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
    at run.RunSpring.main(RunSpring.java:18)
    I appreciate your kind help. Thank you.

  2. Chad Lung says:

    @RBNSN,

    I just tried going through this tutorial again with a clean project and it worked for me. Are you sure your following all the steps as they are laid out?

    Chad

  3. Iain Hull says:

    @RBNSN I had the same problem, I had changed the package name from the one used in the example. So had to change the package in the “context:component-scan” element’s “base-package” attribute

  4. David Wu says:

    RBNSN, you just need to add this line before the tag.

    For some reasons, it was missing from the bean-data.xml file.

  5. Chantal DeLaure says:

    Hello,

    I followed all of your steps and checked my code against your source code, but this is what I got. Please help.

    Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:160)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:89)
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:59)
    at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:61)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    at run.RunSpring.main(RunSpring.java:13)
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 7 more

  6. Chad Lung says:

    @Chantal

    That error typically means the commons-logging-xxx.jar is missing or it can’t be found in your project structure – add it to the build path.

    Chad

  7. Alfred says:

    Execellent. it is work.
    I need to mention one thing:

    HelloSpringWorld instance = (HelloSpringWorld) beanFactory.getBean(“helloSpringWorld”);

    the “helloSpringWorld” is leading lowcase as the parameter.

Comments are closed.