SlideShare a Scribd company logo
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/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/
Juergen Hoeller
Spring Framework Lead
Pivotal
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/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) {
...
}
}
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/3
Composable Annotations
@Service
@Scope("session")
@Primary
@Transactional(rollbackFor=Exception.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyService {}
@MyService
public class MyBookAdminService {
...
}
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/4
Composable Annotations with Overridable 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)
boolean readOnly();
}
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/5
Convenient Scoping Annotations (out of the box)
■ Web scoping annotations coming with Spring Framework 4.3
● @RequestScope
● @SessionScope
● @ApplicationScope
■ Special-purpose scoping annotations across the Spring portfolio
● @RefreshScope
● @JobScope
● @StepScope
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/6
The State of the Art: Configuration Classes
@Configuration
@Profile("standalone")
@EnableTransactionManagement
public class MyBookAdminConfig {
@Bean
public BookAdminService myBookAdminService() {
MyBookAdminService service = new MyBookAdminService();
service.setDataSource(bookAdminDataSource());
return service;
}
@Bean
public BookAdminService bookAdminDataSource() {
...
}
}
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/7
Configuration Classes with Autowired Bean Methods
@Configuration
@Profile("standalone")
@EnableTransactionManagement
public class MyBookAdminConfig {
@Bean
public BookAdminService myBookAdminService(
DataSource bookAdminDataSource) {
MyBookAdminService service = new MyBookAdminService();
service.setDataSource(bookAdminDataSource);
return service;
}
}
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/8
Configuration Classes with Autowired Constructors
@Configuration
public 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;
}
}
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/9
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;
}
}
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/10
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;
}
}
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/11
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, even with other MyRepository beans around
}
}
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/12
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: repository Y first, then X
}
}
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/13
Injection of Collection Beans
@Bean
public List<Account> myAccountList() { ... }
@Service
public 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
}
}
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/14
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
}
}
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/15
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>> repo) {
// 'repo' will be a lazy handle, allowing for .get() access
}
@PreDestroy
public void shutdown() {
...
}
}
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/16
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>> repo) {
if (repo.isPresent()) { ... }
}
@PreDestroy
public void shutdown() {
...
}
}
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/17
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;
...
}
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/18
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;
...
}
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/19
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() {
...
}
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/20
Annotated MVC Controllers
@RestController
@CrossOrigin
public class MyRestController {
@RequestMapping(path="/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);
}
}
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/21
Precomposed Annotations for MVC Controllers
@RestController
@CrossOrigin
public 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);
}
}
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/22
STOMP on WebSocket
@Controller
public class MyStompController {
@SubscribeMapping("/positions")
public List<PortfolioPosition> getPortfolios(Principal user) {
...
}
@MessageMapping("/trade")
public void executeTrade(Trade trade, Principal user) {
...
}
}
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/23
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) {
...
}
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/24
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) {
...
}
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/25
Declarative Cache Interaction
@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) {
}
}
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/26
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) {
}
}
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/27
Spring Framework 4.3 Roadmap
■ Last 4.x feature release!
■ 4.3 RC1: March 2016
■ 4.3 GA: May 2016
■ Extended support life until 2020
● on JDK 6, 7, 8 and 9
● on Tomcat 6, 7, 8 and 9
● on WebSphere 7, 8.0, 8.5 and 9
■ Spring Framework 5.0 coming in 2017
● on JDK 8+, Tomcat 7+, WebSphere 8.5+
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/28
Learn More. Stay Connected.
■ Current: Spring Framework 4.2.5
(February 2016)
■ Coming up: Spring Framework 4.3
(May 2016)
■ Check out Spring Boot!
http://projects.spring.io/spring-boot/
Twitter: twitter.com/springcentral
YouTube: spring.io/video
LinkedIn: spring.io/linkedin
Google Plus: spring.io/gplus

More Related Content

What's hot (20)

