How to Troubleshoot Apps for the Modern Connected Worker
Β
Lo nuevo en Spring 3.0
1. Spring Framework 3.0
The Next Generation
JΓΌrgen HΓΆller
VP & Distinguished Engineer
SpringSource
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
2. Agenda
β’ Quick Review: Spring 2.5
β’ Spring 3.0 Themes and Features
β’ Spring 3.0 Roadmap
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2
3. Spring Framework 2.5
β’ Comprehensive support for
annotation-based configuration
β @Autowired
β’ with optional @Qualifier or custom qualifier
β @Transactional
β @Service, @Repository, @Controller
β’ Common Java EE 5 annotations supported too
β @PostConstruct, @PreDestroy
β @PersistenceContext, @PersistenceUnit
β @Resource, @EJB, @WebServiceRef
β @TransactionAttribute
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3
4. Annotated Bean Component
@Service
public class RewardNetworkService
implements RewardNetwork {
@Autowired
public RewardNetworkService(AccountRepository ar) {
β¦
}
@Transactional
public RewardConfirmation rewardAccountFor(Dining d) {
β¦
}
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4
5. Minimal XML Bean Definitions
<!β Activating annotation-based configuration -->
<context:annotation-config/>
<bean class=βcom.myapp.rewards.RewardNetworkImplβ/>
<bean class=βcom.myapp.rewards.JdbcAccountRepositoryβ/>
<!β Plus shared infrastructure configuration beans:
PlatformTransactionManager, DataSource, etc -->
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5
6. 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
}
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6
7. @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";
}
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7
8. 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
β’ Declarative model validation
β Hibernate Validator, JSR 303
β’ Support for Portlet 2.0
β action/event/resource request mappings
β’ Early support for Java EE 6
β JSF 2.0, JPA 2.0, etc
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8
9. New Project Layout
β’ Framework modules revised
β now managed in Maven style
β one source tree per module jar
β’ spring-beans.jar, spring-aop.jar, etc
β no spring.jar anymore!
β’ Built with new Spring build system as known
from Spring Web Flow 2.0
β Ivy-based "Spring Build" system
β consistent deployment procedure
β consistent dependency management
β consistent generation of OSGi manifests
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9
10. Core APIs updated for Java 5
β’ BeanFactory interface returns typed bean
instances as far as possible
β T getBean(String name, Class<T> requiredType)
β Map<String, T> getBeansOfType(Class<T> type)
β’ Spring's TaskExecutor interface extends
java.util.concurrent.Executor now
β extended AsyncTaskExecutor supports standard
Callables with Futures
β’ Typed ApplicationListener<E>
β ApplicationEventMulticaster detects declared event
type and filters accordingly
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10
11. Portfolio Rearrangements
β’ Spring 3.0 includes a revised version of the
Object/XML Mapping (OXM) module
β known from Spring Web Services
β supported for REST-style payload conversion
β also useful e.g. for SQL XML access
β’ Spring 3.0 features a revised binding and
type conversion infrastructure
β including the capabilities of Spring Web Flow's binding
β stateless Java 5+ type converters with EL integration
β superseding standard JDK PropertyEditors
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11
12. 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;
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12
13. 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 {
β¦
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13
14. EL in Bean Definitions
<bean class="mycompany.RewardsTestDatabase">
<property name="databaseName"
value="β#{systemProperties.databaseName}β/>
<property name="keyGenerator"
value="β#{strategyBean.databaseKeyGenerator}β/>
</bean>
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14
15. EL in Component Annotations
@Repository
public class RewardsTestDatabase {
@Value(β#{systemProperties.databaseName}β)
public void setDatabaseName(String dbName) { β¦ }
@Value(β#{strategyBean.databaseKeyGenerator}β)
public void setKeyGenerator(KeyGenerator kg) { β¦ }
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15
16. Powerful Spring EL Parser
β’ Custom expression parser implementation
shipped as part of Spring 3.0
β package org.springframework.expression
β next-generation expression engine
β’ inspired by Spring Web Flow 2.0's expression support
β pluggable and customizable
β’ Compatible with Unified EL but significantly
more powerful
β navigating bean properties, maps, etc
β method invocations
β construction of value objects
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16
17. 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
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17
18. 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
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18
19. 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
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19
20. REST in MVC - @PathVariable
http://rewarddining.com/rewards/12345
@RequestMapping(value = "/rewards/{id}", method = GET)
public Reward reward(@PathVariable("id") long id) {
return this.rewardsAdminService.findReward(id);
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20
21. Different Representations
β’ JSON
GET http://rewarddining.com/accounts/1 accepts application/json
GET http://rewarddining.com/accounts/1.json
β’ XML
GET http://rewarddining.com/accounts/1 accepts application/xml
GET http://rewarddining.com/accounts/1.xml
β’ ATOM
GET http://rewarddining.com/accounts/1 accepts application/atom+xml
GET http://rewarddining.com/accounts/1.atom
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21
22. 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) {
...
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22
23. 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
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23
24. 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(β¦);
}
}
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24
25. 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
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25
26. Scoped Bean Serializability
β’ Problem with Spring 2.5: serializability of
session and conversation objects
β when referencing shared service objects via Spring
dependency injection
β’ and not marking them as transient
β typical situation: scoped bean instance holds on to
DataSource or the like
β’ DataSource reference is not serializable
β’ -> whole bean not serializable
β’ Solution: proxies that reobtain references
on deserialization
β from current Spring WebApplicationContext
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26
27. Scheduling Enhancements
β’ Spring 3.0 introduces a major overhaul of
the scheduling package
β extended java.util.concurrent support
β’ prepared for JSR-236 "Concurrency Utilities for Java EE"
β TaskScheduler interface with Triggers
β’ including cron expression triggers
β @Async annotation for asynchronous user methods
β’ @Scheduled for cron-triggered methods
β’ XML scheduling namespace
β cron expressions with method references
β convenient executor service setup
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27
28. 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+
β’ Spring 3.x: support for Java EE 6 platforms
β Servlet 3.0 (waiting for GlassFish 3 and Tomcat 7)
β JSR-236 "Concurrency Utilities for Java EE"
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28
29. Spring 3.0 Platform Support
β’ Requires Java 5 or above
β Java 6 automatically detected & supported
β’ Web container: Servlet 2.4 or above
β e.g. Tomcat 5.5 & 6.0
β’ Java EE level: J2EE 1.4 or above
β e.g. WebSphere 6.1 & 7.0
β’ Fully OSGi compatible out of the box
β featured in dm Server 2.0
β’ Also compatible with Google App Engine
β and other 'special' hosting environments
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29
30. Pruning & Deprecation in 3.0
β’ Some pruning: removal of outdated features
β Commons Attributes support
β’ superseded by Java 5 annotations
β traditional TopLink API support
β’ in favor of JPA (EclipseLink)
β subclass-style Struts 1.x support
β’ Some deprecation: superseded features
β traditional MVC form controller hierarchy
β’ superseded by annotated controller style
β traditional JUnit 3.8 test class hierarchy
β’ superseded by test context framework
β several outdated helper classes
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30
31. Spring 2.5 Mission Continued
β’ Spring 3 continues Spring 2.5's mission
β fully embracing Java 5 in the core Spring
programming and configuration model
β now with even the core framework requiring Java 5
β’ all framework classes using Java 5 language syntax
β’ Backwards compatibility with Spring 2.5
β 100% compatibility of programming model
β 95% compatibility of extension points
β all previously deprecated API to be removed
β’ Make sure you're not using outdated Spring 1.2 / 2.0
API anymore!
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 31
32. 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 remains backwards compatible
with Spring 2.5 on Java 5+
β enabling a smooth migration path
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 32
33. Spring 3.0 Roadmap
β’ Spring Framework 3.0 M3 to be released
any day now
β last milestone before RC1
β’ Spring Framework 3.0 RC1 scheduled for
late May 2009
β GA to follow soon after
β’ Spring Framework 3.1 expected in
Q4 2009
β full compatibility with Java EE 6 platforms
β first-class support for conversation management
Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 33
34. Copyright 2009 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 34