This document summarizes new features in Spring 3 and 3.1 for component-based application design. Spring 3 focuses on annotation-based components while also supporting concise XML configurations. Key features include stereotypes, factory methods, expression language support, standardized annotations, validation, formatting, scheduling, and REST support. Spring 3.1 enhances environments with profiles for bean definitions, enables Java-based configuration, adds a "c:" namespace for XML, and introduces declarative caching capabilities.
2. Spring 3.0 – Component Model Revisited
Powerful annotated component model
• stereotypes, factory methods, JSR-330
Spring Expression Language
• Unified EL++
Comprehensive REST support
• and other Spring @MVC additions
Support for Portlet 2.0 request types
• action/render/event/resource mappings
Declarative validation and formatting
• integration with JSR-303 Bean Validation
Scheduling enhancements
• scheduling annotation with cron support
CONFIDENTIAL 2
3. Enhanced Stereotype Model
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 BookAdminService {
…
}
CONFIDENTIAL 3
4. Annotated Factory Methods
Spring 3.0 includes the core functionality of the former
Spring JavaConfig project
• configuration classes defining managed beans
• common handling of annotated factory methods
@Bean @Primary @Lazy
public BookAdminService bookAdminService() {
MyBookAdminService service = new MyBookAdminService();
service.setDataSource(…);
return service;
}
CONFIDENTIAL 4
5. Standardized Annotations
@ManagedBean
public class MyBookAdminService implements BookAdminService {
@Inject
public MyBookAdminService(AccountRepository ar) {
…
}
@TransactionAttribute
public BookUpdate updateBook(Addendum addendum) {
…
}
}
CONFIDENTIAL 5
6. JSR-330 and Co
@javax.inject.Inject is part of JSR-330
• "Dependency Injection for Java"
• also defines @Qualifier semantics
• and a Provider interface
@javax.annotation.ManagedBean is part of JSR-250 v1.1
• driven by the Java EE 6 specification
• can be detected through classpath scanning
@javax.ejb.TransactionAttribute is part of the EJB 3.0/3.1
specification
• also supported for Spring beans
CONFIDENTIAL 6
7. EL in XML Bean Definitions
<bean class="mycompany.BookTestDatabase">
<property name="databaseName"
value="#{systemProperties.databaseName}"/>
<property name="keyGenerator"
value="#{strategyBean.databaseKeyGenerator}"/>
</bean>
CONFIDENTIAL 7
8. EL in Component Annotations
@Repository
public class BookTestDatabase {
@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String dbName) { … }
@Value("#{strategyBean.databaseKeyGenerator}")
public void setKeyGenerator(KeyGenerator kg) { … }
}
CONFIDENTIAL 8
9. REST in MVC - @PathVariable
http://mybookstore.com/books/12345
@RequestMapping(value = "/books/{id}", method = GET)
public Book findBook(@PathVariable("id") long id) {
return this.bookAdminService.findBook(id);
}
CONFIDENTIAL 9
10. Portlet 2.0 Support in MVC
@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(…);
}
}
CONFIDENTIAL 10
11. Declarative Model Validation
public class Book {
@NotNull
@Past
private Date releaseDate;
}
@RequestMapping("/books/new")
public void newBook(@Valid Book book) { … }
JSR-303 "Bean Validation" as the common ground
Spring 3.0 fully supports JSR-303 for MVC data binding
Same metadata can be used for persisting, rendering, etc
CONFIDENTIAL 11
12. Conversion and Formatting
Spring 3.0 features a revised binding and type conversion
infrastructure
• stateless Java 5+ type converters and formatters
• annotation-driven number/date formatting
public class Book {
@NotNull
@Past
@DateTimeFormat(iso=ISO.DATE)
private Date releaseDate;
}
CONFIDENTIAL 12
13. Scheduling Enhancements
Spring 3.0 introduces a major overhaul of the scheduling
package
• TaskScheduler interface with Trigger abstraction
• XML scheduling namespace with cron support
• @Async annotation for asynchronous user methods
• @Scheduled annotation for cron-triggered methods
@Scheduled(cron = "0 0 12 * * ?")
public void performTempFileCleanup() {
...
}
CONFIDENTIAL 13
14. Related Java EE 6 Specifications
Java EE 6 API support in Spring Framework 3.0
• several specifications adopted into the Spring component model
Integration with JPA 2.0
• support for query builder, native delegates, etc
Integration with JSF 2.0
• full compatibility as a managed bean facility
JSR-303 Bean Validation integration
• through Hibernate Validator 4.1
JSR-330: common dependency injection annotations
• natively supported by Spring itself
CONFIDENTIAL 14
15. Spring 3.1 – Component Model Enhancements
Environment profiles for bean definitions
Java-based application configuration
"c:" namespace for XML configuration
Declarative caching
CONFIDENTIAL 15
17. Environment Configuration
Environment association of specific bean definitions
• XML 'profile' attribute on <beans> element
• @Profile annotation on configuration classes
• @Profile annotation on individual component classes
Activating specific profiles by name
• e.g. through a system property
• -Dspring.profiles.active=development
• or other means outside of the deployment unit
• according to environment conventions
Ideally: no need to touch deployment unit across different
stages/environments
CONFIDENTIAL 17
18. Java-Based Application Configuration
@FeatureConfiguration
@Import(DataConfig.class)
public class TxFeatures {
@Feature
public TxAnnotationDriven tx(DataConfig dataConfig) {
return new TxAnnotationDriven(dataConfig.txManager()).proxyTargetClass(true);
}
}
<tx:annotation-driven transaction-manager="txManager"
proxy-target-class="true"/>
@Configuration
public class DataConfig {
@Bean
public PlatformTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
// ... configure and return JDBC DataSource ...
}
}
CONFIDENTIAL 18
19. "c:" Namespace for XML Configuration
New XML namespace for use with bean configuration
• shortcut for <constructor-arg>
• inline argument values
• analogous to existing "p:" namespace
• use of constructor argument names
• recommended for readability
• debug symbols have to be available in the application's class files
<bean class="…" c:age="10" c:name="myName"/>
<bean class="…" c:name-ref="nameBean"
c:spouse-ref="spouseBean"/>
CONFIDENTIAL 19
20. Declarative Caching
@Cacheable
public Owner loadOwner(int id);
@Cacheable(condition="name.length < 10")
public Owner loadOwner(String name);
@CacheEvict
public void deleteOwner(int id);
CONFIDENTIAL 20
21. Summary
Spring 3 provides plenty of features for modern component design
• focus on annotation-based components
• also investing into concise XML-based bean definitions
Selected core Spring 3.0 features
• stereotypes, factory methods, EL
• support for standardized annotations
• declarative validation and formatting
Selected enhancements in Spring 3.1
• environment profiles for bean definitions
• Java-based application configuration
• declarative caching
CONFIDENTIAL 21