PPTX
Jsp and Servlets
Raghu nath
 
PDF
Spring 4 Web App
Rossen Stoyanchev
 
PPTX
Spring framework in depth
Vinay Kumar
 
PDF
Spring Framework - Core
Dzmitry Naskou
 
PDF
Spring Framework - MVC
Dzmitry Naskou
 
PPT
Spring MVC Basics
Bozhidar Bozhanov
 
PPT
J2EE - JSP-Servlet- Container - Components
Kaml Sah
 
ODP
springmvc-150923124312-lva1-app6892
Tuna Tore
 
ODP
Spring User Guide
Muthuselvam RS
 
PPT
Spring MVC
yuvalb
 
KEY
Multi Client Development with Spring
Joshua Long
 
PDF
SpringMVC
Akio Katayama
 
PPTX
Spring MVC framework
Mohit Gupta
 
PDF
Spring Framework - III
People Strategists
 
PDF
Weblogic
Raju Sagi
 
PPT
Spring MVC 3.0 Framework
Ravi Kant Soni ([email protected])
 
KEY
Multi client Development with Spring
Joshua Long
 
PPT
Spring 3.x - Spring MVC
Guy Nir
 
PDF
Design & Development of Web Applications using SpringMVC
Naresh Chintalcheru
 
PPT
JEE Course - The Web Tier
odedns
 
Jsp and Servlets
Raghu nath
 
Spring 4 Web App
Rossen Stoyanchev
 
Spring framework in depth
Vinay Kumar
 
Spring Framework - Core
Dzmitry Naskou
 
Spring Framework - MVC
Dzmitry Naskou
 
Spring MVC Basics
Bozhidar Bozhanov
 
J2EE - JSP-Servlet- Container - Components
Kaml Sah
 
springmvc-150923124312-lva1-app6892
Tuna Tore
 
Spring User Guide
Muthuselvam RS
 
Spring MVC
yuvalb
 
Multi Client Development with Spring
Joshua Long
 
SpringMVC
Akio Katayama
 
Spring MVC framework
Mohit Gupta
 
Spring Framework - III
People Strategists
 
Weblogic
Raju Sagi
 
Spring MVC 3.0 Framework
Ravi Kant Soni ([email protected])
 
Multi client Development with Spring
Joshua Long
 
Spring 3.x - Spring MVC
Guy Nir
 
Design & Development of Web Applications using SpringMVC
Naresh Chintalcheru
 
JEE Course - The Web Tier
odedns
 

Viewers also liked (20)

PDF
Voxxed berlin2016profilers|
Grzegorz Duda
 
PDF
Paolucci voxxed-days-berlin-2016-age-of-orchestration
Grzegorz Duda
 
PDF
Docker orchestration voxxed days berlin 2016
Grzegorz Duda
 
PDF
The internet of (lego) trains
Grzegorz Duda
 
PDF
Cassandra and materialized views
Grzegorz Duda
 
PDF
Advanced akka features
Grzegorz Duda
 
PDF
OrientDB - Voxxed Days Berlin 2016
Luigi Dell'Aquila
 
PDF
Size does matter - How to cut (micro-)services correctly
OPEN KNOWLEDGE GmbH
 
PDF
Rise of the Machines - Automate your Development
Sven Peters
 
PDF
Microservices with Java, Spring Boot and Spring Cloud
Eberhard Wolff
 
PPTX
House style - Ashley Tonks
nctcmedia12
 
PDF
Chapter6 36pages
Catherine McLean
 
PDF
Cambridge practice tests for ielts 6
Black Dragon
 
PPT
Redação científica i afonso
Giliander Allan da Silva
 
PDF
O ingilizce
Cemalettin Karpuzcu
 
PDF
Informe ciudad
kode99
 
PPTX
Organisation
nctcmedia12
 
PPTX
Injury Prevention Programs
University of Canberra
 
