Sei sulla pagina 1di 27

Welcome to the Spring Session

Chiradip

Lean Software Development Practice


Series
J2EE (Now JEE)
Frameworks

• What?
• Why?
• How? (intention)
• With what? (composition)
J2EE (Now JEE)
Frameworks

✤ Sun J2EE (now JEE)


✤ EJB
✤ JSP-Servlet-JSF
✤ Third party frameworks
✤ Struts
✤ Hibernate
What is Spring
Framework?
• One-stop shop for enterprise
application development
• Spring is modular - you can pick one
or a combination
• Non-intrusive application
development & management
framework
Lets write our first
Spring App...

✤ See it on my iWeb Spring page and


eclipse
IoC Fundamentals

• What is IoC?
• Dependency Injection
• “Plugin pattern” - special mention
IoC Fundamentals (contd...)

✤ Different types of DI
✴ Constructor injection
✴ Setter Injection
✴ Interface Injection
AOP Fundamentals

✤ Aspect orientation is a different paradigm


✤ Special abstraction over object orientation
✤ Example - Fruit is a class and mango,
pineapple, apple etc are objects and this
situation perfectly fits in the object-
oriented space.
AOP Fundamentals (contd...)

✤ Now think of Pest Management!!


✤ What about harvesting then?
AOP Fundamentals (contd...)
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
ofAarule for class,
single matching theanparts
is not of the objectproblem
object-oriented model that the
as such.
functionality will
be applied to. This is analogous to the rule defining when a

✤ Cross-cutting concern
database trigger would apply.
A package of functionality providing the cross-cutting
requirements. A set of
triggers for auditing database access would be analogous to an

✤ Point-cut
AOP aspect for auditing.

The implementation of functionality that will be applied. This


is analogous to the implementation of a database trigger.

✤ Aspect
✤ Advice
Lets write our first
Spring-AOP App...

See it on my iWeb Spring page and


eclipse
Spring AOP
ProxyFactoryBean
ProxyFactoryBean
One of the most important benefits of using a
ProxyFactoryBean or another IoC-aware class to
create AOP proxies, is that it means that advices
and pointcuts can also be managed by IoC. This is
a powerful feature, enabling certain approaches
that are hard to achieve with other AOP
frameworks. For example, an advice may itself
reference application objects (besides the target,
which should be available in any AOP framework),
benefiting from all the pluggability provided by
Dependency Injection.
Spring & ORM

✤ JDBC

✤ Java Persistence API (JPA)

✤ Java Data Objects (JDO)

✤ Hibernate

✤ Common Client Interface (CCI)

✤ iBATIS SQL Maps

✤ Oracle TopLink
Spring & ORM

✤ DAO
Spring & ORM

✤ Helper Classes
Spring MVC

• What is MVC?
• How Spring supports it?
Spring MVC (contd.)

Request Response

• Model Controller View


• View
• Controller
Model
Spring MVC (contd.)

✤ Components
✤ Dispatcher - the entry point of the Spring
MVC is a servlet -
org.springframework.web.servlet.Dispatch
erServlet
<servlet><servlet-name>timetracker</servlet-name>

<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:timetracker-servlet.xml</param-value>

</init-param>
</servlet>
Spring MVC (contd.)

timetracker-servlet.xml

<servlet-mapping>

<servlet-name>timetracker</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>
Spring MVC (contd.)

Mappings

✤ The mapping converts all the incoming


urls into invocations of appropriate controller

✤DispatcherServlet loads the


HandlerMapping by autowiring
Spring MVC (contd.)

✤ Mappings
......

for most purposes SimpleHandlerMapping (spring) is appropriate

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<map>

<entry key="/login" value-ref="loginHandler"/>

<entry key="/accessDenied" value-ref="accessDeniedHandler"/>

<entry key="/admin" value-ref="adminUserListController"/>

<entry key="/admin/list" value-ref="adminUserListController"/>

<entry key="/admin/view/**" value-ref="adminUserViewController"/>

</map>

</property>

<property name="defaultHandler" ref="defaultHandler"/>

</bean>
Spring MVC (contd.)

✤ Controller
✤ controller is the core of the presentation logic for Spring MVC.
Once DispatcherServlet gets the GET request it chooses one suitable
controller from the Mapping.

✤ A sample controller can be like the follows: It is used for single


page rendering so very simple in nature

<bean id="defaultHandler" class=

"org.springframework.web.servlet.mvc.ParameterizableViewController">

<property name="viewName" value="home"/>

</bean>
Spring MVC (contd.)

✤ Controller
✤ We have SimpleFromController as well. It can be overridden to do
the following things:

• Provide reference data to the view for rendering.

• Validate incoming request parameters.

• Assign (and type-convert) incoming request parameters to attributes of a command object representing the form to be rendered.
(This is known as binding the request parameters to the command object.)

• Bind incoming form submissions to the command object.

• Validate the command object upon form submission.

• Forward to the original view if validation of the command object fails.

• Populate the request with error objects representing the points of failure in validation.

• Provide localized messages associated with the validation errors.


Spring MVC (contd.)

✤ Views & Resolvers


✤ Normally in Spring MVC context we use JSP for the
view, and we use it in a plug-able way.

✤ The mechanism used to identify the rendering


mechanism is resolver (view resolver).

✤ UrlBasedViewResolver is a sample and usable class


provided by Spring and can be used when all the views
are processed by the same mechanism.
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
Spring MVC (contd.)

✤ Validation (form)
✤ Exception Handling
Spring MVC (contd.)

✤ Spring Web Flow

<start-state idref="createUser"/>

<view-state id="createUser" view="admin/createUser">

<transition on="preview" to="previewUser"/>

<transition on="cancel" to="listUsers"/>

</view-state>

<view-state id="previewUser" view="admin/previewUser">

<transition on="edit" to="createUser"/>

<transition on="save" to="listUsers"/>

</view-state>

<end-state id="listUsers" view="externalRedirect:/admin"/>


Case study: Order
Management Systems

Potrebbero piacerti anche