Your SlideShare is downloading. ×
  • Like
Spring design-juergen-qcon
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Spring design-juergen-qcon

  • 668 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
668
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
16
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Modern Component Design with Spring 3Jürgen Höller, Principal Engineer, SpringSource CONFIDENTIAL © 2010 SpringSource, A division of VMware. All rights reserved
  • 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 {}@MyServicepublic 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 @Lazypublic BookAdminService bookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(…); return service;} CONFIDENTIAL 4
  • 5. Standardized Annotations@ManagedBeanpublic 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@Repositorypublic 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 Validationpublic 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 formattingpublic 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
  • 16. Environment Profiles<beans profile="production"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClass" value="${database.driver}"/> <property name="jdbcUrl" value="${database.url}"/> <property name="username" value="${database.username}"/> <property name="password" value="${database.password}"/> </bean></beans><beans profile="embedded"> <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="/WEB-INF/database/schema-member.sql"/> <jdbc:script location="/WEB-INF/database/schema-activity.sql"/> <jdbc:script location="/WEB-INF/database/schema-event.sql"/> <jdbc:script location="/WEB-INF/database/data.sql"/> </jdbc:embedded-database></beans> CONFIDENTIAL 16
  • 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"/>@Configurationpublic 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 applications 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@Cacheablepublic Owner loadOwner(int id);@Cacheable(condition="name.length < 10")public Owner loadOwner(String name);@CacheEvictpublic 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
  • 22. 杭州站 · 2011年10月20日~22日 www.qconhangzhou.com(6月启动)QCon北京站官方网站和资料下载 www.qconbeijing.com