This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes very new to spring. Need help in  this   small spring  program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "very new to spring. Need help in  this   small spring  program" Watch "very new to spring. Need help in  this   small spring  program" New topic

very new to spring. Need help in this small spring program

hari ann

Joined: Dec 09, 2007
Posts: 11
I am learning spring . i tried to run small program. but it is giving the error. please help where i did wrong

public interface hello
public String sayhello(String a);


<?xml version="1.0" encoding="UTF-8"?>


<bean id="hello"


<property name="greeting">

<value>Good Morning!...</value>




public class helloimpl implements hello

private String greeting;
public helloimpl()



public helloimpl(String a)




public String sayhello(String s)


return greeting+s;


public void setGreeting(String a)





import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;

public class helloclient


public static void main(String args[]) throws Exception




System.out.println("please Wait.");
Resource res = new ClassPathResource("hello.xml");
BeanFactory factory = new XmlBeanFactory(res);
hello bean1 = (hello)factory.getBean("hello");
String s = bean1.sayhello(args[0]);


catch(Exception e1)

{ System.out.println(""+e1); }



after running i am getting the below error

Dec 1, 2008 7:14:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [hello.xml]
java.lang.ArrayIndexOutOfBoundsException: 0

please help in this.
Saradha Konimeti

Joined: Dec 01, 2008
Posts: 4
Are you passing the argument while executing client?
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

If you are just learning Spring, I recommend not using the BeanFactory, but an ApplicationContext object instead.

Also, is your hello.xml in the root of your classpath?

Then the only code you would need to start up Spring is

ApplicationContext context = new ClassPathXmlApplicationContext("hello.xml");

then just use
Hello hello = context.getBean("hello");

I also recommend putting your classes into packages, instead of the root package, like you are doing.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Have you checked out Aspose?
subject: very new to spring. Need help in this small spring program
It's not a secret anymore!