Here we see how we can develope a sample to say hello world using spring3 with configuration xml file.
Please Download spring3 framework from http://www.springsource.org/download
In command prompt, create a directory under E disk or a disk you wish.
>E:
E:> mkdir Spring3
The directory will be created.
Navigate to Spring3 and create tow directories named src and lib
E:\Spring3> mkdir src
E:\Spring3> mkdir lib
Navigate to lib directory and copy necessary jars from spring3 framework downloaded:
Navigate to src directory and create a package called "mypack"
E:\Spring3\src> mkdir mypack;
Now set path and classpath to compile and run the spring console classes which we are going to create.
E:\Spring3\src> set path = %path%;C:\Program Files\Java\jdk1.5.0_19\bin;
E:\Spring3\src> set classpath = E:\Spring3\src;C:\Program Files\Java\jdk1.5\bin;E:\Spring3\lib\org.springframework.aop-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.asm-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.aspects-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.beans-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.context-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.context.support-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.core-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.expression-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.instrument-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.instrument.tomcat-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.jdbc-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.jms-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.orm-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.oxm-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.test-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.transaction-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.web-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.web.portlet-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.web.servlet-3.0.0.RELEASE.jar;E:\Spring3\lib\org.springframework.web.struts-3.0.0.RELEASE.jar;E:\Spring3\lib\commons-logging.jar;E:\Spring3\lib\cglib-2.2.jar;E:\Spring3\lib\asm-debug-all-3.2.jar;
Now navigate to mypack package and create classes as follows:
1) Spring3HelloWorld.java
2) Spring3HelloWorldTest.java
The classes will look like
1) Spring3HelloWorld.java
package
mypack;
public
class Spring3HelloWorld {
public void sayHello(){
System.out.println("Hello Spring 3.0");
}
}
2) Spring3HelloWorldTest.java
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();
}
}
Now navigate to src directory and create SpringHelloWorld.xml as follows:
<?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="mypack.Spring3HelloWorld" />
</beans>
Now compile Spring3HelloWorld.java and Spring3HelloWorldTest.java
E:\Spring3\src> javac mypack\Spring3HelloWorld.java
E:\Spring3\src> javac myPack\Spring3HelloWorldTest.java
Now run the Spring3HelloWorldTest.java as follows:
E:\Spring3\src>java Spring3HelloWorld
The output will be as follows:
Hello Spring 3.0
Now we create spring3 helloworld sample using annotation configuration.
Navigate mypack package and create Spring3HelloWorldConfig.javaand Spring3HelloWorldConfigTest.java as follows:
3) Spring3HelloWorldConfig.java
package mypack;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Spring3HelloWorldConfig {
public @Bean Spring3HelloWorld spring3HelloWorld() {
return new Spring3HelloWorld();
}
}
4) Spring3HelloWorldConfigTest.java
mypack;
Feb 4, 2011 2:56:10 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [SpringHelloWorld.xml]
package
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
public
class Spring3HelloWorldConfigTest {
public static void main(String[] args) { //Initialize IoC Container
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spring3HelloWorldConfig.class);
System.out.println("Calling Bean method: sayHello()"); //Retrieve the bean from Container
Spring3HelloWorld myBean = (Spring3HelloWorld) context.getBean("spring3HelloWorld");
myBean.sayHello();
}
}
Now navigate to src directory and compile Spring3HelloWorldConfig.java and Spring3HelloWorldConfigTest.java
E:\Spring3\src> javac Spring3HelloWorldConfig.java
E:\Spring3\src> javac Spring3HelloWorldConfigTest.java
Now run the Spring3HelloWorldConfigTest.java as follows:
E:\Spring3\src> java Spring3HelloWorldConfigTest
The output will be as follows:
Feb 4, 2011 3:01:07 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1cd8669: startup date [Fri Feb 04 15:01:07 IST 2011]; root of context hierarchy
Feb 4, 2011 3:01:08 PM org.springframework.context.annotation.ConfigurationClassEnhancer enhance
INFO: Successfully enhanced mypack.Spring3HelloWorldConfig; enhanced class name is: mypack.Spring3HelloWorldConfig$$EnhancerByCGLIB$$b89f2401
Feb 4, 2011 3:01:08 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5224ee: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,spring3HelloWorldConfig,spring3HelloWorld,spring3HelloSubbu]; root of factory hierarchy
Calling Bean method: sayHello()
Hello Spring 3.0
Use the attachment in this page to include in eclipse as it is and just run the application.