Spring Introduction
Spring Introduction
Spring Introduction
INDEX
Introduction -----------------------------------------------------------------
1. Introduction 04
a. Java learning is all about 3 things 04
b. Two types of s/w Technologies 05
c. What is Framework? 07
d. Different types of Java Frameworks based on the kind of
application we develop 08
e. Type of Frameworks based on mode of application 12
f. Middleware services/ Aspects/ Cross cutting corners 13
g. Java Bean 14
h. Remote client vs Local client 17
i. How Spring is evolved 18
2. Spring 1.x Modules, 2.x, 3.x, 4.x overview diagrams 20
3. Explain POJI, POJO class, Java Bean class, Bean class/ Component
class and Spring Bean class 31
a. POJO class (Pain Old Java Object class) 31
b. POJI (Plain Old Java Interface) 32
c. Java Bean 33
d. Bean class/ Component class 34
e. Spring Bean 34
4. Spring Framework 36
a. What is Spring? Define Spring 36
b. Two ways of Dependency Management 38
a. Programming language:
- It is directly installable software having basic features to develop
software application. It defines syntaxes (Rules), semantics (structure) of
programming.
- These are base to create software technologies like frameworks, tools,
DB software, Operating systems and etc. as well as Tools.
- These are raw materials of software programming.
e.g., C, C++, C#, Java and etc.
b. Software Technology:
- It is a software specification that provides set of rules and guidelines
for vendor companies to develop implementation software’s by taking
support one or another programming language.
- Technologies are not installable. But technologies-based
implementation s/w are installable or arrangeable. Working these
implementations software is nothing but working with Technologies.
- JDBC is technology giving rules and guidelines to develop JDBC Driver
s/w. Working with JDBC driver s/w is nothing but working with JDBC
Technology.
e.g.: JDBC, Servlet, JSP, EJB, Jndi and etc...
Up-to java7:
Technology rules are the technology API interfaces.
Technology guidelines are the technology API classes (concrete class).
Note: The technology API abstract classes represent both rules and
guidelines.
b. Proprietary Technologies:
Here the technology rules and guidelines are specific to one
vendor and only that vendor is allowed to develop the
implementation softwares.
e.g.: All Microsoft technologies like asp.net.
What is Framework?
Def 1:
Framework is a special installable software that build on top of one or
more technologies having ability to generate common logic
dynamically, this makes programmers to develop only application
specific logics.
It is an installable software that uses existing technologies internally to
simplify the application development having ability to generate
common logics of application internally and dynamically.
Def 2:
Framework is a special installable software that provides abstraction
on one or more technologies and simplifies application development.
It is an installable software that provides abstraction layer on existing
technologies to simplify the application development process.
Note: The process giving details about any resource like class or interface or
file and etc. to underlying serve or container or framework or JVM is called
configuration.
M ----> Model
V ----> View
C ----> Controller
ORM Frameworks:
➢ ORM stands for Object Relational Mapping.
➢ Provides abstraction on plain JDBC/ Java JDBC technology and simplifies
to develop objects-based DB software independent persistence logics.
e.g.
Hibernate ----> From Soft Tree/ now RedHat (1)
Toplink ----> From Oracle Corp.
Eclipse link ----> From Eclipse (3)
iBatis ----> From Apache (4)
OJB ----> From Apache
OpenJPA ----> From Sun MS/ Oracle Corp. (2)
Note: Spring is not good for developing distributed application, so prefer using
Webservices.
Note: RMI, EJB, Corba and etc., distributed technologies to develop distributed
application and they are outdated.
Enterprise Application:
➢ The enterprise can be a standalone app or web application or distributed
app or combination of multiple, but it has to deal complex, heavy weight
business logics/ operations.
e.g.
nareshit.com ----> Web Application
flipkart.com Web ----> Application (Enterprise Application)
PayPal ----> Distributed Application (Enterprise Application)
Flipkart with PayPal/ G-Pay/ Phone-Pay (Enterprise Application)
Invasive frameworks:
➢ Here the programmer developed class of framework-based application
development or tightly couple with framework API, i.e., these classes
must be developed either implementing framework APIs interfaces or
extending framework API classes.
➢ Due to this we cannot out our application/ project classes to other
frameworks for execution.
➢ These frameworks say to programmers come to me, use me and stay
with me forever.
e.g., struts 1.x (outdated)
Note: Our Servlet components class also invasive because it has to implement
javax.servlet.Servlet (l) directly or indirectly.
Non-invasive frameworks:
➢ Here the programmer developed classes of framework-based
application/ project are loosely coupled with frameworks APIs, i.e. these
classes need not to implement or extend from Framework API interfaces
and classes.
➢ So, we can move these classes to another frameworks, because these
classes ordinary classes/ interfaces.
Important terminology:
- Some important terminology before introduce how Spring involved.
a. Middleware services/ Aspects
b. Java Beans
c. Local Client vs Remote Client
Primary logics: These are horizontal because they will be developed parallel
and more ever every primary logic is different from another primary logic.
Secondary logics: These are vertical because they will be developed once and
they are trying to apply in multiple Primary logics.
Solution with Java bean: Use Java bean to carry the data
Bean class
public class StudentBean implements Serializable {
private int sno;
private String name, sadd;
private int m1, m2, m3;
//Setters & Getters
……
……
}
Main class
Public class StudentRegistration {
public string register (StudentBean bean) {
……
…... // Use getter method to read data from
…... // bean object and use them business logics
}
}
Client app
// Create Java bean class object having data
StudentBean bean = new StudentBean ();
bean.setNo(110);
bean.setSname("nimu");
bean.setM1(23); bean.setM2(56);
16 Prepared By - Nirmala Kumar Sahu
//Create main class object
StudentRegistration registration = new StudentRegistration ();
String result = registration.register(bean);
Systen.out.println(result);
Note: In one computer we can start one or more JVMs simultaneously. To run
each Java application one JVM is required. So, to run multiple Java application
simultaneously we need the support multiple JVMs.
EJB:
1998 ----> EJB (Enterprise Java Bean) as Distributed technology runs on
the top RMI.
EJB is no way related to Java bean better not to compare.
EJB is useful to developed Distributed App/ components.
Pros
o Allows both Remote clients and Local clients
o Gives built-in middleware service
o Suitable for medium scale and large-scale apps development
Cons
o Heavy weight (size wise, learning wise)
o Very complex to learn and use
Note: 1998 to 2005 the companies have used EJB even in non-distributed
applications only to take the advantage of transaction management which was
bad practice.
Spring 2.x web module = Spring 1.x web + Spring 1.x web MVC +miscellaneous
Spring JEE module = spring 1.x context module + miscellaneous
3.x/ 4.x/ 5.x ---> 20+ modules.
Spring Core:
It is base module for other modules.
This module is given to supply Spring containers and perform
Dependency management.
Container:
• Container is a software program that manages the whole life cycle of
given component class (class with reusable logic) from birth to death
(object creation to object destruction).
• Servlet container manages the life cycle of Servlet components.
• JSP container manages the life cycle of JSP components.
• Spring container manages the life cycle of Spring bean.
Spring Containers:
• Spring framework gives two built-in spring containers
a. BeanFactory (Basic) These are no way related
b. Application Context (Advanced)
to Servlet, JSP containers.
Dependency management:
• It is the process of assigning Dependent class object to target class
object by loading both classes and creating both objects.
• The class that uses other class service is called target/ main class.
• The class that acts as helper class to main/ target class is called as
Dependent class.
e.g.
Target class Dependent class
Flipchart DTDC
Vehicle Engine
Student Course material
Department Employees
Mobile device SIM card
and etc.
Spring DAO:
DAO stands for Data Access Object.
Provides abstraction on plain JDBC/ Java JDBC technology and simplifies
JDBC style SQL queries-based persistence logic development.
Gives details exception class hierarchy to display different error message
for different database related problems.
Note: Spring DAO also called as Spring JDBC is given to develop JDBC style DB
s/w. Dependent persistence logic using SQL queries.
Spring ORM:
ORM is stands for Object Relational mapping.
Here the object of java classes represents DB table records, so we can
manipulate records through objects.
Provides abstraction on multiple ORM frameworks like Hibernate, iBatis,
Toplink, JDO and etc. and simplifies object-based DB s/w independent
persistence logic without SQL queries.
Spring DAO persistence logic is JDBC style DB s/w dependent persistence
logic using SQL queries where Spring ORM persistence logic is DB s/w
independent object-based persistence logic without SQL queries.
Note: JDBC is not outdated because hibernate, Spring JDBC and other ORM
framework still used JDBC.
Spring Web:
This module provides plugins to make our Spring applications interactive
or communicative from web framework applications like Struts
applications, JSF applications, Webwork applications and etc.
Plugin is a small patch software or code that provides additional
functionalities to existing software or code.
Java application ----> JDBC API ----> DB s/w (Oracle, MySQL and etc.)
Java application ----> Jndi API ----> Jndi registry (RMI registry, COS registry,
Tomcat registry)
Note:
✓ All latest servers like Web logic, Glassfish, Wild fly are having their own
Jndi registry.
✓ Spring Jndi provides abstraction on plain Jndi technology and simplifies
Jndi code development.
- We cannot call methods of ordinary/ normal class object (RMI, EJB not
involved) from remote clients though its object reference is kept in Jndi
registry.
Note:
✓ RMI, EJB both are outdated to develop distributed applications, so
Spring RMI, EJB are also outdated.
✓ Webservice is best option to develop distributed applications.
Spring AOP:
AOP is stands for Aspect Oriented Programming.
AOP is not replacement for OOP, in fact it compliments OOP. i.e., built
on the top of OOP.
The logics that are minimum and mandatory to complete the tasks in
application are called primary logics.
e.g., W.R.T to Banking Application
Withdraw money, deposit money, transfer money, opening account and
etc.
Spring 4.x & 5.x overview Diagram: [Both are look like same]
e.g.
Note: The target and Dependent class we take as Spring Beans to perform
Dependency Management can be taken POJO class.
Java Bean
➢ Java bean is a java class that is developed by following some standards.
➢ Java bean doesn't contain any logic it acts as helper class to carry data
from a one class to another class with in the project, or across the
multiple projects.
➢ In order to pass more than 3 values from 1 class to another class or from
one project to another project take the support of Java bean.
Note: Bean class/ Component class can be developed as POJO class or non-
POJO class based on requirement.
Spring Bean
➢ The Java class whose object is created and managed by Spring container,
i.e. whose life cycle is managed by Spring container is called Spring Bean.
➢ Spring Bean can be pre-defined class, user-defined class or third party
Note:
✓ applicationContext.xml file name is no way related to
ApplicationContext Spring container.
✓ While working with both BeanFactory and ApplicationContext
containers, it is recommended to applicationContext.xml as the Spring
Bean configuration file.
✓ The process giving details about any resource like class or interface or
file and etc. to underlying server or container or framework or JVM is
called configuration
applicationContext.xml
<beans >
<bean id-"dt", class="java.util.Date"/> [fully qualified class name as
Bean ID Spring bean]
<bean id-"wmg", class="com.nt.beans.WishMessageGenerator"/>
</beans>
- With in the Spring application/ Spring container the Spring Beans are not
identifies with their fully qualifies class names. They are identified with their
bean IDs.
Some F&Qs:
1. Can we develop Spring bean as POJO class? Yes,
2. Every POJO class is Spring bean? No (we should configure in XML file).
3. Every Java bean is POJO class? Yes
4. Component class can be taken Spring bean? Yes
5. Every Spring bean must be a component class? No
6. We can java class as Spring bean without using xml, annotations? No
Properties of Spring:
1. Open-source
2. Lightweight
3. Loosely coupled
4. Aspect oriented
5. Non-invasive
6. Dependency Management
Open-source:
• Spring framework is not only free and we get its source code along with
installation. [Extracting zip file], for this reason Spring is an open-source
framework.
• <Spring home>\ libs folders contain jars
representing by codes of Spring modules (<name>-<ver>. release.jar)
docs of Spring modules (<name>-<ver>. release-javadocs.jar)
source code of Spring modules (<name>-<ver>. release-sources.jar)
36 Prepared By - Nirmala Kumar Sahu
Lightweight:
• Spring framework is released as zip having less size.
• Spring framework supplied containers are lightweight containers.
• By just creating object for predefined that implements BeanFactory (l),
we can create BeanFactory container.
e.g.
//Hold the name add location of Spring bean configuration file
Resource rea = new FileSystemResource("<location>/applicationContext.xml");
FileSystemResource is the implementation of
//Create BeanFactory container
Resource (l) that internally uses to java.io.File
BeanFactory factory =
class to hold the name and location of given
new XmlBeanFactory(res);
Spring Bean configuration file.
Note: Servlet container and JSP containers are heavy weight, because we
cannot create/ start programmatically, we need to start the web server/
application server in order to start Servlet container/ JSP container but web
Server/ application server is heavy weight to install and to start.
Loosely coupled:
• If the degree of dependency is less between components, then they are
called loosely coupled.
e.g., TV and Remote
• If the degree of dependency is more between components, then they
are called tightly coupled.
e.g., System and keyboard
Non-invasive:
• The classes of Spring application development can be developed without
having any tight coupling Spring API that means we can develop them as
ordinary classes/ interfaces (POJOs and POJIs), so that we can move to
other frameworks or technologies of java where ever we want. This
takes Spring framework as non-invasive.
Dependency Management:
• It is all about arranging and giving dependent class object to target class
object, so that target class can use dependent class services.
Dependency Lookup:
o Here target class write some logics and spend some time to search and
get dependent class object.
e.g., Student (target) asking for course material (dependent) and getting
from Institute employees.
Note: In dependency Lookup target pulls the dependent from different places
and keeps it ready for target to use.
Dependency Injection:
o Here the underlying server/ container/ Framework/ JVM and etc.
assigns/ injects dependent class object to target class object
dynamically.
o Here dependent vis pushed to target by underlying server/ container/
JVM/ frameworks and etc.
e.g., Students getting course material the moment he joins for the
course.
o Servlet container injects/ assigns ServletConfig object to our servlet class
object the moment Servlet class object created.
o JVM calls constructor automatically to assign initial values to object by
calling constructor the moment object is created.
o @Resourse (-) annotations gets dependent object from Jndi registry and
assigns/ inject to our Servlet class object dynamically.