Spring framework makes the easy development of JavaEE application.
Spring is a lightweight framework. It can be thought of as a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc.
Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM, WEB MVC etc.
1. Introduction to Spring Framework
www.collaborationtech.co.in
Bengaluru INDIA
Presentation By
Ramananda M.S Rao
2. Content
Content
Introduction
MVC Architecture
Spring Configuration
Get Started
Dependency Injection
The Spring Container(IOC) and API
Spring and Hibernate Integration
Spring AOP
Transaction Management
Spring Data Validation
Advanced techniques
Spring Controllers and Ajax
Restful Web Services
www.collaborationtech.co.in
3. Content
Remoting and clustering
Spring Security
Java Message Service (JMS)
JMX
Spring OXM(Object XML Mapping)
Test-driven Development
Future Exploration
Spring Best Practices
Project
About Us
www.collaborationtech.co.in
4. Introduction
Spring framework makes the easy development of JavaEE
application.
Spring is a lightweight framework. It can be thought of as a
framework of frameworks because it provides support to
various frameworks such as Struts, Hibernate, Tapestry, EJB,
JSF etc.
Spring framework comprises several modules such as IOC,
AOP, DAO, Context, ORM, WEB MVC etc.
Spring framework was initially written by Rod Johnson and
was first released under the Apache 2.0 license in June 2003.
www.collaborationtech.co.in
5. Spring MVC Architecture
Spring MVC helps in building flexible and loosely coupled web
applications.
The Model-view-controller design pattern helps in separating the
business logic, presentation logic and navigation logic.
Models are responsible for encapsulating the application data.
The Views render response to the user with the help of the model
object.
Controllers are responsible for receiving the request from the user
and calling the back-end services.
www.collaborationtech.co.in
6. Get Started
Step 1 - Create Java Project
create Helloworld.java and Main.java files under the
com.collaba.helloworld package.
Helloworld.java
package com.collaba.helloworld;
public class Helloworld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
www.collaborationtech.co.in
7. Get Started
Main.java
package com.collaba.helloworld;
import org.springframework.context.ApplicationContext;
Importorg.springframework.context.support.ClassPathXmlApplication
Context;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("Beans.xml");
Helloworld obj = (Helloworld) context.getBean("helloWorld");
obj.getMessage();
}
}
www.collaborationtech.co.in
8. Get Started
Step 2 - Create Bean Configuration File
create a Bean Configuration file which is an XML file and acts as cement that
glues the beans ie. classes together. This file needs to be created under the
src directory as shown below:
<?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-3.0.xsd">
<bean id="helloWorld" class="com.collaba.helloworld.Helloworld">
<property name="message" value="HELLO WORLD!"/>
</bean>
</beans>
Step 3 - Running the Program
If everything is fine with your application, this will print the following
message in Eclipse IDE's console.
www.collaborationtech.co.in
9. Autowiring in Spring
Autowiring feature of spring framework enables you to inject the object dependency
implicitly. It internally uses setter or constructor injection.
Autowiring can't be used to inject primitive and string values. It works with reference
only.
A.Java
package com.collaba.autowiring;
//This class contains reference of B class and constructor and method.
public class A {
B b;
A(){System.out.println("a is created");}
public B getB() {
return b; }
public void setB(B b) {
this.b = b; }
void print(){System.out.println("hello a");}
void display(){
print();
b.print(); } }
www.collaborationtech.co.in
10. Autowiring in Spring
B.Java
package com.collaba.autowiring;
//This class contains a constructor and method only.
public class B {
B(){System.out.println("b is created");}
void print(){System.out.println("hello b");
}
}
www.collaborationtech.co.in
11. Autowiring in Spring
Test.java
package com.collaba.autowiring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//This class gets the bean from the applicationContext.xml file and calls the display
method.
public class Test {
public static void main(String[] args) {
ApplicationContext context=new
ClassPathXmlApplicationContext("applicationContext.xml");
A a=context.getBean("a",A.class);
a.display();
}
}
www.collaborationtech.co.in