0% found this document useful (0 votes)
20 views32 pages

Day 9-EL, RB, Spring Framework, IOC, DI.

EL, RB, Spring framework, IOC, DI.

Uploaded by

77rccbgkqb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views32 pages

Day 9-EL, RB, Spring Framework, IOC, DI.

EL, RB, Spring framework, IOC, DI.

Uploaded by

77rccbgkqb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Deloitte

Training
2016

FOUNDATION TRAINING
Expression Language and
Introduction to Spring 1
Deloitte Training 2016

EL, Custom tags, Introduction to Spring


Day 9

3
Objectives of Day 9 Deloitte Training 2016

At the end of this module, you will be able to:


 Implement Expression Language

 Explain custom tags

 Implement custom tags

 Work on

 The Physical Overview of Maven2

 The Maven repositories

 Maven Archetypes

 Maven 2 life cycle phases

 How to set the environment for Maven 2

 Basic programming in Spring technology

4
Accessing Implicit Objects Deloitte Training 2016

Using EL
 EL provides implicit objects that can be used to easily access
all implicit objects and scoped variables of JSP.
 The following code snippet shows the use of an implicit
object:
 My Email is: ${initParam.myEmail}

5
Accessing JavaBean Using EL Deloitte Training 2016

 A JavaBean is a simple Java class that exposes internal fields as properties


using the corresponding getter and setter methods.
 A JavaBean is a reusable and a self-contained software component that
takes advantage of all the security and platform-independent features of
Java.
 A JavaBean can be included in a JSP page to start processing user
requests.
 To be used as a JavaBean, a class must be concrete. In addition, it should
be public, and have a constructor that does not accept any arguments.
 JavaBeans components can be effortlessly accessed by using EL
expressions. EL provides various attributes and properties to access a
JavaBeans component.
 An attribute of a JavaBeans component can be accessed by using the
following syntax:
 ${bean.attribute}

6
Handling Null Values Deloitte Training 2016

Using EL
 One of the advantages of EL is that it can handle null values,
and therefore avoid throwing an exception.
 If you try to display the value of an undefined variable by using
the EL expression, nothing will be displayed.
 For example, if the variable, var is not declared and assigned a
value, but you are retrieving the value in the JSP page, it will not
throw an exception. The following code snippet shows this
implementation:
 The value is: ${var}

7
Operators Supported by EL Deloitte Training 2016

 The following table describes the arithmetic operators used


by EL to evaluate expressions.
 The following code snippet shows the use of the + arithmetic
operator in EL:
o The sum is ${3+5}
Operator Description
+ It represents the addition operator.
- It represents the subtraction operator.
* It represents the multiplication operator.
/ and div It represents the division operator.
% and mod It represents the remainder operator.

8
Custom Tags Deloitte Training 2016

 A custom tag is a reusable component.


 It provides a mechanism that can be used by a Web developer to
encapsulate complex recurring code or tasks.
 Once encapsulated, these codes can be reused in a simpler form
as tags in a JSP page.
 Custom tags provide the following benefits:
 Reduction of scriptlets in the code

 Reusability

9
contd.. Deloitte Training 2016
Custom tags can be classified into the following categories:
 Empty tags

 The following code snippet shows an empty custom tag:

 <td:welcome />

 Tags with attributes

 The following code snippet shows a custom tag with an attribute


named color:
 <td: welcome color=”blue”></td:welcome>

 Tags with a body

 The following code snippet shows a custom tag that contains a JSP
scripting element as its body:
 <td:welcome>

 <%=today_date%>

 </td:welcome>

10
contd.. Deloitte Training 2016

To create custom tags, you need to perform the following tasks:


 Create a tag handler

 Create the Tag Library Descriptor (TLD) file

