spring - a java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... ·...

38
Spring / odrotbohm Oliver Drotbohm [email protected] A Java application framework

Upload: others

Post on 21-May-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

!Spring!/" odrotbohmOliver Drotbohm " [email protected]

A Java application framework

Page 2: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

! Script

2

" https://github.com/odrotbohm/lectures " http://static.olivergierke.de/lectures/spring

Page 3: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

# Guestbook Sample

3

" https://github.com/st-tu-dresden/guestbook

Page 4: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Goals of Frameworks

4

Page 5: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

1. Separation of concerns 2. Raising the abstraction level 3. Removal of boilerplate code

5

Page 6: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring Framework

6

Page 7: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

1. Dependency Injection 2. Portable service abstraction

7

Page 8: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Dependency Injection

8

Page 9: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

About constructing a net of objects…

9

Page 10: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

10

class MyService {

void someBusinessMethod() { !// MyRepository.save(…) ? } }

class MyRepository {

MyEntity save(MyEntity entity) { … } }

How do we establish

this relationship?

Page 11: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

11

class MyService {

private final MyRepository repo;

void someBusinessMethod() { repo.save(new MyEntity()); } }

class MyRepository {

MyEntity save(MyEntity entity) { … } }

Page 12: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

12

class MyService {

private final MyRepository repo;

MyService() { … !// ? }

void someBusinessMethod() { repo.save(new MyEntity()); } }

class MyRepository {

MyEntity save(MyEntity entity) { … } }

How do we obtain a

repository instance?

Page 13: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

13

class MyService {

private final MyRepository repo;

MyService(MyRepository repo) { this.repo = repo }

void someBusinessMethod() { repo.save(new MyEntity()); } }

class MyRepository {

MyEntity save(MyEntity entity) { … } }

Page 14: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

14

class MyService {

private final MyRepository repo;

MyService(MyRepository repo) { this.repo = repo }

void someBusinessMethod() { repo.save(new MyEntity()); } }

interface MyRepository { MyEntity save(MyEntity entity); }

class JpaRepository implements MyRepository {

@Override MyEntity save(MyEntity entity) { … } }

Page 15: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

15

Page 16: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

16

Page 17: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Manual dependency injection 17

!// For production MyRepository repository = new JpaRepository(); MyService service = new MyService(repository);

!// For tests MyRepository repository = new DummyRepository(); MyService service = new MyService(repository);

Different implementationsfor production and test!

Page 18: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Dependency Injection allows to select the actual implementationat construction time.

18

Page 19: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring-based Dependency Injection 19

@Component class MyService { MyService(MyRepository repository) { … } }

@Component class JpaRepository implements MyRepository { … }

!// For production ApplicationContext context = new AnnotationConfigApplicationContext(); MyService service = context.getBean(MyService.class);

Declare classes as framework components

Bootstrapframework

Page 20: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Portable Service Abstraction

20

Page 21: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Transactions Security

21

Page 22: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Transactions

22

Page 23: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Declarative transactions 23

class JpaRepository implements MyRepository {

@Override @Transactional MyEntity save(MyEntity entity) {

} }

The magic happens here!

Page 24: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Proxy creation 24

!// Wrapping a component into a transactional proxy

JpaRepository repository = new JpaRepository();

ProxyFactory factory = new ProxyFactory(repository); factory.addAdvice(new TransactionInterceptor(…));

MyRepository proxy = factory.getProxy(); MyService service = new MyService(proxy);

Page 25: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Runtime component setup 25

User code

Spring-provided

Page 26: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Proxy invocation flow 26

User code Spring-provided

Page 27: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC

27

Page 28: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC Controller 28

class MyController {

}

Page 29: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC Controller 29

@RestController class MyController {

}

Declares what

kind of class that is

Page 30: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC Controller 30

@RestController class MyController {

String sayHelloTo( ) { } }

Page 31: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC Controller 31

@RestController class MyController {

@GetMapping("/hello") String sayHelloTo(@RequestParam Optional<String> name) { } }

Which URI to map to?

Page 32: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC Controller 32

@RestController class MyController {

@GetMapping("/hello") String sayHelloTo(@RequestParam Optional<String> name) { } }

What parts of the request

are we interested in?

Page 33: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Spring MVC Controller 33

@RestController class MyController {

@GetMapping("/hello") String sayHelloTo(@RequestParam Optional<String> name) { return String.format("Hello, %s!", name.orElse("world")); } }

Page 34: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Using Frameworks

34

Page 35: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Reuse VS. Coupling

35

Page 36: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Java and the Enterprise

36

Page 37: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

1. Security of investment 2. Backwards compatibility 3. Availability of support

37

Page 38: Spring - A Java application frameworkstatic.olivergierke.de/lectures/spring/spring-a-java... · 2019-06-03 · Removal of boilerplate code 5. Spring Framework 6. 1. Dependency Injection

Thank you!

38

!/" odrotbohmOliver Drotbohm " [email protected]