• Like
  • Save
Spring design-juergen-qcon
Upcoming SlideShare
Loading in...5
×
 

Spring design-juergen-qcon

on

  • 970 views

 

Statistics

Views

Total Views
970
Views on SlideShare
970
Embed Views
0

Actions

Likes
0
Downloads
13
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Spring design-juergen-qcon Spring design-juergen-qcon Presentation Transcript

    • Modern Component Design with Spring 3Jürgen Höller, Principal Engineer, SpringSource CONFIDENTIAL © 2010 SpringSource, A division of VMware. All rights reserved
    • 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
    • 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
    • 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
    • Standardized Annotations@ManagedBeanpublic class MyBookAdminService implements BookAdminService { @Inject public MyBookAdminService(AccountRepository ar) { … } @TransactionAttribute public BookUpdate updateBook(Addendum addendum) { … }} CONFIDENTIAL 5
    • 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
    • EL in XML Bean Definitions<bean class="mycompany.BookTestDatabase"> <property name="databaseName" value="#{systemProperties.databaseName}"/> <property name="keyGenerator" value="#{strategyBean.databaseKeyGenerator}"/></bean> CONFIDENTIAL 7
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • Spring 3.1 – Component Model Enhancements Environment profiles for bean definitions Java-based application configuration "c:" namespace for XML configuration Declarative caching CONFIDENTIAL 15
    • 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
    • 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
    • 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
    • "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
    • Declarative Caching@Cacheablepublic Owner loadOwner(int id);@Cacheable(condition="name.length < 10")public Owner loadOwner(String name);@CacheEvictpublic void deleteOwner(int id); CONFIDENTIAL 20
    • 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
    • 杭州站 · 2011年10月20日~22日 www.qconhangzhou.com(6月启动)QCon北京站官方网站和资料下载 www.qconbeijing.com