Spring MVC Framework: Mohit Gupta

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

Spring MVC Framework

Mohit Gupta
Agenda

1. Framework
2. OOPS Concepts
3. Spring
4. JSP & Servlets
5. Loggers
6. ANT Tool
7. Web Services

August 1, 2016
2
What is a Framework ?

It is an essential supporting structure of a building,


vehicle,
enterprise a or object. In software, framework is used to
construct applications which basically are integrated
With each other
A software framework is a re-usable
design for a software system.
Need of framework
Hotel Owner

Hotel

Hotel Guest Hotel Owner

Without Framework

With Framework
OOP Concepts since JAVA is a OOP
Language

August 1, 2016
5
August 1, 2016
6
Platform Independence

August 1, 2016
7
Spring Overview
Spring Overview

• The Spring Framework is an open source application


framework and inversion of control container for
the Java platform.
• The framework's core features can be used by any Java
application
• Philosophy: “Lightweight Container” concept
What are Lightweight Frameworks?
• Lightweight Frameworks simplify application development
• By removing re-occurring pattern code
• Productivity friendly
• Unit test friendly

• Very pluggable

• Usually open source

• Examples:
• Spring, Pico
• Hibernate, IBatis
Spring 3.0 MVC- Basic Architecture
HandlerMapping
(Map of URL and
2 controllers)

Controller
(Responsibl
Request Dispacth
3
e to handle
1 erServlet request)
(Front controller)
4

5 Model
View (JSP, (POJO
XML, Beans)
Velocity)
Flow diagram Spring framework

August 1, 2016
12
Overview of the Spring Framework
This framework has very loosely coupled components which are widely reusable
and are packaged separately.
Modules of the Spring Framework
The Spring Framework can be considered as a collection of frameworks-
in-the-framework:
1. Core - Inversion of Control (IoC) and Dependency Injection
2. AOP - Aspect-oriented programming
3. DAO - Data Access Object support, transaction management, JDBC-
abstraction
4. ORM - Object Relational Mapping technique for converting data b/w
incompatible type system in OOP language.
5. MVC - Model-View-Controller implementation for web-applications
6. Remote Access, Authentication and Authorization, Remote Management,
Messaging Framework, Web Services, Email, Testing, …
Bean

• Basic JavaBean pattern:


• include a “getter” and “setter” method for each field:

class MyBean {
private int counter;

public int getCounter()


{ return counter; }

public void setCounter(int counter)


{ this.counter = counter; }
}

• Rather than locating needed resources, application components provide getter &
setter methods through which resources are passed in during initialization
• In Spring Framework, this pattern is used extensively, and initialization is usually
done through configuration file(web.xml) rather than application code
Example of BEAN Class
• package player;

public class PersonBean implements java.io.Serializable {

private String name = null;

private boolean deceased = false;

public PersonBean() {
}

/* Getter for property */


public String getName() {
return name;
}

/**
* Setter for property */
public void setName(final String value) {
name = value;
}

/ * Getter for property "deceased"*/


public boolean isDeceased() {
return deceased;
}

/ * Setter for property */


public void setDeceased(final boolean value) {
deceased = value;
}
}
Bean
Autowiring feature of spring framework enables you to inject the object
dependency implicitly. It internally uses setter or constructor injection.
@Bean
@Bean is a method-level annotation and a direct analog of the
XML <bean/> element.
The annotation supports most of the attributes offered by <bean/> such as init-
method, destroy-method, autowiring.

@Configuration
public class AppConfig { @Bean publicTransferService transferService() { return new
TransferServiceImpl(); } }
The above is exactly equivalent to thefollowing appConfig.xml:
<beans><bean name="transferService" class="com.acme.TransferServiceImpl"/> </beans>

August 1, 2016
17
Dependency Injection -Inversion of
Control (IoC)

• Based on “Inversion of Control Containers and the Dependency Injection


pattern”
Spring Provides centralized, automated configuration, managing and wiring of
application Java objects.
• Container responsibilities:
1. creating objects,
2. configuring objects,
3. calling initialization methods
4. passing objects to registered callback objects
5. etc

• All together form the object lifecycle which is one of the most important
features
Dependency Injection-Container resolves (injects)
dependencies of components by setting implementation
object during runtime

Draw()
Circle
Triangle

During Implementation
Dependency Injection - IoC
• Beans define their dependencies through constructor arguments or properties
• Container resolves (injects) dependencies of components by setting
implementation object during the runtime
• BeanFactory interface – It is the core that
loads bean definitions and manages beans
• Most commonly used implementation
is the XmlBeanFactory class
• It allows you to express the objects that compose
application, and the interdependencies
between such objects, in terms of XML
• The XmlBeanFactory takes this XML
configuration metadata and uses it to create a fully configured system
Spring Solutions

