Introducing spring

Post on 05-Aug-2015

116 views 2 download

Transcript of Introducing spring

Introducing Spring FrameworkIntroducing Spring Framework

Who am I ?

Developing Software

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

Kent Beck

Our toolbox

• Clean code

• TDD

• Design patters

• Frameworks

• ...

Clean code

Source code are for humans not computers!

“Code Smells”, Martin Fowler

TDD

Design Patterns

Frameworks

Choose your weapon !

Spring FrameworkSpring Framework

The Spring Framework

• Best practices

• Proven Solutions

• OpenSource

• Continuously growing

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...

In a Nutshell

• IoC container

• Lightweight

• Comprehensive coverage of AOP

• TDD

• Modular

Where?

• Junit system Test

• Java EE web application

• Java EE enterprise application

• Standalone Java application

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.

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

And that means...

Spring IoCSpring IoC

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.”

How?

Dep

enden

cy Inje

ctio

nSimpleObject

Asp

ect-Orien

ted P

rogram

min

g

Enterprise Service Abstractions

Spring Triangle

Production

Unit Test

Stub / Mock

Local development

H2

Deployments

Development Test QA Production

orMocks

+Dummy data

+Dump real data

+Real data

Dependency Injection (IoC)

• XML

• Annotations

• Java

TransferService

AccountRepository

XML

app-config.xml

And run it !

Annotations

XML

app-config.xml

And run it !

Java Configuration

Defined outside !!

Spring AOPSpring AOP

Aspect-Oriented Programming

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

Cross-Cutting Concerns?

Generic funcionality that is needed in many places in your application

− Security− Caching− Logging− ...

Tangling

Mixing of concerns

Scattering

Duplication

How?

ExceptionReporterAdvice(Aspect)

TransferServiceImpl (Target)

Spring AOP Proxy

Aspect Target

transfer(300,1,2)

AOP

<<Interface>>TransferService

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

What about ... ?

Web Access

HTTP / HTML

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, ...

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>

Spring MVCSpring MVC

Model - View - Controller

Spring MVC

DispatcherServlet Handler

View

model + View

render(model)JSP

@ControllerBlazeDS (Flex)

WebFlow...

JSPPDFExcel

...

request

response

HandlerMapping HandlerAdapter

HandlerMapping HandlerAdapter

ViewResolverViewResolver

Spring MVC

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

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)

Implement Controller

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>

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

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

… 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

SummarySummary

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.

Q&AQ&A

Ernesto Hernández@ehdez73