FSD SpringCore 18042022
FSD SpringCore 18042022
FSD SpringCore 18042022
The Spring framework comprises several modules such as IOC, AOP, DAO, Context, ORM,
WEB MVC etc.
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
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
5) Fast Development
It provides powerful abstraction to JavaEE specifications such as JMS, JDBC, JPA and
JTA.
7) Declarative support
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.
Context
Web
This group comprises of Web, Web-Servlet, Web-Struts and Web-Portlet.
These modules provide support to create web application.
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.
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:
1. BeanFactory
2. ApplicationContext
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:
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");
Step1
package sp;
<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">
</beans>
Step4
package mproject.com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sp.Student;