Modern Java Component Design with Spring Framework 4.2

33 %
67 %
Information about Modern Java Component Design with Spring Framework 4.2

Published on September 29, 2015

Author: SpringCentral

Source: slideshare.net

1. Modern Java Component Design with Spring Framework 4.2 By Juergen Hoeller & Stéphane Nicoll @springjuergen / @snicoll

2. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2 The State of the Art: Component Classes @Service @Lazy public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(AccountRepository repo) { ... } @Transactional public BookUpdate updateBook(Addendum addendum) { ... } }

3. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 3 Composable Annotations @Service @Scope("session") @Primary @Transactional(rollbackFor=Exception.class) @Retention(RetentionPolicy.RUNTIME) public @interface MyService {} @MyService public class MyBookAdminService { ... }

4. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4 Composable Annotations with Overridable Attributes @Scope("session") @Retention(RetentionPolicy.RUNTIME) public @interface MySessionScoped { ScopedProxyMode proxyMode() default ScopedProxyMode.NO; } @Transactional(rollbackFor=Exception.class) @Retention(RetentionPolicy.RUNTIME) public @interface MyTransactional { boolean readOnly(); }

5. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 5 Composable Annotations with Declared Attributes @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, attribute="readOnly") boolean readOnly(); }

6. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 6 The State of the Art: Configuration Classes @Configuration @Profile("standalone") @EnableTransactionManagement public class MyBookAdminConfig { @Bean @Scope("session") public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } ... }

7. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7 Configuration Classes with Base Classes @Configuration public class MyApplicationConfig extends MyBookAdminConfig { ... } public class MyBookAdminConfig { @Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } }

8. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 8 Configuration Classes with Java 8 Default Methods @Configuration public class MyApplicationConfig implements MyBookAdminConfig { ... } public interface MyBookAdminConfig { @Bean default BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } }

9. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 9 Ordered Configuration Classes @Configuration @Order(2) public class MyApplicationConfig { @Bean public SpecialBookAdminService myBookAdminService() { ... } } @Configuration @Order(1) public class MyBookAdminConfig { @Bean public BookAdminService myBookAdminService() { ... } }

10. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 10 Generics-based Injection Matching @Bean public MyRepository<Account> myAccountRepository() { ... } @Bean public MyRepository<Product> myProductRepository() { ... } @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(MyRepository<Account> repo) { // specific match, despite other MyRepository beans around } }

11. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 11 Ordered Collection Injection @Bean @Order(2) public MyRepository<Account> myAccountRepositoryX() { ... } @Bean @Order(1) public MyRepository<Account> myAccountRepositoryY() { ... } @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(List<MyRepository<Account>> repos) { // 'repos' List with two entries: Y first, then X } }

12. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 12 Lazy Injection Points @Bean @Lazy public MyRepository<Account> myAccountRepository() { return new MyAccountRepositoryImpl(); } @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(@Lazy MyRepository<Account> repo) { // 'repo' will be a lazy-initializing proxy } }

13. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 13 Component Declarations with JSR-250 & JSR-330 import javax.annotation.*; import javax.inject.*; @ManagedBean public class MyBookAdminService implements BookAdminService { @Inject public MyBookAdminService(Provider<MyRepository<Account>> r) { // 'r' will be a lazy handle, allowing for .get() access } @PreDestroy public void shutdown() { ... } }

14. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14 Optional Injection Points on Java 8 import java.util.*; import javax.annotation.*; import javax.inject.*; @ManagedBean public class MyBookAdminService implements BookAdminService { @Inject public MyBookAdminService(Optional<MyRepository<Account>> r) { if (r.isPresent()) { ... } } @PreDestroy public void shutdown() { … } }

15. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 15 Declarative Formatting with Java 8 Date-Time import java.time.*; import javax.validation.constraints.*; import org.springframework.format.annotation.*; public class Customer { // @DateTimeFormat(iso=ISO.DATE) private LocalDate birthDate; @DateTimeFormat(pattern="M/d/yy h:mm") @NotNull @Past private LocalDateTime lastContact; ... }

16. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 16 Declarative Formatting with JSR-354 Money & Currency import javax.money.*; import org.springframework.format.annotation.*; public class Product { private MonetaryAmount basePrice; @NumberFormat(pattern="¤¤ #000.000#") private MonetaryAmount netPrice; private CurrencyUnit originalCurrency; ... }

17. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 17 Declarative Scheduling (with two Java 8 twists) @Async public Future<Integer> sendEmailNotifications() { return new AsyncResult<Integer>(...); } @Async public CompletableFuture<Integer> sendEmailNotifications() { return CompletableFuture.completedFuture(...); } @Scheduled(cron="0 0 12 * * ?") @Scheduled(cron="0 0 18 * * ?") public void performTempFileCleanup() { ... }

18. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 18 Annotated MVC Controllers @RestController @CrossOrigin public class MyRestController { @RequestMapping(value="/books/{id}", method=GET) public Book findBook(@PathVariable long id) { return this.bookAdminService.findBook(id); } @RequestMapping("/books/new") public void newBook(@Valid Book book) { this.bookAdminService.storeBook(book); } }

19. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 19 STOMP on WebSocket @Controller public class MyStompController { @SubscribeMapping("/positions") public List<PortfolioPosition> getPortfolios(Principal user) { ... } @MessageMapping("/trade") public void executeTrade(Trade trade, Principal user) { ... } }

20. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 20 Annotated JMS Endpoints @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) { ... }

21. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 21 Annotated Event Listeners @EventListener public void processEvent(MyApplicationEvent event) { ... } @EventListener public void processEvent(String payload) { ... } @EventListener(condition="#payload.startsWith('OK')") public void processEvent(String payload) { ... }

22. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 22 Declarative Cache Interaction @CacheConfig("books") public class BookRepository { @Cacheable public Book findById(String id) { } @CachePut(key="#book.id") public void updateBook(Book book) { } @CacheEvict public void delete(String id) { } }

23. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 23 JCache (JSR-107) Support 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) { } }

24. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 24 Learn More. Stay Connected. Check out... “Get the Most out of Testing with Spring 4.2” (Tue 10:30) “Spring MVC 4.2: New and Noteworthy” (Wed 10:30) @springcentral Spring.io/video

Add a comment

Related pages

Modern Java Component Design with Spring Framework 4

Modern Java Component Design with Spring Framework 4.2 ... Java 8 Lambdas with Spring APIs ... Spring Framework 4.2
Read more

Modern Java Component Design with Spring Framework 4.2

Core Spring Track. Spring's programming and configuration model has a strong design philosophy with respect to application components and configuration ...
Read more

Modern Java Component Design with Spring 4.2 | Spring I/O 2015

Modern Java Component Design with Spring 4.2; ... with a focus on the upcoming Spring Framework 4.2 and a selection of Java 8 enabled features, ...
Read more

SpringOne: Modern Java Component Design with Spring 4.2 ...

... Modern Java Component Design with Spring 4.2. SpringOne: ... For more information about these annotations and others in the Spring framework, ...
Read more

KTG-5194 Modern Java Component Design with Spring 4.2 ...

Modern Java Component Design with Spring 4.2 All Conferences ... with a focus on the upcoming Spring Framework 4.2 and a selection of Java 8 enabled ...
Read more

Modern Java Component Design with Spring 4.2 at Spring I/O ...

Modern Java Component Design with Spring 4.2 ... Spring Framework 4 component model highlights, with a focus on the upcoming Spring Framework 4.2 and a ...
Read more

Spring Framework 4.2 goes GA

... announce that Spring Framework 4.2 is now ... general Spring 4 system requirements (Java ... on modern Java component design with Spring 4.2.
Read more

Spring 4 Tutorial - Java Programming Tutorials

... new and announcement about the Spring 4 framework. Spring ... version is Spring 4.2. ... Java Component Design with Spring Framework 4 ...
Read more

Spring Framework 4.2 goes RC1

Dear Spring community, It’s my pleasure to announce that Spring Framework 4.2 RC1 is ... modern web capabilities ... modern Java component design for ...
Read more

Spring Framework - Wikipedia, the free encyclopedia

... current latest version Spring Framework 4.2 ... components and lifecycle management of Java ... Java Development with the Spring Framework ...
Read more