modern java component design with spring framework 4.3

24
Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution- NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Modern Java Component Design with Spring Framework 4.3 Juergen Hoeller @springjuergen

Upload: spring-io

Post on 07-Jan-2017

2.048 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Modern Java Component Designwith Spring Framework 4.3

Juergen Hoeller@springjuergen

Page 2: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

The State of the Art: Component Classes

2

@Service@Lazypublic class MyBookAdminService implements BookAdminService {

// @Autowired public MyBookAdminService(AccountRepository repo) { ... }

@Transactional public BookUpdate updateBook(Addendum addendum) { ... }}

Page 3: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Composable Annotations

3

@Service@Scope("session")@Primary@Transactional(rollbackFor=Exception.class)@Retention(RetentionPolicy.RUNTIME)public @interface MyService {}

@MyServicepublic class MyBookAdminService { ...}

Page 4: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Composable Annotations with Overridable Attributes

4

@Scope(scopeName="session")@Retention(RetentionPolicy.RUNTIME)public @interface MySessionScoped {

@AliasFor(annotation=Scope.class, attribute="proxyMode") ScopedProxyMode mode() default ScopedProxyMode.NO;}

@Transactional(rollbackFor=Exception.class)@Retention(RetentionPolicy.RUNTIME)public @interface MyTransactional {

@AliasFor(annotation=Transactional.class) boolean readOnly();}

Page 5: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Convenient Scoping Annotations (out of the box)

5

Web (as of 4.3)

@RequestScope @SessionScope @ApplicationScope

Elsewhere

@RefreshScope @JobScope @StepScope

Page 6: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

The State of the Art: Configuration Classes

6

@Configuration@Profile("standalone")@EnableTransactionManagementpublic class MyBookAdminConfig {

@Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; }

@Bean public DataSource bookAdminDataSource() { ... }}

Page 7: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Configuration Classes with Autowired Bean Methods

7

@Configuration@Profile("standalone")@EnableTransactionManagementpublic class MyBookAdminConfig {

@Bean public BookAdminService myBookAdminService( DataSource bookAdminDataSource) {

MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource); return service; }}

Page 8: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Configuration Classes with Autowired Constructors

8

@Configurationpublic class MyBookAdminConfig {

private final DataSource bookAdminDataSource;

// @Autowired public MyBookAdminService(DataSource bookAdminDataSource) { this.bookAdminDataSource = bookAdminDataSource; }

@Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(this.bookAdminDataSource); return service; }}

Page 9: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Configuration Classes with Base Classes

9

@Configurationpublic class MyApplicationConfig extends MyBookAdminConfig {

...}

public class MyBookAdminConfig {

@Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; }}

Page 10: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Configuration Classes with Java 8 Default Methods

10

@Configurationpublic class MyApplicationConfig implements MyBookAdminConfig {

...}

public interface MyBookAdminConfig {

@Bean default BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; }}

Page 11: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Generics-based Injection Matching

11

@Beanpublic MyRepository<Account> myAccountRepository() { ... }

@Beanpublic MyRepository<Product> myProductRepository() { ... }

