WT - Module - V JSF
WT - Module - V JSF
One of the most popular Java Enterprise Edition (Java EE) frameworks,
Spring helps developers create high performing applications.
1) Predefined Templates
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
5) Fast Development
6) Powerful Abstraction
7) Declarative Support
aspect-oriented programming
object-relational mapping
Java Messaging Service
Object xml Model
develop MVC (Model View Controller) based web
applications.
1. struts2 = webwork + struts1
POJO (Plain Old Java Objects)
Core Container
The Core Container consists of the Core, Beans, Context, and Expression
Language modules.
The Core and Beans modules provide the fundamental parts of the
framework, including the IoC and Dependency Injection features.
The BeanFactory is a sophisticated implementation of the factory pattern.
It removes the need for programmatic singletons and allows you to
decouple the configuration and specification of dependencies from your
actual program logic.
The Context module builds on the solid base provided by the Core and
Beans modules: it is a means to access objects in a framework-style manner
that is similar to a JNDI registry. The Context module inherits its features
from the Beans module and adds support for internationalization (using,
for example, resource bundles), event-propagation, resource-loading, and
the transparent creation of contexts by, for example, a servlet container.
The Context module also supports Java EE features such as EJB, JMX ,and
basic remoting. The ApplicationContext interface is the focal point of the
Context module.
The Expression Language module provides a powerful expression
language for querying and manipulating an object graph at runtime. It is an
extension of the unified expression language (unified EL) as specified in
the JSP 2.1 specification. The language supports setting and getting
property values, property assignment, method invocation, accessing the
context of arrays, collections and indexers, logical and arithmetic operators,
named variables, and retrieval of objects by name from Spring's IoC
container. It also supports list projection and selection as well as common
list aggregations.
Data Access/Integration
The Data Access/Integration layer consists of the JDBC, ORM, OXM,
JMS and Transaction modules.
The JDBC module provides a JDBC-abstraction layer that removes the
need to do tedious JDBC coding and parsing of database-vendor specific
error codes.
The ORM module provides integration layers for popular object-relational
mapping APIs, including JPA, JDO, Hibernate, and iBatis. Using the
ORM package you can use all of these O/R-mapping frameworks in
combination with all of the other features Spring offers, such as the simple
declarative transaction management feature mentioned previously.
The OXM module provides an abstraction layer that supports Object/XML
mapping implementations for JAXB, Castor, XMLBeans, JiBX and
XStream.
The Java Messaging Service (JMS) module contains features for producing
and consuming messages.
The Transaction module supports programmatic and declarative
transaction management for classes that implement special interfaces and
for all your POJOs (plain old Java objects).
Web
The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-
Portlet modules.
Spring's Web module provides basic web-oriented integration features
such as multipart file-upload functionality and the initialization of the IoC
container using servlet listeners and a web-oriented application context. It
also contains the web-related parts of Spring's remoting support.
The Web-Servlet module contains Spring's model-view-controller (MVC)
implementation for web applications. Spring's MVC framework provides
a clean separation between domain model code and web forms, and
integrates with all the other features of the Spring Framework.
The Web-Struts module contains the support classes for integrating a
classic Struts web tier within a Spring application. Note that this support is
now deprecated as of Spring 3.0. Consider migrating your application to
Struts 2.0 and its Spring integration or to a Spring MVC solution.
The Web-Portlet module provides the MVC implementation to be used in
a portlet environment and mirrors the functionality of Web-Servlet module.
AOP and Instrumentation
Spring's AOP module provides an AOP Alliance-compliant aspect-
oriented programming implementation allowing you to define, for example,
method-interceptors and pointcuts to cleanly decouple code that
implements functionality that should be separated. Using source-level
metadata functionality, you can also incorporate behavioral information
into your code, in a manner similar to that of .NET attributes.
The separate Aspects module provides integration with AspectJ.
The Instrumentation module provides class instrumentation support and
classloader implementations to be used in certain application servers.
Test
The Test module supports the testing of Spring components with JUnit or
TestNG. It provides consistent loading of Spring ApplicationContexts and
caching of those contexts. It also provides mock objects that you can use
to test your code in isolation.
Dependency Injection in Spring
Example :
class Employee{
Address address;
Employee(Address address)
{
this.address=address;
}
public void setAddress(Address address)
{
this.address=address;
}
}
A) By constructor method
B) By setter method
1. BeanFactory
2. ApplicationContext
1. BeanFactory
Example :
Example:
Advantage of Autowiring
⚫ It requires the less code because we don't need to write the code to
inject the dependency explicitly.
Disadvantage of Autowiring
⚫ No control of programmer.
⚫ It can't be used for primitive and string values.
Example of Autowiring
N Mode Description
o.
2) byName The byName mode injects the object dependency according to name of the bean.
In such case, property name and bean name must be same. It internally calls setter
method.
3) byType The byType mode injects the object dependency according to type.
So property name and bean name can be different. It internally calls setter method.
4) constructor The constructor mode injects the dependency by calling the constructor of
the class. It calls the constructor having large number of parameters.
Spring Jdbc eliminates all the above mentioned problems of JDBC API.
⚫ We need to write a lot of code before and after executing the query,
such as creating connection, statement, closing resultset, connection etc.
⚫ We need to perform exception handling code on the database logic.
⚫ We need to handle transaction.
⚫ Repetition of all these codes from one to another database logic is a
time consuming task.
Step1: Create Employee table in MySQL
create table employee(
id number(10),
name varchar2(50),
salary number(10)
);
Step2 : Create Employee.java file
import java.io.*;
Import java.sql.*;
public class Employee
{
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
}
Step 3|: Create spring framework using EmployeeDao.java file
import java.io.*;
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;
ORM Tool :
⚫ An ORM tool simplifies the data creation, data manipulation and data
access.
⚫ It is a programming technique that maps the object to the data stored in
the database.
⚫ The ORM tool internally uses the JDBC API to interact with the
database.
What is JPA?
Hibernate Architecture
2) Fast Performance
Hibernate supports Query cache and provide statistics about query and
database status.
Draw backs of Hibernate Framework
⚫ Separate roles : The Spring MVC separates each role, where the
model object, controller, command object, view resolver,
DispatcherServlet, validator, etc. can be fulfilled by a specialized
object.
⚫ Light-weight : It uses light-weight servlet container to develop and
deploy your application.
⚫ Powerful Configuration : It provides a robust configuration for both
framework and application classes that includes easy referencing
across contexts, such as from web controllers to business objects and
validators.
⚫ Rapid development : The Spring MVC facilitates fast and parallel
development.
⚫ Reusable business code: Instead of creating new objects, it allows us
to use the existing business objects.
⚫ Easy to test : In Spring, generally we create Java Beans classes that
enable you to inject test data using the setter methods.
⚫ Flexible Mapping : It provides the specific annotations that easily
redirect the page.