11
Using Custom Tags Deloitte Training 2016
You can use custom tags in a JSP page after creating the desired tag
handlers and TLD file(s). To use custom tags in a JSP page, you need to
perform the following tasks:
1. Place the desired TLD file(s) in the WEB-INF folder of the Web
application.
2. Place the class file of the tag handler in the WEB-INF/classes
folder of the Web application.
3. Add a taglib directive in the desired JSP page, as shown in the
following code snippet:
<%@ taglib prefix="ct" uri="/WEB-INF/tld1.tld"%>
4. Use the custom tag at the desired location in the JSP page, as
shown in the following code snippet:
<ct:TestTag/>

12
Deloitte Training 2016

Spring
Introduction and Background
 Background –
 Applications developed using J2EE consist of many
components.
 These components belong to different frame works either
within Java/J2EE or exterior.
 Application server offers transaction management,
multithreading, security, etc.
 Despite this, the result is heavy weight application which
includes services even when not needed.
 Developers and architects spend lot of time in setting up
configurations, using xml files.
contd..
 The 'look-up' problem.
 When a component C1 requires another component C2,
then C1 itself is responsible for looking up the C2 as it
depends upon.
 This look-up happens by name, so the name of the
dependency is hardcoded in the component (code or
deployment descriptor).
 Components communicate with each other from J2EE
application servers of different vendors is almost always
problematic.
 Many components do not need all the services the
application server provided, but since there was no other API
to build components, all your components became heavy
weight, bloating the application.
contd..
 Coding against the J2EE standards is very cumbersome, as
user needs to comply to several seemingly arbitrary rules,
and write code that is not so Object Oriented.
 The classes have to be implemented in a very specific way,
which coupled the business logic to the J2EE classes.
 The code less extensible, and not very test friendly as it uses
specific J2EE classes and interfaces.
 The code cant be run in a unit test, since it presumes all
kinds of services present, normally provided by the J2EE
application server i.e. it is not testable outside of the
container.
 Deploying a J2EE application in a container and starting it
up can be a very time-consuming task, so not very ideal if
test cases need to run unit tests regularly.
contd..
 Solution is to write just POJOs, without the J2EE standards
overhead.
 No overhead to implement any interfaces or extend from
other classes,
 Clean implementation and design of domain with regular
Java classes.
 Disadvantage with POJOs is cannot benefit from the
services provided by the container, such as transaction
management, remoting, etc.
SPRING INTRODUCTION
 Spring provides a framework that integrates all kinds of Java
technologies/API's and makes it possible to use them with
simple POJO's.
 Spring does not reinvent the wheel. It provides a nice and
elegant way to use existing technologies viz.. EJB, JDO,
Hibernate, Toplink, JMS, etc. This is accomplished by several
support classes and 'templates‘.
 The programming model is built upon Inversion of Control
and Aspect-Oriented Programming. The user code need not
have Spring references all over the place.
 Spring integrates easily using these techniques by placing
specific frameworks where needed.
Inversion of Control
 Introduction –
 Developing an application, involves dependencies between
and on components, services, classes etc.
 These are 'wired’ together on the spot where needed.

 The disadvantage of this approach is that when user needs


to use a different implementation of the dependency, its
necessary for code change.
 Involves huge cost if change is necessitated in
implementation context of the environment.
 E.g. to use a different authentication service during
development as in production. It is not really convenient to
change code every time to make a production artifact, or
each time to run unit-tests.
contd..
 So the wiring of these dependencies is taken out of the code, and
an external party manages the wiring, namely the
CONTAINER.
 Hence the name inversion of control, i.e. something from the
outside controls how dependencies are wired together.
 Its also Dependency injection because the container 'injects' the
necessary dependencies instead of letting the developer manage
them.
 In principle, a framework is not needed to inject dependencies
into the code; In practice, applications built by using inversion of
control use a framework of some type to carry out the dependency
injection.
 Typically, as with Spring, these read configuration information
and then use the Java reflection API or bytecode manipulation to
invoke the appropriate methods on your code to inject the
dependencies.
contd..
 The one disadvantage that Spring have over hard-coding of
dependencies:
 that they lose some of the advantages of static type checking.

 the configuration information will not be read until runtime;


