Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Jürgen Höller on Spring 3.0

Jürgen Höller on Spring 3.0

More Decks by Enterprise Java User Group Austria

Other Decks in Technology

Transcript

  1. Spring Framework 3.0 In a Java EE 6 World Jürgen

    Höller VP & Distinguished Engineer SpringSource
  2. Quick Review: Spring 2.5 • Annotation-based component style – dependency

    injection: @Autowired • with optional @Qualifier or custom qualifier – middleware services: @Transactional – stereotypes: @Component, @Repository, @Controller • Common Java EE 5 annotations supported too – @PostConstruct, @PreDestroy, @Resource, etc • Component scanning in the classpath – as alternative to (minimal) XML bean definitions • Annotated web controllers (a.k.a. @MVC)
  3. Annotated Bean Component @Service public class RewardNetworkService implements RewardNetwork {

    @Autowired public RewardNetworkService(AccountRepository ar) { … } @Transactional public RewardConfirmation rewardAccountFor(Dining d) { … } }
  4. Test Context Framework @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class RewardSystemIntegrationTests { @Autowired

    private RewardNetwork rewardNetwork; @Test @Transactional public void testRewardAccountForDining() { // test in transaction with auto-rollback } }
  5. @MVC Controller Style @Controller public class MyController { private final

    MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } @RequestMapping("/removeBook") public String removeBook(@RequestParam("book") String bookId) { this.myService.deleteBook(bookId); return "redirect:myBooks"; } }
  6. Spring 3.0 Themes • Java 5+ foundation – even stronger

    support for annotated components • Spring Expression Language – Unified EL++ • Comprehensive REST support – and other Spring @MVC additions • Support for Portlet 2.0 – action/event/resource request mappings • Declarative model validation – Hibernate Validator, JSR-303 • Early support for Java EE 6 – JSF 2.0, JPA 2.0, etc
  7. Use of Meta-Annotations • More powerful options for custom annotations

    – combining meta-annotations e.g. on stereotype – automatically detected (no configuration necessary!) @Service @Scope("request") @Transactional(rollbackFor=Exception.class) @Retention(RetentionPolicy.RUNTIME) public @interface MyService {} @MyService public class RewardsService { … }
  8. Annotated Factory Methods • Spring 3.0 includes the core functionality

    of the Spring JavaConfig project – configuration classes defining managed beans – common handling of annotated factory methods @Bean @Primary @Lazy public RewardsService rewardsService() { RewardsServiceImpl service = new RewardsServiceImpl(); service.setDataSource(…); return service; }
  9. EL in Component Annotations @Repository public class RewardsTestDatabase { @Value(“#{systemProperties.databaseName}”)

    public void setDatabaseName(String dbName) { … } @Value(“#{strategyBean.databaseKeyGenerator}”) public void setKeyGenerator(KeyGenerator kg) { … } }
  10. EL Context Attributes • Example showed access to EL attributes

    – "systemProperties", "strategyBean" – implicit references in expressions • Implicit attributes to be exposed by default, depending on runtime context – e.g. "systemProperties", "systemEnvironment" • global platform context – access to all Spring-defined beans by name • similar to managed beans in JSF expressions – extensible through Scope SPI • e.g. for step scope in Spring Batch
  11. Web Context Attributes • Implicit web-specific attributes to be exposed

    by default as well – "contextParameters": web.xml init-params – "contextAttributes": ServletContext attributes – "request": current Servlet/PortletRequest – "session": current Http/PortletSession • Exposure of all implicit JSF objects when running within a JSF request context – "param", "initParam", "facesContext", etc – full compatibility with JSF managed bean facility
  12. REST Support • Spring MVC to provide first-class support for

    REST-style mappings – extraction of URI template parameters – content negotiation in view resolver • Goal: native REST support within Spring MVC, for UI as well as non-UI usage – in natural MVC style • Alternative: using JAX-RS through integrated JAX-RS provider (e.g. Jersey) – using the JAX-RS component model to build programmatic resource endpoints
  13. REST in MVC - @PathVariable @RequestMapping(value = "/rewards/{id}", method =

    GET) public Reward reward(@PathVariable("id") long id) { return this.rewardsAdminService.findReward(id); } http://rewarddining.com/rewards/12345
  14. Common @MVC Refinements • More options for handler method parameters

    – in addition to @RequestParam and @PathVariable – @RequestHeader: access to request headers – @CookieValue: HTTP cookie access – supported for Servlet MVC and Portlet MVC @RequestMapping("/show") public Reward show(@RequestHeader("region") long regionId, @CookieValue("language") String langId) { ... }
  15. Portlet 2.0 Support • Portlet 2.0: major new capabilities –

    explicit action name concept for dispatching – resource requests for servlet-style serving – events for inter-portlet communication – portlet filters analogous to servlet filters • Spring's Portlet MVC 3.0 to support explicit mapping annotations – @ActionMapping, @RenderMapping, @ResourceMapping, @EventMapping – specializations of Spring's @RequestMapping • supporting action names, window states, etc
  16. Spring Portlet MVC 3.0 @Controller @RequestMapping("EDIT") public class MyPortletController {

    … @ActionMapping("delete") public void removeBook(@RequestParam("book") String bookId) { this.myService.deleteBook(bookId); } @EventMapping("BookUpdate") public void updateBook(BookUpdateEvent bookUpdate) { // extract book entity data from event payload object this.myService.updateBook(…); } }
  17. Model Validation public class Reward { @NotNull @Past private Date

    transactionDate; } In view: <form:input path="transactionDate"> • Same metadata can be used for persisting, rendering, etc • Spring 3.0 RC1: to be supported for MVC data binding • JSR-303 "Bean Validation" as the common ground
  18. Spring 3.0 and Java EE 6 • Early Java EE

    6 API support in Spring 3.0 – integration with JSF 2.0 • full compatibility as managed bean facility – integration with JPA 2.0 • support for lock modes, query timeouts, etc – support for JSR-303 Bean Validation annotations • through Hibernate Validator 4.0 integration • All embeddable on Tomcat 5.5+ / J2EE 1.4+ – even WebSphere 6.1! – or OSGi-based deployment models • e.g. SpringSource dm Server
  19. Spring 3.x and Java EE 6 • Spring 3.1/3.2: support

    for Java EE 6 platforms – Servlet 3.0 • automatic deployment of framework adapters • async request support in Spring MVC • waiting for GlassFish 3 and Tomcat 7 – JSR-236 "Concurrency Utilities for Java EE" • standardized access to server-managed thread pools • Optimal use of full Java EE 6 platforms underneath the Spring programming model – automatic detection as far as possible – linking in Java EE 6 platform services through JNDI
  20. Spring 3.0 Summary • Spring 3.0 embraces REST and EL

    – full-scale REST support – broad Unified EL++ support in the core • Spring 3.0 significantly extends and refines annotated web controllers – RESTful URI mappings – annotation-based model validation • Spring 3.0 is a very good citizen on Java EE 5 and EE 6 platforms – making the best of standard EE APIs and conventions within the Spring programming & configuration model
  21. Spring & Runtime Platforms • Spring's central value proposition –

    providing a unified programming model for business components – providing consistent configuration across different runtime environments • Foundation: plain Java objects – transparently integrating with environment services through Spring's generic container – rich annotation support on Java 5
  22. Spring and Java EE • Java EE: standard system services

    – Spring abstractions can run on top of EE Java EE system services Java EE deployment and management Spring application container Spring service abstractions Application components
  23. Spring's Positioning • Spring is a distinctive, self-contained programming and

    configuration model – competes at the very heart of the component model • not just as yet another EJB/Web Beans container – own "Spring style": simple objects, clear lifecycle, middleware services on demand • "what you define is what you get" • Rich, agile component management – integrating with popular de-facto standards – bringing new functionality onto existing platforms • no need to update the web/application server!
  24. Spring and Standards • Spring embraces standards as "common ground"

    between different frameworks – Servlet API & Portlet API: server foundation that Spring's web support is based upon – JPA & JSF: persistence and web frameworks that Spring integrates with very closely (if desired) – JSR-250: common lifecycle annotations, simple injection annotations • fully supported for Spring-managed beans – JSR-303: common constraint annotations for domain model classes • fully supported for Spring data binding
  25. EJB 3.1 and Web Beans • Spring != EJB container

    in many respects – too many differences in registration, lifecycle behavior, concurrency and transaction defaults – Spring deliberately innovates through going a different path than EJB • What about JSR-299 a.k.a. Web Beans? – now "Contexts and Dependency Injection for Java EE" – defines rich dependency injection and scoping semantics, based on annotations – for Java EE components models such as Servlets and primarily EJB 3.1 Session Beans – similar to Spring, but closely tied to the EE mindset
  26. Emerging Standards • RFC-124 "OSGi Blueprint Service" – standardized XML

    bean definitions for OSGI bundles – based on Spring Dynamic Modules and Spring XML bean definitions – Spring DM 2.0 as reference implementation! • JSR-330 "Dependency Injection for Java" – new JSR defining common ground for any dependency injection container – led by Bob Lee (Guice) and Rod Johnson (SpringSource) – to be reused by JSR-299 (Web Beans)!
  27. Spring Platform Summary • Spring 3.0 runs on many common

    platforms – bringing modern component management even to existing J2EE 1.4 (!) platforms – as well as to OSGi platforms • Spring 3.0 supports common EE standards – e.g. JPA, JSF, JSR-303 "Bean Validation" – and of course running on top of full EE platforms • Spring 3.x to support emerging dependency injection standards as well – RFC-124 for components on OSGi – JSR-330 for components on Java SE and EE