DOCX
العزو السببي لدى الفرق الرياضية لمدارسة تربية محافظة نينوى
Mukhalad Hamza
 
PDF
Tarifas x Planes
Carlos Díaz
 
Voxxed berlin2016profilers|
Grzegorz Duda
 
Paolucci voxxed-days-berlin-2016-age-of-orchestration
Grzegorz Duda
 
Docker orchestration voxxed days berlin 2016
Grzegorz Duda
 
The internet of (lego) trains
Grzegorz Duda
 
Cassandra and materialized views
Grzegorz Duda
 
Advanced akka features
Grzegorz Duda
 
OrientDB - Voxxed Days Berlin 2016
Luigi Dell'Aquila
 
Size does matter - How to cut (micro-)services correctly
OPEN KNOWLEDGE GmbH
 
Rise of the Machines - Automate your Development
Sven Peters
 
Microservices with Java, Spring Boot and Spring Cloud
Eberhard Wolff
 
House style - Ashley Tonks
nctcmedia12
 
Chapter6 36pages
Catherine McLean
 
Cambridge practice tests for ielts 6
Black Dragon
 
Redação científica i afonso
Giliander Allan da Silva
 
O ingilizce
Cemalettin Karpuzcu
 
Informe ciudad
kode99
 
Organisation
nctcmedia12
 
Injury Prevention Programs
University of Canberra
 
العزو السببي لدى الفرق الرياضية لمدارسة تربية محافظة نينوى
Mukhalad Hamza
 
Tarifas x Planes
Carlos Díaz
 
Ad

Similar to Spring 4.3-component-design (20)

PDF
Spring framework
Aircon Chen
 
PDF
Spring 3 MVC CodeMash 2009
kensipe
 
PPTX
Spring jdbc dao
Anuj Singh Rajput
 
PDF
Java persistence api 2.1
Rakesh K. Cherukuri
 
PPT
Spring, web service, web server, eclipse by a introduction sandesh sharma
Sandesh Sharma
 
PPTX
Spring Basics
ThirupathiReddy Vajjala
 
PDF
Spring db-access mod03
Guo Albert
 
PPT
Hybernat and structs, spring classes in mumbai
Vibrant Technologies & Computers
 
PDF
Advance Java Training in Bangalore | Best Java Training Institute
TIB Academy
 
PDF
ActiveJDBC - ActiveRecord implementation in Java
ipolevoy
 
PPTX
Building enterprise web applications with spring 3
Abdelmonaim Remani
 
PPTX
Get the Most out of Testing with Spring 4.2
Sam Brannen
 
PPTX
Spring framework part 2
Haroon Idrees
 
ODP
JavaEE Spring Seam
Carol McDonald
 
PPT
Spring overview
Dhaval Shah
 
PDF
Spring design-juergen-qcon
Yiwei Ma
 
PDF
Introduction to Spring Boot.pdf
ShaiAlmog1
 
PPT
Os Johnson
oscon2007
 
ODP
Jpa buenas practicas
fernellc
 
ODP
JPA Best Practices
Carol McDonald
 
Spring framework
Aircon Chen
 
Spring 3 MVC CodeMash 2009
kensipe
 
Spring jdbc dao
Anuj Singh Rajput
 
Java persistence api 2.1
Rakesh K. Cherukuri
 
Spring, web service, web server, eclipse by a introduction sandesh sharma
Sandesh Sharma
 
Spring db-access mod03
Guo Albert
 
Hybernat and structs, spring classes in mumbai
Vibrant Technologies & Computers
 
Advance Java Training in Bangalore | Best Java Training Institute
TIB Academy
 
ActiveJDBC - ActiveRecord implementation in Java
ipolevoy
 
Building enterprise web applications with spring 3
Abdelmonaim Remani
 
Get the Most out of Testing with Spring 4.2
Sam Brannen
 
Spring framework part 2
Haroon Idrees
 
