HOW TO CREATE A JAVA APPLICATION IN SIMPLE SPRING FRAMEWORK

What is Spring

The Spring Framework is a very comprehensive framework.
The fundamental functionality provided by the Spring Container is dependency injection. Spring provides a light-weight container, e.g. the Spring core container, for dependency injection (DI).
This container allows to inject required objects into other objects. This results in a design in which the Java class are not hard-coupled. The injection in Spring is either done via setter injection of via construction injection.
These classes which are managed by Spring must conform to the JavaBean standard.
In the context of Spring classes are also referred to as beans or as spring beans.
The Spring core container:
  • handles the configuration, generally based on annotations or on an XML file (XMLBeanFactory)
  • manages the selected Java classes via the BeanFactory
The core container uses the so-called bean factory to create new objects. New objects are generally created as Singletons if not specified differently.

What is Dependency Injection

 Java components / classes should be as independent as possible of other Java classes. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing). To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.
A class A has a dependency to class B if class uses class B as a variable.
If dependency injection is used then the class B is given to class A via
  • the constructor of the class A - this is then called construction injection
  • a setter - this is then called setter injection
The general concept between dependency injection is called Inversion of Control. A class should not configure itself but should be configured from outside.
A design based on independent classes / components increases the re-usability and possibility to test the software. For example if a class A expects a Dao (Data Access object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.
A software design based on dependency injection is possible with standard Java.
Spring just add some simplifications in using dependency injection by providing a standard way of providing the configuration and by managing the reference to the created objects.

 

Download Spring from 

Goto

http://www.springframework.org/download.

and download zip : spring-framework-3.0.4.RELEASE.zip

unzip the zip ..

Goto 

spring-framework-3.0.4.RELEASE\dist (you can find jars those are required for spring to work)

 Goto

http://commons.apache.org/logging/download_logging.cgi

and download zip : commons-logging-1.1.1-bin.zip

 unzip the zip

you can find commons-logging-1.1.1.jar (this is also required)

Now let us start....

Step 1:

Create a new java project in Eclipse and add jars Requried:

Step 2:

Create a jar ,Class and interface:

In Package writer
Interface IWriter 
class Writer 
class NiceWriter 
 
In Package testbean 
class MySpringBeanWithDependency 

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

package writer;

public interface IWriter {
    public void writer(String s);
}

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


package writer;

public class Writer implements IWriter {

    public void writer (String s){
        System.out.println("write "+s);
    }


}

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


package writer;

public class NiceWriter implements IWriter {
    public void writer (String s){
        System.out.println("The string is " + s);
    }
}

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

package testbean;

import writer.IWriter;

public class MySpringBeanWithDependency {
    private IWriter writer;

    public void setWriter(IWriter writer) {
        this.writer = writer;
    }

    public void run() {
        String s = "This is my test";
        writer.writer(s);
    }
}

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

Step 3:

Create bean.xml file most important

 paste following code

<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<bean id="Nicewriter" class="writer.NiceWriter" />
<bean id="writer" class="writer.Writer" />

<bean id="mySpringBeanWithDependency" class="testbean.MySpringBeanWithDependency">
<property name="writer" ref="writer" />
</bean>

</beans>

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

Now everything is done only to test it is working

Step 4:

 create a class

package main;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import testbean.MySpringBeanWithDependency;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "bean.xml");
        BeanFactory factory = context;
        MySpringBeanWithDependency test = (MySpringBeanWithDependency) factory
                .getBean("mySpringBeanWithDependency");
        test.run();
    }
}

Run the main

OutPut you can see as follow:










Comments

Popular posts from this blog

Apache Karaf + Pax CDI OSGI Service

Apache CXF CDI in Karaf (Using CXF and CDI 1.1 (JSR-346))

OSGI + PAX CDI + Drools + Karaf