• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning spring . i tried to run small program. but it is giving the error. please help where i did wrong


hello.java

public interface hello
{
public String sayhello(String a);
}


hello.xml

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


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">;
<beans>

<bean id="hello"

class="helloimpl">

<property name="greeting">

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

</property>

</bean>

</beans>

helloimpl.java


public class helloimpl implements hello

{
private String greeting;
public helloimpl()

{

}

public helloimpl(String a)

{

greeting=a;

}

public String sayhello(String s)

{

return greeting+s;

}

public void setGreeting(String a)

{

greeting=a;

}

}
helloclient.java


import java.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;
import org.springframework.core.io.*;

public class helloclient

{

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

{

try

{

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]);
System.out.println(s);

}

catch(Exception e1)

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

}

}


after running helloclient.java 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you passing the argument while executing client?
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
Right! We're on it! Let's get to work tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic