Thursday 26 June 2014

Spring framework

Introduction

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Features

Dependency Injection
Aspect-Oriented Programming including Spring's declarative transaction management
Spring MVC web application and RESTful web service framework
Foundational support for JDBC, JPA, JMS
Much more...

Quick Start

Jars needed for spring framework

1. spring-framework-3.2.9.RELEASE (http://maven.springframework.org/release/org/springframework/spring/3.2.9.RELEASE/spring-framework-3.2.9.RELEASE-dist.zip)
2. asm-2.2.3.jar (http://www.java2s.com/Code/JarDownload/asm/asm-2.2.3.jar.zip)
3. commons logging.jar
4. log4j jar

Once you downloaded this jars ,add to your libraries, you'll be able to do the following:

Example

here we have an interface named MessageService which have getMessage() unimplemented method.

hello/MessageService.java


package hello;
public interface MessageService {
    String getMessage();
}


the above Interface is implemented below

hello/MessagePrinter.java

package hello;

package hello;

public class MessagePrinter implements MessageService {

    private String name;
   
    public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String getMessage() {

// TODO Auto-generated method stub
return "Hello "+name;
}
}


hello/Application.java


package hello;

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

public class Application {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
MessagePrinter mp=(MessagePrinter) ctx.getBean("messageprinter");
System.out.println(mp.getMessage());



}

}

spring.xml

<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-2.0.xsd">

<bean name="messageprinter" class="hello.MessagePrinter">
<property name="name" value="jhon"></property>
</bean>

</beans>


 
The example above shows the basic concept of dependency injection.Here we are avoiding concept of creating new operator for object initialization.


If your intrested to learn Spring follow these sites which are very useful:
http://www.tutorialspoint.com/spring/index.htm
for video tutorials follow
https://www.youtube.com/playlist?list=PL2882729612B70122

No comments:

Post a Comment