@Servicepublic class MyBookAdminService implements BookAdminService {

@Autowired public MyBookAdminService(MyRepository<Account> repo) { // specific match, even with other MyRepository beans around }}

Page 12: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Ordered Collection Injection

12

@Bean @Order(2)public MyRepository<Account> myAccountRepositoryX() { ... }

@Bean @Order(1)public MyRepository<Account> myAccountRepositoryY() { ... }

@Servicepublic class MyBookAdminService implements BookAdminService {

@Autowired public MyBookAdminService(List<MyRepository<Account>> repos) { // 'repos' List with 2 entries: repository Y first, then X }}

Page 13: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Injection of Collection Beans

13

@Beanpublic List<Account> myAccountList() { ... }

@Servicepublic class MyBookAdminService implements BookAdminService {

@Autowired public MyBookAdminService(List<Account> repos) { // if no raw Account beans found, looking for a // bean which is a List of Account itself }}

Page 14: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Lazy Injection Points

14

@Bean @Lazypublic MyRepository<Account> myAccountRepository() { return new MyAccountRepositoryImpl();}

@Servicepublic class MyBookAdminService implements BookAdminService {

@Autowired public MyBookAdminService(@Lazy MyRepository<Account> repo) { // 'repo' will be a lazy-initializing proxy }}

Page 15: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Component Declarations with JSR-250 & JSR-330

15

import javax.annotation.*;import javax.inject.*;

@ManagedBeanpublic class MyBookAdminService implements BookAdminService {

@Inject public MyBookAdminService(Provider<MyRepository<Account>> repo){ // 'repo' will be a lazy handle, allowing for .get() access }

@PreDestroy public void shutdown() { ... }}

Page 16: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Optional Injection Points on Java 8

16

import java.util.*;import javax.annotation.*;import javax.inject.*;

@ManagedBeanpublic class MyBookAdminService implements BookAdminService {

@Inject public MyBookAdminService(Optional<MyRepository<Account>> repo){ if (repo.isPresent()) { ... } }

@PreDestroy public void shutdown() { ... }}

Page 17: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Annotated MVC Controllers

17

@Controller@CrossOriginpublic class MyRestController {

@RequestMapping(path="/books/{id}", method=GET) public Book findBook(@PathVariable long id) { return this.bookAdminService.findBook(id); }

@RequestMapping(path="/books/new", method=POST) public void newBook(@Valid Book book) { this.bookAdminService.storeBook(book); }}

Page 18: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Precomposed Annotations for MVC Controllers

18

@RestController@CrossOriginpublic class MyRestController {

@GetMapping("/books/{id}") public Book findBook(@PathVariable long id) { return this.bookAdminService.findBook(id); }

@PostMapping("/books/new") public void newBook(@Valid Book book) { this.bookAdminService.storeBook(book); }}

Page 19: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

STOMP on WebSocket

19

@Controllerpublic class MyStompController {

@SubscribeMapping("/positions") public List<PortfolioPosition> getPortfolios(Principal user) { ... }

@MessageMapping("/trade") public void executeTrade(Trade trade, Principal user) { ... }}

Page 20: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Annotated JMS Endpoints

20

@JmsListener(destination="order")public OrderStatus processOrder(Order order) { ...}

@JmsListener(id="orderListener", containerFactory="myJmsFactory", destination="order", selector="type='sell'", concurrency="2-10")@SendTo("orderStatus")public OrderStatus processOrder(Order order, @Header String type) { ...}

Page 21: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Annotated Event Listeners

21

@EventListenerpublic void processEvent(MyApplicationEvent event) { ...}

@EventListenerpublic void processEvent(String payload) { ...}

@EventListener(condition="#payload.startsWith('OK')")public void processEvent(String payload) { ...}

Page 22: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Declarative Cache Interaction

22

@CacheConfig("books")public class BookRepository {

@Cacheable(sync=true) public Book findById(String id) { }

@CachePut(key="#book.id") public void updateBook(Book book) { }

@CacheEvict public void delete(String id) { }}

Page 23: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

JCache (JSR-107) Support

23

import javax.cache.annotation.*;

@CacheDefaults(cacheName="books")public class BookRepository {

@CacheResult public Book findById(String id) { }

@CachePut public void updateBook(String id, @CacheValue Book book) { }

@CacheRemove public void delete(String id) { }}

Page 24: Modern Java Component Design with Spring Framework 4.3

Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Learn More. Stay Connected.

Current: Spring Framework 4.3.2 (July 28th, 2016)

Check out Spring Boot 1.4, based on Spring Framework 4.3!

@springcentralspring.io/blog

@pivotalpivotal.io/blog

@pivotalcfhttp://engineering.pivotal.io

@piv