• Spring address solutions to major J2EE problem areas:


• Web application development (MVC)
• Enterprise Java Beans (EJB, JNDI)  Annotations+ POJO
• Database access (JDBC,ORM)
• Transaction management ( Hibernate, JDBC)
• Remote access (Web Services, RMI)
Structure
Creating Spring MVC Application
ner
Contai
Servlet
August 1, 2016
24
August 1, 2016
25
August 1, 2016
26
Logging in Java

• log4j is a reliable, fast and flexible logging


framework (APIs) written in Java, which is
distributed under the Apache Software License.
• log4j has three main components:
1. loggers: Responsible for capturing logging
information.
2. appenders: Responsible for publishing logging
information to various preferred destinations.
3. layouts: Responsible for formatting logging
information in different styles.
August 1, 2016
27
Logging Levels

Level Description

ALL All levels including custom levels.

DEBUG Designates fine-grained informational events that are most useful to debug
an application.

ERROR Designates error events that might still allow the application to continue
running.

FATAL Designates very severe error events that will presumably lead the
application to abort.

INFO Designates informational messages that highlight the progress of the


application at coarse-grained level.

OFF The highest possible rank and is intended to turn off logging.

TRACE Designates finer-grained informational events than the DEBUG.

WARN Designates potentially harmful situations.

August 1, 2016
28
August 1, 2016
29
Types of Layouts

• All the possible options are:

1. DateLayout
2. HTMLLayout
3. PatternLayout
4. SimpleLayout
5. XMLLayout

• Using HTMLLayout and XMLLayout, you can generate log in HTML and in
XML format as well.

August 1, 2016
30
ANT-Build Tool
• ANT stands for Another Neat Tool. It is a Java-based build tool from
Apache.
• Need for a Build Tool (DEV OPS)
• On an average, a developer spends a substantial amount of time
doing mundane tasks like build and deployment that include:
• Compiling the code
• Packaging the binaries
• Deploying the binaries to the test server Testing the changes
• Copying the code from one location to another
• To automate and simplify the above tasks, Apache Ant is useful. It is
an Operating System build and deployment tool that can be executed
from the command line.

August 1, 2016
31
What is the use of the build.xml file?

The build.xml file is an Ant script that is created by the Plug-in Development Environment (PDE) to
take your plug-in components and combine them into a deployable format.

This file compiles and archives your plug-in source code into a single JAR file.

The build.properties file controls what goes into your plug-in distribution.

The build.xml file can be created by using the context menu on plugin.xml and selecting PDE Tools >
Create Ant Build File.

Ant build files are low-level mechanisms to package up plug-ins. A much easier way to share and
deploy plug-ins is to create a feature for your plug-in and then an update site.

August 1, 2016
32
Build path vs Class Path

• The build path is used for building your application. It


contains all of your source files and all Java libraries
that are required to compile the application.

• The classpath is used for executing the application.


This includes all java classes and libraries that are
needed to run the java application. A Classpath is
mandatory, the default path is . which is used if the
java virtual machine can't find a user defined path.

August 1, 2016
33
What is the difference between EAR, JAR and
WAR file
 JAR
EJB modules which contains enterprise java beans class files
and EJB deployment descriptor are packed as JAR files with
.jar extenstion
 WAR
Web modules which contains Servlet class files,JSP
FIles,supporting files, GIF and HTML files are packaged as
JAR file with .war( web achive) extension
 EAR
All above files(.jar and .war) are packaged as JAR file with
.ear ( enterprise archive) extension and deployed into
Application Server.

August 1, 2016
34
August 1, 2016
35
WEB SERVICES
Web service is a technology to communicate one
programming language with another. For example, java
programming language can interact with PHP and .Net by
using web services. In other words, web service provides a
way to achieve interoperability.
SOAP & REST Webservices

August 1, 2016
36
Advantages Of MVC Framework

• Controller is separated from Model


• View is separated from Model (View can be seen
on any type of UI-Mobile ,desktop etc)
• View is separated from Controller
• There are different types of MVC Frameworks
• Each is used as per unique requirement
• Popular:-Spring,Struts,Hibernate,JSF

August 1, 2016
37
Advantages of Spring Architecture
• Enable you to write powerful, scalable applications using POJOs
• Lifecycle – responsible for managing all your application components, particularly those in
the middle tier container sees components through well-defined lifecycle: init(), destroy()
• Dependencies - Spring handles injecting dependent components without a component
knowing where they came from (IoC)
• Configuration information - Spring provides one consistent way of configuring everything,
separate configuration from application logic, varying configuration
• In J2EE (e.g. EJB) it is easy to become dependent on container and deployment
environment; Spring eliminates them
• Portable (can use server-side in web/ejb app, client-side in swing app, business logic is
completely portable)
• Thank You

August 1, 2016

You might also like