JavaEE Spring Seam
Carol McDonald
 
Spring overview
Dhaval Shah
 
Spring design-juergen-qcon
Yiwei Ma
 
Introduction to Spring Boot.pdf
ShaiAlmog1
 
Os Johnson
oscon2007
 
Jpa buenas practicas
fernellc
 
JPA Best Practices
Carol McDonald
 
Ad

Recently uploaded (20)

PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Add Background Images to Charts in IBM SPSS Statistics Version 31.pdf
Version 1 Analytics
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 

Spring 4.3-component-design

  • 1. 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/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/ Juergen Hoeller Spring Framework Lead Pivotal
  • 2. 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/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-2016 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-2016 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(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(); }
  • 5. 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/5 Convenient Scoping Annotations (out of the box) ■ Web scoping annotations coming with Spring Framework 4.3 ● @RequestScope ● @SessionScope ● @ApplicationScope ■ Special-purpose scoping annotations across the Spring portfolio ● @RefreshScope ● @JobScope ● @StepScope
  • 6. 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/6 The State of the Art: Configuration Classes @Configuration @Profile("standalone") @EnableTransactionManagement public class MyBookAdminConfig { @Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } @Bean public BookAdminService bookAdminDataSource() { ... } }
  • 7. 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/7 Configuration Classes with Autowired Bean Methods @Configuration @Profile("standalone") @EnableTransactionManagement public class MyBookAdminConfig { @Bean public BookAdminService myBookAdminService( DataSource bookAdminDataSource) { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource); return service; } }
  • 8. 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/8 Configuration Classes with Autowired Constructors @Configuration public 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; } }
  • 9. 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/9 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; } }
  • 10. 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/10 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; } }
  • 11. 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/11 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, even with other MyRepository beans around } }
  • 12. 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/12 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: repository Y first, then X } }
  • 13. 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/13 Injection of Collection Beans @Bean public List<Account> myAccountList() { ... } @Service public 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 } }
  • 14. 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/14 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 } }
  • 15. 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/15 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>> repo) { // 'repo' will be a lazy handle, allowing for .get() access } @PreDestroy public void shutdown() { ... } }
  • 16. 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/16 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>> repo) { if (repo.isPresent()) { ... } } @PreDestroy public void shutdown() { ... } }
  • 17. 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/17 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; ... }
  • 18. 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/18 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; ... }
  • 19. 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/19 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() { ... }
  • 20. 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/20 Annotated MVC Controllers @RestController @CrossOrigin public class MyRestController { @RequestMapping(path="/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); } }
  • 21. 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/21 Precomposed Annotations for MVC Controllers @RestController @CrossOrigin public 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); } }
  • 22. 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/22 STOMP on WebSocket @Controller public class MyStompController { @SubscribeMapping("/positions") public List<PortfolioPosition> getPortfolios(Principal user) { ... } @MessageMapping("/trade") public void executeTrade(Trade trade, Principal user) { ... } }
  • 23. 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/23 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) { ... }
  • 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/24 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) { ... }
  • 25. 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/25 Declarative Cache Interaction @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) { } }
  • 26. 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/26 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) { } }
  • 27. 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/27 Spring Framework 4.3 Roadmap ■ Last 4.x feature release! ■ 4.3 RC1: March 2016 ■ 4.3 GA: May 2016 ■ Extended support life until 2020 ● on JDK 6, 7, 8 and 9 ● on Tomcat 6, 7, 8 and 9 ● on WebSphere 7, 8.0, 8.5 and 9 ■ Spring Framework 5.0 coming in 2017 ● on JDK 8+, Tomcat 7+, WebSphere 8.5+
  • 28. 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/28 Learn More. Stay Connected. ■ Current: Spring Framework 4.2.5 (February 2016) ■ Coming up: Spring Framework 4.3 (May 2016) ■ Check out Spring Boot! http://projects.spring.io/spring-boot/ Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus