Top Banner
Lightweight Enterprise Java Development using Spring Framework Oleksiy Rezchykov Eugene Scripnik
73

Lightweight J2EE development with Spring (special for UADEV)

May 10, 2015

Download

Technology

springbyexample

Our presentation for UADev club on the "Spring va J2EE" topic.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lightweight J2EE development with Spring (special for UADEV)

Lightweight Enterprise Java Development

using Spring Framework

Oleksiy RezchykovEugene Scripnik

Page 2: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

2

About us Software Engineers

Working with Spring since 2006

Pragmatic programmers

SpringByExample.com.ua founders

Page 3: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

3

Contents Spring origins

IoC using Spring

Persistence with Spring

Declarative caching using @Cachable

Web applications with Spring MVC

Spring ecosystem

Framework criticism

Page 4: Lightweight J2EE development with Spring (special for UADEV)

4

J2EE 5 Development

SpringByExample.com.ua @ua_spring #uadevclub

Page 5: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

5

SpringFramework history

In October 2002 Rod Johnson wrote his famous book

The first milestone release 1.0 - June 2004

Company renamed from Interface21 to SpringSource in 2007

SpringSource acquired by VMWare in 2009

Rod Johnson left VMWare – July 2012

Page 6: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

6

Inversion of Control Inversion of regular approach where object

was responsible for satisfying it’s own dependencies

Implementation of Dependency Injection

Container instantiates and wires dependencies

Page 7: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

7

Injection types Setter injection– when you are using setter to

fulfill bean dependency

Constructor injection – when constructor is called to set-up a bean with dependencies

Method injection

Page 8: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

8

Context An object which contains beans declarations

with their dependencies

BeanFactory interface implementation

Page 9: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

9

XML namespaces

Page 10: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

10

Context

Page 11: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

11

Context usage

Page 12: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

12

Bean lifecycle Depends on scope and lazy initialization

Main bean scopes:

Singleton and Prototype

(Also there are multiple web scopes)

Page 13: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

13

Bean instantiation order

Page 14: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

14

Bean Post-processingBea

n instantiation

• Bean instantiated for singletons with eager initialization

Dependency

injection

• Dependencies are injected into beans

Bean

post-

processing

• Bean post-processing actions• You can put your custom logic

in here

Page 15: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

15

Map, List, Set, Prop, Null

Page 16: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

16

Map, List, Set, Prop, Null

Page 17: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

17

Util namespace

Page 18: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

18

Component model

Page 19: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

19

@Autowired Spring proprietary annotation for property

injection

Supports @Qualifier to select implementation by name if more than one exists in context

Has required parameter to enforce dependency loading

Page 20: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

20

@Autowired

Page 21: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

21

@Inject Context and Dependency Injection (CDI)

standard JSR-299

Page 22: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

22

@Resource JSR 250 (Common annotations)

Supports @PostConstruct and @PreDestroy as well

Page 23: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

23

Resource loading and i18n

Page 24: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

24

Resource loading and i18n

Page 25: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

25

SpEl Stands for Spring Expression Language

Can be used in the xml context

Can be used in bean classes

Page 26: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

26

SpEl

Page 27: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

27

Java Spring Config @Configuration

@Bean

Support for Environment and Profiles

Page 28: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

28

Java Spring Config

Page 29: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

29

Environment abstraction & context

profiles Environment abstraction introduced in Spring 3.1

Can be declaratively and programmatically configured

Affects property resolution

Affects bean profiles

Page 30: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

30

Environment abstraction & context

profiles

Page 31: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

31

AOPA programming paradigm which allows to separate cross-cutting concerns

Cross-cutting logic:

Logging

Tracing

Security

Page 32: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

32

AOP Aspect

Advice

Join point

Pointcut

Page 33: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

33

AOP

Page 34: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

34

Spring AOP Spring AOP defaults to using standard J2SE

dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

Spring AOP can also use CGLIB proxies. This is necessary to proxy classes, rather than interfaces

It is good practice to program to interfaces rather than classes, business classes normally will implement one or more business interfaces.

Page 35: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

35

AspectJ Spring AOP is often uses the AspectJ

