What’s New in
Spring 3.0
Sam Brannen
Senior Software Consultant
Spring Geek Night
Zürich Switzerland
25 February 2010
Speaker Profile
Senior Software Consultant – Swiftmind GmbH
Spring Framework Core Developer
Previous SpringSource dm Server™ developer: OSGi-enabled
Web deployment models, Tomcat integration, Test Framework
Java developer with 12+ years' experience
Regular speaker at conferences on Spring, dm Server, Java,
OSGi, and testing
Co-author of Spring in a Nutshell;
chief technical reviewer for Spring Recipes
2Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Agenda
Review of Spring 2.0 & 2.5
Spring 3.0 Releases
Deprecations in Spring 3.0
What’s New in Spring 3.0
Spring 3.0 Examples
Q&A
3Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 2.0
4Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 2.0 Review
Java 5, Bean scopes, JPA, Message-driven POJOs
XML Namespaces: beans, p, aop, tx, jee, lang, util
Annotations: @AspectJ, @Transactional, @Required,
@PersistenceContext, @PersistenceUnit
SimpleJdbcTemplate, NamedParameterJdbcTemplate
Web:
JSP form tag library
Convention over configuration
Dynamic language support: JRuby, Groovy, Beanshell
Testing: AbstractJpaTests and annotations
5Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 2.5
6Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 2.5 Review
Annotation-driven configuration & component scanning
@Autowired, @Qualifier
JSR-250: @Resource, @PostConstruct, @PreDestroy
@Component, @Service, @Repository, @Controller
XML Namespaces: context, jms
Spring Web @MVC: @RequestMapping, @RequestParam,
@ModelAttribute, @InitBinder, @SessionAttributes
Spring TestContext Framework
JUnit 3.8, JUnit 4.4, TestNG 5.5
@ContextConfiguration, @TestExecutionListeners,
@BeforeTransaction, @AfterTransaction, …
7Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0
8Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.M1
Java 5+ and J2EE 1.4+
Java 5 language features: generics and var-args
Spring EL parser
Support for #{…} expressions in bean definitions
@PathVariable for URI template handling
@RequestParam defaultValue attribute
@RequestHeader for HTTP header attribute handling
AbstractAtomFeedView & AbstractRssFeedView
<spring:url /> & <spring:param /> JSP tags
9Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.M2
Further Java 5 updates
Generic Collections and Maps
Generified FactoryBean
Generified ApplicationListener
Generified callback interfaces: TransactionCallback,
HibernateCallback, …
Concurrency and TaskExecutor abstraction
java.util.concurrent integration
JSR 236: “Concurrency Utilities for Java EE 6”
@Async & EJB 3.1’s @Asynchronous
OXM module moved from Spring-WS to core
Portlet 2.0 and early JPA 2.0 support
10Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.M3
Distributed with reference guide
Annotated Factory methods and JavaConfig in core
@Bean, @Lazy, @Primary
@Configuration, @Scope
Mix and match with XML configuration
Meta-annotations a la @TransactionalService
Extended TaskExecutor abstraction
ScheduledExecutorServices, CommonJ TimerManager
Trigger API and CronTrigger implementation
Type conversion subsystem from SWF
Java 5 based, stateless, and replaces PropertyEditors
11Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.M4
XML Namespaces: task, jdbc
BeanWrapper and DefaultListableBeanFactory accept a custom
ConversionService
Scoped proxies are serializable for web scopes and singleton
beans
@Primary, @Lazy, and @DependsOn supported as meta-
annotations
@Transactional supports qualifier value for choosing
transaction manager
Upgraded to JUnit 4.6* and TestNG 5.9; @DirtiesContext
supported at class level
12Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.RC1
@Value supports dynamic #{…} expressions or static
${…} placeholders
JSR 330: “Dependency Injection for Java”
@Inject, @Qualifier, @Named, …
JSR 303: “Bean Validation” and Hibernate Validator
@Valid, @NotNull, @Min, @Max, @Size, …
Formatting subsystem and annotation support
PropertyPlaceholderConfigurer supports
"${key:defaultValue}" defaulting syntax
Upgraded to JUnit 4.7* and TestNG 5.10
13Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.RC2
XML Namespaces: mvc
Fully compliant with the JSR 330 TCK
AnnotationConfigApplicationContext and
AnnotationConfigWebApplicationContext
Revised lifecycle management and LifecycleProcessor
Additional enhancements and bug fixes
14Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.0.RELEASE
Released December 16, 2009
No major new features
Updated JPA 2.0 support to final spec
Full support for Glassfish V3
Bug fixes
Documentation and Reference Manual
15Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.1.RELEASE
Released February 18, 2010
3.0.1.RELEASE-A for OSGi compatibility
Support for Hibernate 3.5, Tiles 2.2.1, and further support for
JPA 2.0
spring:eval tag for evaluating SpEL expressions in JSPs
Further vararg support in BeanDefinitionReader,
ClassPathXmlApplicationContext,
FileSystemXmlApplicationContext, JdbcTemplate
Various minor bug fixes
16Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0.2.RELEASE
Scheduled for March 11, 2010
Last release in the 3.0.x branch
Upgraded to Hibernate Validator 4.0.2 and Jackson 1.4.2
No new features
Minor bug fixes
17Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0 – Deprecations
PetClinic now in spring-samples repository
Commons attributes support
Traditional TopLink API
Struts 1.x
SimpleFormController class hierarchy
JUnit 3.8 legacy test class hierarchy
@NotTransactional
18Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring Expression Language
19Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
What is SpEL?
EL for querying and manipulating an object graph at runtime
Similar to Unified EL syntax but with more features
Single, well supported EL across the Spring Portfolio
Supports:
Literal expressions
Boolean expressions and relational operators
Regular expressions
Class expressions
Templated expressions
20Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
SpEL Features (continued)
Accessing properties, arrays, lists, maps
Method and constructor invocation
Assignment
Ternary operator
Variables
User-defined functions
Collection projection and selection
Implicit variables: systemProperties, systemEnvironment,
request, session, …
21Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
SpEL Programmatic Usage
ExpressionParser parser = new
SpelExpressionParser();
Expression exp =
parser.parseExpression("'Hello World'");
assertEquals("Hello World", exp.getValue());
exp = parser.parseExpression(
"new String('hello world').toUpperCase()");
assertEquals("HELLO WORLD",
exp.getValue(String.class));
22Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
SpEL with Context
class Foo { String getBar() { return "bar”; }}
Foo foo = new Foo();
EvaluationContext context = new
StandardEvaluationContext(foo);
ExpressionParser parser = new
SpelExpressionParser();
Expression exp =
parser.parseExpression("bar");
assertEquals("bar”, exp.getValue(context));
23Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
SpEL with Bean Definitions
24Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
SpEL and @Value
25Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
SpEL Expressions
26Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0 MVC
27Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
@RequestParam Default Value
@RequestMapping("/category")
public ModelAndView displayCategory(
@RequestParam(value="id", defaultValue="1") Long id){
return new ModelAndView(
"category")
.addObject(service.findById(id));
}
Example URL: http://example.com/category?id=9
28Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
@PathVariable
@RequestMapping("/category/{id}”)
public ModelAndView displayCategory(
@PathVariable("id") Long id) {
return new ModelAndView(
"category")
.addObject(service.findById(id));
}
Example URL: http://example.com/category/9
29Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
@RequestHeader
@RequestMapping("/view")
public ModelAndView view(
@RequestHeader("accept-language") String lang) {
// do something with lang …
}
30Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
@CookieValue
@RequestMapping("/view")
public ModelAndView view(
@CookieValue("lang") String lang) {
// do something with lang …
}
31Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Before <mvc /> Namespace
32Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
After <mvc /> Namespace
<mvc:annotation-driven conversion-service="conversionService"
validator="validator" />
<bean id="conversionService"
class="org…FormattingConversionService" />
<bean id="validator" class="org…LocalValidatorFactoryBean" />
Or simply…
<mvc:annotation-driven />
33Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
The jdbc Namespace
34Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Embedded Database in XML
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:/my-schema.sql" />
<jdbc:script location="classpath:/my-data.sql" />
</jdbc:embedded-database>
Or simply…
<jdbc:embedded-database id="dataSource" />
35Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Embedded Database in Code
36Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Populate Database in XML
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:/schema_01.sql" />
<jdbc:script location="classpath:/schema_02.sql" />
<jdbc:script location="classpath:/data_01.sql" />
<jdbc:script location="classpath:/data_02.sql" />
</jdbc:initialize-database>
37Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
The task Namespace
38Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Scheduling Tasks in XML
<task:scheduled-tasks scheduler="taskScheduler">
<task:scheduled ref="worker" method="work" fixed-rate="1000" />
<task:scheduled ref="worker" method="work" fixed-delay="3000" />
<task:scheduled ref="worker" method="work"
cron="*/4 * 9-17 * * MON-FRI" />
</task:scheduled-tasks>
<task:scheduler id=”taskScheduler" />
<bean id="worker" class="org.example.Worker" />
39Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Annotation-driven Scheduling
40Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Spring 3.0 Configuration
41Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Java-based Configuration
42Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
JSR 303 Validation
43Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Declarative Bean Validation
44Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
public class AppointmentForm {
@NotNull
private Long doctor;
@NotNull
private Long client;
@NotNull
private Long patient;
@NotNull
private LocalDate date = new LocalDate();
@NotNull
private LocalTime time = new LocalTime();
// …
Bean Validation & Spring MVC
45Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment,
BindingResult result) {
if (result.hasErrors()) {
return "appointments/new";
}
appointmentBook.addAppointment(appointment);
return "redirect:/appointments”;
}
Spring Formatting
46Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Formatter API
47Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Formatter Implementations
48Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
NumberFormatter: A general-purpose Number formatter
CurrencyFormatter: BigDecimal formatter for currency values
DateFormatter: A formatter for Date types
NumberFormatAnnotationFormatterFactory: supports fields
annotated with @NumberFormat
DateTimeFormatAnnotationFormatterFactory: supports fields
annotated with @DateTimeFormat (Joda Time)
Formatting & Spring MVC
49Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
@RequestMapping(value="/{day}”, method=RequestMethod.GET)
public String getForDay(@PathVariable
@DateTimeFormat(iso=ISO.DATE) LocalDate day,
Model model)
{
DoctorAppointments appointments =
appointmentBook.getAppointmentsForDay(day);
model.addAttribute("doctorAppointmentsMap",
appointments.asMap());
return "appointments";
}
Summary
50Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Summary
Java 5 language features
Spring Expression Language
New task, jdbc, and mvc namespaces
REST support
New @MVC annotations
Java-based configuration
JSR 303 Bean Validation
JSR 330 DI for Java
Formatting and Conversion systems
51Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Further Resources
Spring Framework:
http://springframework.org
Spring Forums:
http://forum.springframework.org
Spring JIRA:
http://jira.springframework.org
SpringSource Team Blog:
http://blog.springsource.com
Swiftmind GmbH
http://www.swiftmind.com
“Spring in a Nutshell”
available from O’Reilly in Spring of 2010
52Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
Q&A
Sam Brannen
sam.brannen [at] swiftmind [dot] com
http://www.swiftmind.com
http://twitter.com/sam_brannen
53Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.

What's New in Spring 3.0

  • 1.
    What’s New in Spring3.0 Sam Brannen Senior Software Consultant Spring Geek Night Zürich Switzerland 25 February 2010
  • 2.
    Speaker Profile Senior SoftwareConsultant – Swiftmind GmbH Spring Framework Core Developer Previous SpringSource dm Server™ developer: OSGi-enabled Web deployment models, Tomcat integration, Test Framework Java developer with 12+ years' experience Regular speaker at conferences on Spring, dm Server, Java, OSGi, and testing Co-author of Spring in a Nutshell; chief technical reviewer for Spring Recipes 2Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 3.
    Agenda Review of Spring2.0 & 2.5 Spring 3.0 Releases Deprecations in Spring 3.0 What’s New in Spring 3.0 Spring 3.0 Examples Q&A 3Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 4.
    Spring 2.0 4Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 5.
    Spring 2.0 Review Java5, Bean scopes, JPA, Message-driven POJOs XML Namespaces: beans, p, aop, tx, jee, lang, util Annotations: @AspectJ, @Transactional, @Required, @PersistenceContext, @PersistenceUnit SimpleJdbcTemplate, NamedParameterJdbcTemplate Web: JSP form tag library Convention over configuration Dynamic language support: JRuby, Groovy, Beanshell Testing: AbstractJpaTests and annotations 5Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 6.
    Spring 2.5 6Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 7.
    Spring 2.5 Review Annotation-drivenconfiguration & component scanning @Autowired, @Qualifier JSR-250: @Resource, @PostConstruct, @PreDestroy @Component, @Service, @Repository, @Controller XML Namespaces: context, jms Spring Web @MVC: @RequestMapping, @RequestParam, @ModelAttribute, @InitBinder, @SessionAttributes Spring TestContext Framework JUnit 3.8, JUnit 4.4, TestNG 5.5 @ContextConfiguration, @TestExecutionListeners, @BeforeTransaction, @AfterTransaction, … 7Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 8.
    Spring 3.0 8Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 9.
    Spring 3.0.0.M1 Java 5+and J2EE 1.4+ Java 5 language features: generics and var-args Spring EL parser Support for #{…} expressions in bean definitions @PathVariable for URI template handling @RequestParam defaultValue attribute @RequestHeader for HTTP header attribute handling AbstractAtomFeedView & AbstractRssFeedView <spring:url /> & <spring:param /> JSP tags 9Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 10.
    Spring 3.0.0.M2 Further Java5 updates Generic Collections and Maps Generified FactoryBean Generified ApplicationListener Generified callback interfaces: TransactionCallback, HibernateCallback, … Concurrency and TaskExecutor abstraction java.util.concurrent integration JSR 236: “Concurrency Utilities for Java EE 6” @Async & EJB 3.1’s @Asynchronous OXM module moved from Spring-WS to core Portlet 2.0 and early JPA 2.0 support 10Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 11.
    Spring 3.0.0.M3 Distributed withreference guide Annotated Factory methods and JavaConfig in core @Bean, @Lazy, @Primary @Configuration, @Scope Mix and match with XML configuration Meta-annotations a la @TransactionalService Extended TaskExecutor abstraction ScheduledExecutorServices, CommonJ TimerManager Trigger API and CronTrigger implementation Type conversion subsystem from SWF Java 5 based, stateless, and replaces PropertyEditors 11Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 12.
    Spring 3.0.0.M4 XML Namespaces:task, jdbc BeanWrapper and DefaultListableBeanFactory accept a custom ConversionService Scoped proxies are serializable for web scopes and singleton beans @Primary, @Lazy, and @DependsOn supported as meta- annotations @Transactional supports qualifier value for choosing transaction manager Upgraded to JUnit 4.6* and TestNG 5.9; @DirtiesContext supported at class level 12Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 13.
    Spring 3.0.0.RC1 @Value supportsdynamic #{…} expressions or static ${…} placeholders JSR 330: “Dependency Injection for Java” @Inject, @Qualifier, @Named, … JSR 303: “Bean Validation” and Hibernate Validator @Valid, @NotNull, @Min, @Max, @Size, … Formatting subsystem and annotation support PropertyPlaceholderConfigurer supports "${key:defaultValue}" defaulting syntax Upgraded to JUnit 4.7* and TestNG 5.10 13Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 14.
    Spring 3.0.0.RC2 XML Namespaces:mvc Fully compliant with the JSR 330 TCK AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext Revised lifecycle management and LifecycleProcessor Additional enhancements and bug fixes 14Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 15.
    Spring 3.0.0.RELEASE Released December16, 2009 No major new features Updated JPA 2.0 support to final spec Full support for Glassfish V3 Bug fixes Documentation and Reference Manual 15Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 16.
    Spring 3.0.1.RELEASE Released February18, 2010 3.0.1.RELEASE-A for OSGi compatibility Support for Hibernate 3.5, Tiles 2.2.1, and further support for JPA 2.0 spring:eval tag for evaluating SpEL expressions in JSPs Further vararg support in BeanDefinitionReader, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, JdbcTemplate Various minor bug fixes 16Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 17.
    Spring 3.0.2.RELEASE Scheduled forMarch 11, 2010 Last release in the 3.0.x branch Upgraded to Hibernate Validator 4.0.2 and Jackson 1.4.2 No new features Minor bug fixes 17Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 18.
    Spring 3.0 –Deprecations PetClinic now in spring-samples repository Commons attributes support Traditional TopLink API Struts 1.x SimpleFormController class hierarchy JUnit 3.8 legacy test class hierarchy @NotTransactional 18Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 19.
    Spring Expression Language 19Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 20.
    What is SpEL? ELfor querying and manipulating an object graph at runtime Similar to Unified EL syntax but with more features Single, well supported EL across the Spring Portfolio Supports: Literal expressions Boolean expressions and relational operators Regular expressions Class expressions Templated expressions 20Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 21.
    SpEL Features (continued) Accessingproperties, arrays, lists, maps Method and constructor invocation Assignment Ternary operator Variables User-defined functions Collection projection and selection Implicit variables: systemProperties, systemEnvironment, request, session, … 21Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 22.
    SpEL Programmatic Usage ExpressionParserparser = new SpelExpressionParser(); Expression exp = parser.parseExpression("'Hello World'"); assertEquals("Hello World", exp.getValue()); exp = parser.parseExpression( "new String('hello world').toUpperCase()"); assertEquals("HELLO WORLD", exp.getValue(String.class)); 22Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 23.
    SpEL with Context classFoo { String getBar() { return "bar”; }} Foo foo = new Foo(); EvaluationContext context = new StandardEvaluationContext(foo); ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("bar"); assertEquals("bar”, exp.getValue(context)); 23Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 24.
    SpEL with BeanDefinitions 24Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 25.
    SpEL and @Value 25Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 26.
    SpEL Expressions 26Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 27.
    Spring 3.0 MVC 27Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 28.
    @RequestParam Default Value @RequestMapping("/category") publicModelAndView displayCategory( @RequestParam(value="id", defaultValue="1") Long id){ return new ModelAndView( "category") .addObject(service.findById(id)); } Example URL: http://example.com/category?id=9 28Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 29.
    @PathVariable @RequestMapping("/category/{id}”) public ModelAndView displayCategory( @PathVariable("id")Long id) { return new ModelAndView( "category") .addObject(service.findById(id)); } Example URL: http://example.com/category/9 29Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 30.
    @RequestHeader @RequestMapping("/view") public ModelAndView view( @RequestHeader("accept-language")String lang) { // do something with lang … } 30Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 31.
    @CookieValue @RequestMapping("/view") public ModelAndView view( @CookieValue("lang")String lang) { // do something with lang … } 31Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 32.
    Before <mvc />Namespace 32Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 33.
    After <mvc />Namespace <mvc:annotation-driven conversion-service="conversionService" validator="validator" /> <bean id="conversionService" class="org…FormattingConversionService" /> <bean id="validator" class="org…LocalValidatorFactoryBean" /> Or simply… <mvc:annotation-driven /> 33Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 34.
    The jdbc Namespace 34Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 35.
    Embedded Database inXML <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:/my-schema.sql" /> <jdbc:script location="classpath:/my-data.sql" /> </jdbc:embedded-database> Or simply… <jdbc:embedded-database id="dataSource" /> 35Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 36.
    Embedded Database inCode 36Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 37.
    Populate Database inXML <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="classpath:/schema_01.sql" /> <jdbc:script location="classpath:/schema_02.sql" /> <jdbc:script location="classpath:/data_01.sql" /> <jdbc:script location="classpath:/data_02.sql" /> </jdbc:initialize-database> 37Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 38.
    The task Namespace 38Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 39.
    Scheduling Tasks inXML <task:scheduled-tasks scheduler="taskScheduler"> <task:scheduled ref="worker" method="work" fixed-rate="1000" /> <task:scheduled ref="worker" method="work" fixed-delay="3000" /> <task:scheduled ref="worker" method="work" cron="*/4 * 9-17 * * MON-FRI" /> </task:scheduled-tasks> <task:scheduler id=”taskScheduler" /> <bean id="worker" class="org.example.Worker" /> 39Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 40.
    Annotation-driven Scheduling 40Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 41.
    Spring 3.0 Configuration 41Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 42.
    Java-based Configuration 42Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 43.
    JSR 303 Validation 43Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 44.
    Declarative Bean Validation 44Copyright2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited. public class AppointmentForm { @NotNull private Long doctor; @NotNull private Long client; @NotNull private Long patient; @NotNull private LocalDate date = new LocalDate(); @NotNull private LocalTime time = new LocalTime(); // …
  • 45.
    Bean Validation &Spring MVC 45Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited. @RequestMapping(method = RequestMethod.POST) public String add(@Valid AppointmentForm appointment, BindingResult result) { if (result.hasErrors()) { return "appointments/new"; } appointmentBook.addAppointment(appointment); return "redirect:/appointments”; }
  • 46.
    Spring Formatting 46Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 47.
    Formatter API 47Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 48.
    Formatter Implementations 48Copyright 2009-2010Sam Brannen. Copying, publishing or distributing without express written permission is prohibited. NumberFormatter: A general-purpose Number formatter CurrencyFormatter: BigDecimal formatter for currency values DateFormatter: A formatter for Date types NumberFormatAnnotationFormatterFactory: supports fields annotated with @NumberFormat DateTimeFormatAnnotationFormatterFactory: supports fields annotated with @DateTimeFormat (Joda Time)
  • 49.
    Formatting & SpringMVC 49Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited. @RequestMapping(value="/{day}”, method=RequestMethod.GET) public String getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) LocalDate day, Model model) { DoctorAppointments appointments = appointmentBook.getAppointmentsForDay(day); model.addAttribute("doctorAppointmentsMap", appointments.asMap()); return "appointments"; }
  • 50.
    Summary 50Copyright 2009-2010 SamBrannen. Copying, publishing or distributing without express written permission is prohibited.
  • 51.
    Summary Java 5 languagefeatures Spring Expression Language New task, jdbc, and mvc namespaces REST support New @MVC annotations Java-based configuration JSR 303 Bean Validation JSR 330 DI for Java Formatting and Conversion systems 51Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 52.
    Further Resources Spring Framework: http://springframework.org SpringForums: http://forum.springframework.org Spring JIRA: http://jira.springframework.org SpringSource Team Blog: http://blog.springsource.com Swiftmind GmbH http://www.swiftmind.com “Spring in a Nutshell” available from O’Reilly in Spring of 2010 52Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.
  • 53.
    Q&A Sam Brannen sam.brannen [at]swiftmind [dot] com http://www.swiftmind.com http://twitter.com/sam_brannen 53Copyright 2009-2010 Sam Brannen. Copying, publishing or distributing without express written permission is prohibited.