Here is the Spring 3.0 HelloWorld in Eclipse IDE for Java
Developers. Thanks to http://www.roseindia.net/spring/spring3/spring-3-hello-world.shtml Make a folder at the root of the project. Name it lib
2. Download Spring Framework 3 (e.g. spring-framework-3.1.0.M1-with-docs.zip) This includes documentations also. http://www.springsource.org/download Extract it somewhere and copy the contents of its dist directory to the lib directory you just made, also copy spring-framework-3.1.0.M1-with-docs\spring-framework-3.1.0.M1\projects\spring-build\lib\ivy\commons-logging.jar to the lib directory in eclipse project.
3. Add all the contents of the lib directory as jar files to the project build path (class path)
4. Create package
net.roseindia. In this package package net.roseindia; public class Spring3HelloWorld { public void sayHello(){ System.out.println("Hello Spring 3.0"); } }
In this section we will use the Spring framework to manage the Spring3HelloWorld bean, and then get the bean from the Spring runtime environment (Spring context) and the call the sayHello() method.
5. create a new xml (SpringHelloWorld.xml) in the src directory of the eclipse project.
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="Spring3HelloWorldBean" class="net.roseindia.Spring3HelloWorld" /> </beans>
Spring uses the xml file to configure the spring run-time environment, this file will be referred to by precise name to access bean list
package net.roseindia; import java.util.Map; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.util.Assert; public class Spring3HelloWorldTest { public static void main(String[] args) { XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource( "SpringHelloWorld.xml")); Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory .getBean("Spring3HelloWorldBean"); myBean.sayHello(); } }
we have created the instance of XmlBeanFactory and the retrieved the "Spring3HelloWorldBean". Then we can call the sayHello() method on the bean. The XmlBeanFactory class is extension of DefaultListableBeanFactory that reads bean definitions from an XML document.
Eclipse : Run as --> Java Application
Output: Jan 1, 2010 6:49:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringHelloWorld.xml] Hello Spring |