declaration style

AspectJ itself uses Load Time Weaving (LTW) to modify the bean code according to Aspect logic

LTW could do much more than dynamic proxies but it is more time-consuming operation. This means that LTW has a performance drawback.

Page 36: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

36

“pure” AspectJ

Page 37: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

37

“pure” AspectJ

Page 38: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

38

Persistence with Spring

Building DAO

Spring Data

Embedded Datasources

JDBC vs ORM DAO’s

Integration testing with Spring

@Transactional & transactions with Spring

Page 39: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

39

Spring supports JDBC

JPA

JDO

Concrete ORM

Page 40: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

40

Spring Data RDBMS: JPA, JDBC Extensions

BigData: ApacheHadoop

DataGrid: GermFire

HTTP: REST

Key-value stores: Redis

Document Stores: MongoDB

Graph Databases: Neo4j

Column stores: Hbase

Page 41: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

41

Persistence layer

Service

• Persistence service which has domain specific logic

Repository (DAO)

• Spring JDBCTemplate• SessionFactory, EntityManagerFactory• Redis/Mongo template

DB

• RDBMS (or NoSQL)

Page 42: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

42

JDBCTemplate Useful interface

No boilerplate code

Exception handling

Page 43: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

43

JDBCTemplate

Page 44: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

44

JDBC namespace

Page 45: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

45

Spring Data JPA CRUD

Page 46: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

46

Spring Data JPA Query DSL

Page 47: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

47

Transactions in Spring

Support for ACID transactions

Declarative and programmatic

Page 48: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

48

Transaction managers

Spring support both container managed and framework managed transactions

Managers:

DataSourceTransactionManager

HibernateTransactionManager

JPATransactionManager

JTATransactionManager

Page 49: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

49

@Transactional Transaction propagation

Read-only transactions

Built using Spring AOP

Page 50: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

50

Persistence layer testing

Context loading in test

Environment abstraction and profiles usage

Transactional methods

Embedded data source could be used

Page 51: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

51

Persistence layer testing

Page 52: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

52

@Cacheable One-two-three magic (not always work as

expected)

Support for JCache (JSR-107)

Page 53: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

53

@Cacheable <cache:annotation-driven> or

@EnableCaching

CacheManager instance in the context

@Cacheable, @CachePut, @CacheEvict

Page 54: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

54

Spring MVC Application context

Dispatcher Servlet and it’s context

Controllers and mappings

Implementing CRUD logic

REST

Testing MVC

Page 55: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

55

Application & Dispatcher servlet

context

Page 56: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

56

Front controller and MVC

Page 57: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

57

URL mappings @RequestMapping

Page 58: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

58

View resolvers AbstractCachingViewResolver

XmlViewResolver

ResourceBundleViewResolver

UrlBasedViewResolver

InternalResourceViewResolver

VelocityViewResolver / FreeMarkerViewResolver

ContentNegotiatingViewResolver

Page 59: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

59

View resolvers

Page 60: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

60

CRUD in one place

Page 61: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

61

REST in Spring MVC Starting Spring 3.1.x

ContentNegotiatingViewResolver

@ResponseBody combined with produces

@RequestBody combined with consumes

Page 62: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

62

REST in Spring MVC

Page 63: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

63

Testing MVC Starting from Spring 3.2

spring-test-mvc project

Server-side Spring MVC tests

New Spring Mocks

Different strategies

Page 64: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

64

Testing MVC

Page 65: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

65

Testing MVC

Page 66: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

66

Just to mention Type conversion

Formatter SPI

Page 67: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

67

Just to mention Type conversion

Formatter SPI

Page 68: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

68

Just to mention Flash attributes and Redirect attributes

Page 69: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

69

Spring in 2012

Page 70: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

70

Just to mention Spring Roo

Spring Hadoop

Spring Android

Page 71: Lightweight J2EE development with Spring (special for UADEV)

SpringByExample.com.ua @ua_spring #uadevclub

71

Criticism Every tool should be used only if you could

not solve your task without it – KISS

When a lot of stuff come out-of-the-box it is not always good - YAGNI

When something is broken it is pain in the …

Open source but VMWare