therefore, any incompatible type information given in the
configuration will not cause errors to be produced until
runtime.
contd..
 Dependency injection
 Beans define their dependencies through constructor
arguments or properties
 The container provides the injection at runtime

 Also known as the Hollywood principle – “don’t call me I will


call you”
 Decouples object creators and locators from application logic
 Easy to maintain and reuse
 Testing is easier
Non-IoC / Dependency Injection
Non-IoC Service Object
public class OrderServiceImpl implements IOrderService {
public Order saveOrder(Order order) throws OrderException{
try{
// 1. Create a Session/Connection object
// 2. Start a transaction
// 3. Lookup and invoke one of the methods in a
// DAO and pass the Session/Connection object.
// 4. Commit transaction
}catch(Exception e){
// handle e, rollback transaction, //cleanup, // throw e
}finally{
//Release resources and handle more exceptions
}
}
IoC / Dependency Injection
IoC Service Object
public class OrderSpringService implements IOrderService {
IOrderDAO orderDAO;
public Order saveOrder(Order order) throws OrderException{
// perform some business logic…
return orderDAO.saveNewOrder(order);
}

public void setOrderDAO(IOrderDAO orderDAO) {


this.orderDAO = orderDAO;
}

• Program to interfaces for your bean dependencies!


Data Access
 Spring offers support for the following APIs and
frameworks:
 JDBC
 Java Persistence API (JPA)
 Java Data Objects (JDO)
 Hibernate
 Common Client Interface (CCI)
 iBATIS SQL Maps
 Oracle TopLink
contd..
 Different data access libraries supported by Spring have
quite different implementations, they do tend to have
similar usage patterns.
 Spring takes advantage of this by providing sets of tailored
support classes to aid in the building of data access logic,
and specifically to aid in building DAO implementations.
 To build a DAO for a supported database access
mechanism, Spring provides helper classes to aid in
implementation.
 These usually include a template class and a DAO support
class.
contd..
 Helper Classes for the Supported Database Access
Mechanisms
FrameworkDAO SupportClass TemplateClass
CCI CciDaoSupport CciTemplate
Hibernate* HibernateDaoSupport HibernateTemplate
JDBC JdbcDaoSupport JcTemplate
JavaDataObjects JdoDaoSupport JdoTemplate
JavaPersistenceAPI JpaDaoSupport JpaTemplate
iBATIS SQLMaps SqlMapClientDaoSupport SqlMapClientTemplate
OracleTopLink TopLinkDaoSupport TopLinkTemplate
Aspect Oriented Programming
 Introduction –
 In most applications there are concerns that 'cut' across
different abstraction layers.
 E.g. logging. every method of the service layer that is used
needs to be logged on entry & exit.
 This scatters log statements all over the service layer, while
logging is actually one concern and as such should be
separated from the business logic into a different entity.
 This is what Aspect-Oriented Programming frameworks
aim to do.
 In AOP terminology a concern is written as an advice,
which is an entity like a class.
contd..
 Then this advice is applied to certain pointcuts in the code.
A pointcut is a place or several places in the code where the
crosscutting concern, the advice, is to be applied.
 An advice together with a pointcut is called an aspect,
hence the name Aspect-Oriented Programming.
 There are many AOP frameworks, Spring uses its own
based upon dynamic proxies and/or CGLIB byte code
generation, but can integrate with others like AspectJ if
desired.
contd..
 Terminology
 Cross-cutting concern: A problem that applies to parts of the
object model that are not conveniently related, or that are not
related in an object-oriented manner. For example, a problem
that applies to method return values in general, rather than to
the methods of a single class, is not an object-oriented problem
as such.
 Pointcut: A rule for matching the parts of the object model that
the functionality will be applied to. This is analogous to the rule
defining when a database trigger would apply.
 Aspect: A package of functionality providing the cross-cutting
requirements. A set of triggers for auditing database access would
be analogous to an AOP aspect for auditing.
 Advice: The implementation of functionality that will be
applied. This is analogous to the implementation of a database
trigger.

You might also like