Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts
Wednesday, February 13, 2013

Spring MVC Tutorial - What is Dependency Injection


Spring MVC Tutorial - What is Dependency Injection

Dependency Injection is a Software design Pattern that allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile-time.

When an Object is created in Spring Framework it is not necessary to call it explicitly like for example i am making two classes here.

Class1 :-

public class Test
{
public void showMessage()
{
System.out.println("Hello World");
}
}

now i make an another class for calling the showMessage() method.

public class TestRun
{
public static void main(String st[])
{
Test t=new Test();
t.showMessage();
}
}

The above method is hard coding method to create an Object explicitly but the Spring Framework provide certain classes for doing this automatically with the help of a XML file this technique is called Dependency Injection 


to do this we need to change the TestRun class as below.



public class TestRun
{
public static void main(String st[])
{
BeanFactory factory=new XmlBeanFactory(new FileSystemResourse("spring.xml"));
Test t=(Test)factory.getBean("test");
t.showMessage();
}
}

Now we need to map the bean into XML file like this.



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

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

<beans>
<bean id="test" class="Test"/>

</beans>




Spring Framework Download - Spring Framework and its all related JARs Download


Spring Framework Download - Spring Framework and its all related JARs Download

The Spring Framework is an Open Source application Framework and inversion of control container for the java platform  It is developed by Rod Johnson in 2003. Spring is a Complete and a modular framework, means spring framework can be used for all layer implementation for a real time application. 


To Use The Spring Framework you need an IDE like Eclipse, Net Beans or any other. and need to be install the Spring Framework JARs which contains the all API's related to the Spring.

Download The Spring Framework from 

the official site of Spring Framework
Thursday, January 24, 2013

Spring Framework Tutorials for Beginners, Spring Framework Step by Step, Hello World Example in Spring Framework with Eclipse, web.xml in Spring Framework


Spring Framework Tutorials for Beginners - Spring Framework Step by Step, web.xml in Spring Framework.


Hello World Example in Spring Framework with Eclipse

Hello and Welcome to all who are new in Spring Framework. I am going to give a quick overview about spring framework and how to make a simple Example like "Hello World" Example......

I am Using Eclipse IDE for this example and we need the Spring Framework JAR's from so download these JAR's from spring official site and Apache commons-logging-1.1.1-bin.zip  from apache official site.

Now First we have to create a new Project in Eclipse IDE. Click on  New from Toolbar --> Other..-->choose Java Project.




Give it a name like HelloWorld.
click on finish button.

then first we want to add the external Spring JAR's which we have downloaded in first step. right click on your HelloWorld project select properties like this


then the property window will appear like this and choose java build path
then click on Libraries tab then click on Add Library then the Add Library window will appear then choose User Library like this


click on Next then click on User Libraries then a User Libraries window will appear.


then click on New then a new window will appear with the name New User Library give it a name like Spring like this.


then click on Ok then click on Spring Liabrary which is appear now in your User Libraries then click on Add External JAR's then select the downloaded JAR's form your Computer's drives like this
first we will add the Spring JAR's from lib folder under the spring-framework-3.2.0.RELEASE


Select all JAR's from there and open them then the all JAR's will be available under the Spring Library
 then again add the Apache commons.logging JAR like this


Select the only one JAR commons-logging-1.1.1 from commons-logging-1.1.1 folder which you have downloaded in first step.
Now you are ready to develop a Spring project Now right click on your project HelloWorld then click on New -->Class



then give a name to your Class and give package name as well like this 


I have given the package name W3STutorial and Class name HelloWorldExample
Now create a new class by the name Message under the W3STutorial the code for each class are as follows.....


HelloWorldExample.java

package W3STutorial;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorldExample {

public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Message message=(Message)context.getBean("message");
message.showMessage();

}

 

  
Message.java



package W3STutorial;

public class Message {
private String msg;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
public void showMessage()
{
System.out.println(msg);
}

}


then Create a XML file like this....... right click on src folder under your project and click on New-->Other...-->XML-->XML File and then Next and give it a name Spring.xml.....

                         

under the Spring.xml write this code...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>
    <bean id="message" class="W3STutorial.Message">
    <property name="msg" value="Hello Wold"/>
    </bean>
    
    
    </beans>


Now your first Spring project is ready to Run this Project right Click on your project and then Run As then click on Java Application like this



 if everything is Ok then your project run successfully the message shown as follows









the Hello World Message show in bottom of your IDE...


So I wish now you are ready to make your first Spring Project if you want to know more about Spring then post your comments here.
All the Best




You Might Also Like