Follow Nirav Raval

Monday 27 June 2011

Spring Hello World

Spring Hello World
Hello Friends,

Nice to see you again.
In Previous Session  we have seen What is Spring.?.  And learn theory aspect.
Today, we will see the example of  Hello World with Spring  Framework of Java.

So be ready to learn this simple example of Spring Framework Tutorail in JAVA Application..
After apply this code in action, you will be capable of using Spring in your Java Applications.

First Download the required JAR to run Spring Framework in Eclipse as shown Below.
















To use Spring in your project follow below steps.

1) First create HelloWorld.java class file  As shown below.

        The HelloWorld class has a message property and its value is set using the
        setMessage()   method.  This is called setter injection. Instead of directly hard 
        coding the message, we inject it through an external configuration file. 
        The design pattern used here is called Dependency Injection design pattern
The HelloWorld class also has a display() method to display the message.
--------------------------------------------------------------------------------------------------------------------
package com.test;
/**
 *
 * @author Nirav Raval
 * this class is used to set Property Values.
 * and will display it to user.
 *
 */
public class HelloWorld {

 private String message;
 private String name;
 
 public void setMessage(String message) {

  this.message = message;
 }

 public void display()
 {
  System.out.println(message);
  System.out.println(name);
 } 
    public void setName(String name) {
  this.name = name;
 }
}


       -------------------------------------------------------------------------------------------------------------


2)      The next step is to add an entry for this in the bean configuration file.
       The bean configuration file is used to configure the beans in the
        Spring IoC  container.

       To create a new bean configuration file right click the src folder and select
       New -> Spring Bean Configuration File.. Save as beans.xml file.

        And write below lines in it.


           -----------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.test.HelloWorld">
<property name="message" value="Hello World.!">
                              </property>
<property name="name" value="Nirav Raval!">
                             </property>
</bean>
</beans>


Here, If you want to display a differnt message, the only change you need
to is to change the value of the message in the bean configuration
file. This is one of the main benefits of using the Dependency Injection
design pattern, this makes the code loosely coupled.

       -------------------------------------------------------------------------------------------------------------

3)     Now Finally, To dispaly the message, create the following
       HelloWorldApplication.java  class.


       -------------------------------------------------------------------------------------------------------------


package com.test;

/**
 * 
 * @author Nirav Raval
 * 
 */
import org.springframework.context.
      ApplicationContext;
import org.springframework.context.
       support.ClassPathXmlApplicationContext;

public class HelloWorldApplication {

 public static void main(String[] args) {
  
  ApplicationContext context = 
   new ClassPathXmlApplicationContext("beans.xml");
  
  HelloWorld helloWorld = 
   (HelloWorld) context.getBean("helloWorld");
  
  helloWorld.display();
   
  }
}



After done above steps our directory structure of project in Eclipse PAckage Explorer is as below.











Ok this is done.  Now run HelloWorldApplication.java  to see output in console.


The output will be like as shown below.









What we are doing here is,
First we instantiate the Spring IoC container with the bean configuration file beans.xml.
We use the getBean() method to retrive the helloWorld bean from the application context
and call the display() method to display the message in the console.
---------------------------------------------------------------------------------------------------------------


So I hope this code is helpful in understanding the working of Spring 
Framework in JAVA.


Feel free to ask me if you have any dought regarding this.
You can also give me suggessions to improve this tutorial.


Leave comments if you like this post...!


Thank you.
Nirav Raval

Wednesday 22 June 2011

What is Spring


What is Spring.?
Hello Friends,

Nice to see you again.
In Previous Sessions  we have seen Spring Setup in Eclipse IDE..
Now this is the basic core explanation of Spring Framework of JAVA.


So be ready to learn this simple exmaple of Spring Framework..

What is Spring..?
         By definition the Spring Framework is “An open-source layered Java/J2EE application framework having a light-weight container implementing Inversion-of-Control and Aspect Oriented Programming.” 
              
              The key points here are “layered application framework” and “Inversion of Control and Aspect Oriented Programming.” 

These key points can be divided into two broad categories:

1) Patterns

2) Components of the Framework

              The former refers to the design patterns supported by Spring Framework 
              And the latter refers to the components that build up the Framework. 

Spring has two patterns at its core. They are:

