This example is same as HelloWorld example and prints HelloWorld on console. But, in addition, it also shows how to implement loose coupling in your code. This example is explained here.
This example is using following technologies:
1) Spring-framework-3.0.2.RELEASE
2) Eclipse Java EE IDE Helios Release (Eclipse version 3.6)
3) Java SE 6
Note: The Spring 3.0 requires at least Java 1.5.
Step1:Create anew project.
Create a new Spring project in eclipse as we did in previous HelloWorld example and name it as SpringHelloWorldLoose. Our project structure is :
Create a new Spring project in eclipse as we did in previous HelloWorld example and name it as SpringHelloWorldLoose. Our project structure is :
Step2: Adding Source Code
Our project has four files:
2) HelloWorld.java (Implementation of above interface)
3) MainApp.java (File containing main() function to execute the project)
4) ApplicationContext.xml ( File containing bean configurations)
Add following source to them.
HelloInterface.java
package examples.spring.helloworld;
public interface HelloInterface{
public void sayHello();
}
package examples.spring.helloworld;
public class HelloWorld implements HelloInterface{
public void sayHello(){
System.out.println("Hello World!");
}
}
public class HelloWorld implements HelloInterface{
public void sayHello(){
System.out.println("Hello World!");
}
}
MainApp.java
package examples.spring.helloworld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String args[]){
ApplicationContext ac=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
HelloInterafce helloBean=(HelloInterface)ac.getBean("hello");
helloBean.sayHello();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String args[]){
ApplicationContext ac=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
HelloInterafce helloBean=(HelloInterface)ac.getBean("hello");
helloBean.sayHello();
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="hello" class="examples.spring.helloworld.HelloWorld"/>
</beans>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="hello" class="examples.spring.helloworld.HelloWorld"/>
</beans>
Step3: Adding Jars
In order to run your project you need to add required jars to it. Following jars will be required:
1) org.springframework.beans-3.0.2.RELEASE.jar
2) org.springframework.core-3.0.2.RELEASE.jar
3) org.springframework.context-3.0.2.RELEASE.jar
4) org.springframework.asm-3.0.2.RELEASE.jar
5) org.springframework.expression-3.0.2.RELEASE.jar
6) commons-logging-1.1.1.jar
You can download commons-logging-1.1.1.jar from here. Other Spring framework related jars can be found in dist folder of your downloaded Spring distribution zip file
To add above jars right click on your project then go to Build Path > Configure Build Path.
Click on Add External JARs... . Go to the location where you have stored your jars. Add them one by one.
Step4: Executing the Program
Right Click on the project and go to Run As > Java Application.
You will see the output "Hello World!" in the Console Pane at bottom.
Explanation >>
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment