introducing spring

60
Introducing Spring Framework Introducing Spring Framework

Upload: ernesto-hernandez-rodriguez

Post on 05-Aug-2015

118 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introducing spring

Introducing Spring FrameworkIntroducing Spring Framework

Page 2: Introducing spring

Who am I ?

Page 3: Introducing spring

Developing Software

Page 4: Introducing spring

“I’m not a great programmer, I’m just a good programmer with great habits.”

Kent Beck

Page 5: Introducing spring

Our toolbox

• Clean code

• TDD

• Design patters

• Frameworks

• ...

Page 6: Introducing spring

Clean code

Source code are for humans not computers!

“Code Smells”, Martin Fowler

Page 7: Introducing spring

TDD

Page 8: Introducing spring

Design Patterns

Page 9: Introducing spring

Frameworks

Page 10: Introducing spring

Choose your weapon !

Page 11: Introducing spring

Spring FrameworkSpring Framework

Page 12: Introducing spring

The Spring Framework

• Best practices

• Proven Solutions

• OpenSource

• Continuously growing

Page 13: Introducing spring

Spring Ecosystem

Spring Framework

Spring WebFlow

Spring MVC

Spring Dynamic Modules

Spring Security

Spring Batch

Spring Web Services

Spring ROO

Spring Android

Spring Integration

Spring SocialSpring Data

And many more...

Page 14: Introducing spring

In a Nutshell

• IoC container

• Lightweight

• Comprehensive coverage of AOP

• TDD

• Modular

Page 15: Introducing spring

Where?

• Junit system Test

• Java EE web application

• Java EE enterprise application

• Standalone Java application

Page 16: Introducing spring

What?

Spring Framework provides comprehensive infrastructure support for developing Java applications.

−Spring deals with the plumbing

−So you can focus on solving the domain problem.

Page 17: Introducing spring

You can build applications from “plain old Java objects” (POJOs) and to apply enterprise services non-invasively to POJOs.

And that means...

Page 18: Introducing spring

Spring IoCSpring IoC

Page 19: Introducing spring

Inversion of Control (IoC)

“Is a programming technique, in which object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.”

Page 20: Introducing spring

How?

Page 21: Introducing spring

Dep

enden

cy Inje

ctio

nSimpleObject

Asp

ect-Orien

ted P

rogram

min

g

Enterprise Service Abstractions

Spring Triangle

Page 22: Introducing spring

Production

Page 23: Introducing spring

Unit Test

Stub / Mock

Page 24: Introducing spring

Local development

H2

Page 25: Introducing spring

Deployments

Development Test QA Production

orMocks

+Dummy data

+Dump real data

+Real data

Page 26: Introducing spring

Dependency Injection (IoC)

• XML

• Annotations

• Java

Page 27: Introducing spring

TransferService

Page 28: Introducing spring

AccountRepository

Page 29: Introducing spring

XML

app-config.xml

Page 30: Introducing spring

And run it !

Page 31: Introducing spring

Annotations

Page 32: Introducing spring

XML

app-config.xml

Page 33: Introducing spring

And run it !

Page 34: Introducing spring

Java Configuration

Defined outside !!

Page 35: Introducing spring

Spring AOPSpring AOP

Page 36: Introducing spring

Aspect-Oriented Programming

“AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns”

Page 37: Introducing spring

Cross-Cutting Concerns?

Generic funcionality that is needed in many places in your application

− Security− Caching− Logging− ...

Page 38: Introducing spring

Tangling

Mixing of concerns

Page 39: Introducing spring

Scattering

Duplication

Page 40: Introducing spring

How?

ExceptionReporterAdvice(Aspect)

TransferServiceImpl (Target)

Spring AOP Proxy

Aspect Target

transfer(300,1,2)

AOP

<<Interface>>TransferService

Page 41: Introducing spring

With Spring AOP

TransferServiceImpl.javaTransferServiceImpl.java

ExceptionReporterAdvice.javaExceptionReporterAdvice.java

<beans> <aop:aspectj-autoproxy> <aop:include name=“exceptionReporter” /> </aop:aspectj-autoproxy>

<bean id=“exceptionReporter” class=“...ExceptionReporterAdvice” /></beans>

Spring-aop.xmlSpring-aop.xml

Page 42: Introducing spring

What about ... ?

Page 43: Introducing spring

Web Access

HTTP / HTML

Page 44: Introducing spring

Spring Web Integration

• Spring provides support in the Web layer

− Spring MVC, Spring WebFlow, …

• However, you are free to use Spring with any Java web framework

− Struts,Tapestry, JSF, WebWork, Wicket, ...

Page 45: Introducing spring

web.xml

<context-param> <param-name>contextConfigLocation</param-name> <param-value>

/WEB-INF/spring/app-config.xml </param-value></context-param>

<listener> <listener-class>

org.springframework.web.context.ContextLoaderListener </listener-class></listener>

Page 46: Introducing spring

Spring MVCSpring MVC

Page 47: Introducing spring

Model - View - Controller

Page 48: Introducing spring

Spring MVC

DispatcherServlet Handler

View

model + View

render(model)JSP

@ControllerBlazeDS (Flex)

WebFlow...

JSPPDFExcel

...

request

response

HandlerMapping HandlerAdapter

HandlerMapping HandlerAdapter

ViewResolverViewResolver

Page 49: Introducing spring

Spring MVC

Page 50: Introducing spring

Quick Start

1. Deploy a Dispatcher Servlet

2. Implement a request Handler (Controller)

3. Implement the View

4. Register the Controller

5. Deploy and Test

Page 51: Introducing spring

Deploy Dispatcher Servlet

web.xml<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>

/WEB-INF/spring/mvc-config.xml </param-value> </init-param></servlet>

<servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/app/*</url-pattern></servlet-mapping>

Don't forget ContextLoaderListener (app-config.xml)

Page 52: Introducing spring

Implement Controller

Page 53: Introducing spring

Register Controller

/WEB-INF/spring/mvc-config.xml

<beans>

<bean class=”org.spf.web...InternalResourceViewResolver”> <property name=”prefix” value=”/WEB-INF/views/” /> <property name=”suffix” value=”.jsp” /> </bean> <bean class=”com.extrema...TransferController” />

</beans>

Page 54: Introducing spring

Implement View

<html>

<head><title>Your transfer</title></head>

<body>

<h1>Your transfer </h1>

Id = ${transfer.id} <br/>

Date = ${transfer.date} <br/>

Amount = ${transfer.amount} <br/>

Credit = ${transfer.credit} <br/>

Debit = ${transfer.debit} <br/>

</body>

</html>

/WEB-INF/views/transfer.jsp

Page 55: Introducing spring

Deploy and Test

Your transfer Id = 1 Date = 2013/01/01 Amount = 300.0 Credit = 1234 Debit = 5678

http://localhost:8080/app/transfer/1 http://localhost:8080/app/transfer/1

Page 56: Introducing spring

… and much more

• Stateless converter for binding and formatting

• Support for JSR-303 declarative validation

• I18n

• Themes

• Content Negotiation

• Support for RESTful web services

• WebFlow

Page 57: Introducing spring

SummarySummary

Page 58: Introducing spring

Conclusions

• Developing software is a craft.

• We need tools to help us in the process.

−Good practices

−Frameworks

• It depends on you to choose the right one for your needs.

• Spring is just one of them.

Page 59: Introducing spring

Q&AQ&A

Page 60: Introducing spring

Ernesto Hernández@ehdez73