FSD SpringCore 18042022

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Spring Framework

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. The framework, in broader sense, can be defined as a structure where
we find solution of the various technical problems.

The Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM,
WEB MVC etc.

Advantages of Spring Framework


There are many advantages of Spring Framework. They are as follows:

1) Predefined Templates

Spring framework provides templates for JDBC, Hibernate, JPA etc. technologies. So
there is no need to write too much code. It hides the basic steps of these technologies.

Let's take the example of JdbcTemplate, you don't need to write the code for exception
handling, creating connection, creating statement, committing transaction, closing
connection etc. You need to write the code of executing query only. Thus, it save a lot of
JDBC code.

2) Loose Coupling

The Spring applications are loosely coupled because of dependency injection.

3) Easy to test

The Dependency Injection makes easier to test the application. The EJB or Struts
application require server to run the application but Spring framework doesn't require
server.

4) Lightweight

Spring framework is lightweight because of its POJO implementation. The Spring


Framework doesn't force the programmer to inherit any class or implement any
interface. That is why it is said non-invasive.

5) Fast Development

The Dependency Injection feature of Spring Framework and it support to various


frameworks makes the easy development of JavaEE application.
6) Powerful abstraction

It provides powerful abstraction to JavaEE specifications such as JMS, JDBC, JPA and
JTA.

7) Declarative support

It provides declarative support for caching, validation, transactions and formatting.

Spring Modules
The Spring framework comprises of many modules such as core, beans,
context, expression language, AOP, Aspects, Instrumentation, JDBC, ORM,
OXM, JMS, Transaction, Web, Servlet, Struts etc. These modules are
grouped into Test, Core Container, AOP, Aspects, Instrumentation, Data
Access / Integration, Web (MVC / Remoting) as displayed in the following
diagram.
Test
This layer provides support of testing with JUnit and TestNG.

Spring Core Container


The Spring Core container contains core, beans, context and expression
language (EL) modules.

Core and Beans

These modules provide IOC and Dependency Injection features.

Context

This module supports internationalization (I18N), EJB, JMS, Basic


Remoting.
Expression Language

It is an extension to the EL defined in JSP. It provides support to setting


and getting property values, method invocation, accessing collections and
indexers, named variables, logical and arithmetic operators, retrieval of
objects by name etc.

AOP, Aspects and Instrumentation


These modules support aspect oriented programming implementation
where you can use Advices, Pointcuts etc. to decouple the code.

The aspects module provides support to integration with AspectJ.

The instrumentation module provides support to class instrumentation and


classloader implementations.

Data Access / Integration


This group comprises of JDBC, ORM, OXM, JMS and Transaction modules.
These modules basically provide support to interact with the database.

Web
This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet.
These modules provide support to create web application.

Inversion Of Control (IOC) and Dependency


Injection
These are the design patterns that are used to remove dependency from the
programming code. They make the code easier to test and maintain. Let's understand
this with the following code:

class Employee{
Address address;
Employee(){
address=new Address();
}
}

In such case, there is dependency between the Employee and Address (tight coupling).
In the Inversion of Control scenario, we do this something like this:
class Employee{
Address address;
Employee(Address address){
this.address=address;
}
}

Thus, IOC makes the code loosely coupled. In such case, there is no need to modify the
code if our logic is moved to new environment.

In Spring framework, IOC container is responsible to inject the dependency. We


provide metadata to the IOC container either by XML file or annotation.

Advantage of Dependency Injection

o makes the code loosely coupled so easy to maintain

o makes the code easy to test

IoC Container
The IoC container is responsible to instantiate, configure and assemble the
objects. The IoC container gets information from the XML file and works
accordingly. The main tasks performed by IoC container are:

o to instantiate the application class

o to configure the object

o to assemble the dependencies between the objects

There are two types of IoC containers. They are:

1. BeanFactory

2. ApplicationContext

Difference between BeanFactory and the


ApplicationContext
The org.springframework.beans.factory.BeanFactory and the
org.springframework.context.ApplicationContext interfaces acts as the IoC
container. The ApplicationContext interface is built on top of the
BeanFactory interface. It adds some extra functionality than BeanFactory
such as simple integration with Spring's AOP, message resource handling
(for I18N), event propagation, application layer specific context (e.g.
WebApplicationContext) for web application. So it is better to use
ApplicationContext than BeanFactory.

Using BeanFactory
The XmlBeanFactory is the implementation class for the BeanFactory
interface. To use the BeanFactory, we need to create the instance of
XmlBeanFactory class as given below:

1. Resource resource=new ClassPathResource("applicationContext.xml"


);
2. BeanFactory factory=new XmlBeanFactory(resource);

The constructor of XmlBeanFactory class receives the Resource object so


we need to pass the resource object to create the object of BeanFactory.

Using ApplicationContext
The ClassPathXmlApplicationContext class is the implementation class of
ApplicationContext interface. We need to instantiate the
ClassPathXmlApplicationContext class to use the ApplicationContext as
given below:

ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");

The constructor of ClassPathXmlApplicationContext class receives string, so


we can pass the name of the xml file to create the instance of
ApplicationContext.

Creating spring application in Eclipse IDE

Here, we are going to create a simple application of spring framework using


eclipse IDE. Let's see the simple steps to create the spring application in
Eclipse IDE.

o create the maven java project

o add dependencies in pom.xml

o create the class


o create the xml file to provide the values

o create the test class

Step1

package sp;

public class Student {


private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public void displayInfo(){


System.out.println("Hello: "+name);
}
}

Add below dependency in Pom.xml

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.2</version>
</dependency>
Bean.xml

------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<bean id="studentbean" class="sp.Student">


<property name="sname" value="Vijay"></property>
</bean>

</beans>

Step4
package mproject.com;

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

import sp.Student;

public class App


{
public static void main( String[] args )
{
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
Student s=(Student)ac.getBean("studentbean");
s. displayInfo() ();
}
}

You might also like