1) Inversion-of-Control

2) Aspect Oriented Programming

              The former is also called IoC and 
              The latter is known as AOP. Here are the details.

Inversion-of-Control 
               It is called IoC for short. IoC “is a concept, and an associated set of programming techniques, in which the control flow is inverted compared to the traditional interaction model expressed in imperative style by a series of procedure calls.” 
              In other words, the framework calls the procedures created by the programmer, rather than programmer calling the procedures or methods of the framework. IoC is also known as the “Hollywood Principle.” The Hollywood Principle says “Don’t call me, I will call you.”

              IoC is used to ‘injectdependencies at runtime. When one uses this pattern, the dependencies of a particular object are not satisfied at compile time. Instead, the framework provides the required dependencies to the object at runtime. Thus, this pattern releases the programmer from the chore of providing all the dependencies at compile time.

Aspect Oriented Programming: 
              AOP for short, is by definition “An approach to programming that attempts the separation of concerns, specifically cross-cutting concerns, as an advance in modularization.
              Here, the key point is separation of concerns. Concern is another term for a module of an application, where module refers to a functionality of the application.

              Separation of concerns means that the applications are divided into modules that do not overlap in terms of functionality. However, some functionalities or modules/concerns always overlap other modules/concerns. Logging is an example of such a concern. 
              AOP provides techniques to encapsulate cross cutting or overlapping concerns. In other words, by using AOP a developer can encapsulate cross cutting concerns.

              Spring provides both IoC as well as AOP. They are built into the core of the Spring Framework. The next aspect of the Spring Framework worth noting is its support for layered architecture. It provides this support through its various components. Some of them are:

MVC Component

IOC container

DAO support

These three cater to the Presentation, Business, and Persistence layers. Here is a brief overview:


MVC Component: 
              The flexibility of the Spring Framework is that one can use any MVC framework with it. However, it also provides its own framework, called Spring MVC.  Spring MVC implements MVC-Model 2 for JSP. It does this by providing controller Servlet and JSP taglib for you to view.

IoC Container: 
              The business logic layer can be developed without coupling it with any other layers by using IoC provided by the Spring Framework. The IoC container is the core component of the framework. IoC lets the developer implement the business logic as Plain Old Java Objects (POJO) and wire them up so that they can be injected or called at runtime. This approach lets you avoid specifying the business objects as dependencies at compile time.

DAO Support: 
               Any of the technologies, including JDBC, Hibernate, and iBatis, can be used with the Spring Framework to access persisted data. The Spring Framework provides this functionality through its DAO support component. A developer can plug in almost any of the persistence framework by using the DAO component.

That completes the bird’s eye view of the Spring Framework. Next, let us have a look at the steps involved in using the Spring Framework, specifically the IoC capability of the Spring Framework.

check here for Detailed Overview Of Spring:
http://www.opensourcelive.com/Denver/springbasics.pdf



So I hope this explanation is helpful in understanding the Spring Framework in JAVA.

Feel free to ask me if you have any dought regarding this.
You can also give me suggessions to improve this tutorial.

Leave comments if you like this post...!

Thank you.
Nirav Raval


Monday 20 June 2011

Spring Setup in Eclipse IDE


Spring Setup in Eclipse IDE
Hello Friends,

Today, we will see the plugin setup of Spring Framework in Eclipse IDE.
Here I am using Eclipse 3.4.1 for demonstration.

You can use any version for this, ther is not much changes in different 
version of Eclipse.

The name of plugin for Spring is Spring IDE.



1)  To install Spring IDE, Go to Help -> Software Updates.

Note:  If you are using Eclipse Helios then it will
           Help -> Install New Software...














2)   Then, Click the "Add Site" button and enter
      "http://springide.org/updatesite"
       in the Add Site popup.







3)   Select all the Spring IDE features and click Install.





















Once the installation is complete you are done.
4)  Now let's see how to create new Spring Project using the Spring IDE.
       go to File -> New -> Project.



















Then Select Spring Project From List


























So I hope this explanation is helpful in setting the Spring IDE in Eclipse.

Feel free to ask me if you have any dought regarding this.
You can also give me suggessions to improve this tutorial.

Leave comments if you like this post...!

Thank you.
Nirav Raval