d-h-n.de
Blog about Oracle, Linux..

Quick Start with Spring

March 19th, 2012 . by admin

1.Software

 

2.Create User Libraries:

In Eclipse you need 2 user libraries: spring-framework-3.1.1 and commons-logging

Create user library: spring-framework-3.1.1
  • Extract spring-framework-3.1.1.RELEASE.zip to C:Javalibrariesspring-framework-3.1.1.RELEASE
  • In Eclipse: Menu Windows → Preferences
  • → Click Java→Build Path→User Libraries→Click Button New…
  • → Input in field User library name: spring-framework-3.1.1
  • → Ok
  • → Click Button Add JARs..
  • → Select all JAR file from C:Javalibrariesspring-framework-3.1.1.RELEASEdist

Create user library: commons-logging-1.1.1
  • Extract spring-framework-3.1.1.RELEASE.zip to C:Javalibrariesspring-framework-3.1.1.RELEASE
  • Do the same steps like user library spring-framework-3.1.1, create the user lirary commons-logging-1.1.1,
    add only JAR file: commons-logging-1.1.1.jar

Note: Without Apache Commons Logging you get the error message:
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

 

 

3. Create Project in Eclipse

  • Create a Java Project exm. Spring_QuickStart:
    Menu File → New → Java Project
  • Add User Libraries:
    Right click Project Spring_QuickStart → Build Path → Add Libraries → User Libraries →
    add the user libraries spring-framework-3.1.1 and commons-logging

 

 

4.Create a simmple Java class

package model;

public class Prompter {
	private String message;

	public void prompt() {
		System.out.println(message);
	}

	public void setMessage(String message) {
		this.message = message;
	}
}

 

 

5.Create file ..src/beans.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	 http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="myPrompter" class="model.Prompter">
		<property name="message" value="Here is my first Spring program!"></property>
	</bean>
</beans>

 

 

6.Create a Test Class

package model;

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

public class App {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.xml");

		Prompter p = (Prompter) context.getBean("myPrompter");
		p.prompt();
	}

}

 

7.Run Test

Run Class App as Java Application, you see this in the Console:

19-Mar-2012 11:41:39 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@133f1d7:
startup date [Mon Mar 19 11:41:39 CET 2012]; root of context hierarchy
19-Mar-2012 11:41:39 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
19-Mar-2012 11:41:39 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@80fa6f:
defining beans [myPrompter]; root of factory hierarchy
Here is my first Spring program!