Java : Spring basics

Download Spring 3.0 RELEASE and copy dist/* and commons-logging.jar to Eclipse project class-path.

There are two ways to Instantiate Spring IoC container
– ClassPathXmlApplicationContext
– FileSystemXmlApplicationContext

Let’s use ClassPathXmlApplicationContext, the config xml will be loaded from the class path.

package sam.spring;

public class HelloSpring3 {
	public void sayHello() {
		System.out.println("Hello Spring 3");

package sam.spring;

import org.springframework.context.ApplicationContext;

public class Driver {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		HelloSpring3 hs3 = (HelloSpring3) context.getBean("helloBean");



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:aop=""

	<bean id="helloBean" class="sam.spring.HelloSpring3" />



Mar 06, 2012 4:43:08 PM prepareRefresh
INFO: Refreshing startup date [Tue Mar 06 16:43:08 IST 2012]; root of context hierarchy
Mar 06, 2012 4:43:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [HelloSpring3.xml]
Mar 06, 2012 4:43:09 PM preInstantiateSingletons
INFO: Pre-instantiating singletons in defining beans [helloBean]; root of factory hierarchy
Hello Spring 3

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: