Advanced Spring

5,365 views

Published on

Presentation of CityGrid internal developer training.

Published in: Technology

Advanced Spring

  1. 1. Developer Training Spring Framework Series: Advanced Spring
  2. 2. Who am I?● Andy Chan● email: andy.chan@citygridmedia.com● skype: achan1218● twitter: iceycake
  3. 3. Example Source Codes● git://github.com/iceycake/intro-spring.git● git://github.com/iceycake/tutorial-advanced-spring.git
  4. 4. How to Get Sample Code> git clone git://github.com/iceycake/tutorial-advanced-spring.git> cd tutorial-advanced-spring> git branch -a> mvn clean install
  5. 5. GitHub Demo
  6. 6. Installing Git on OS X● Use Installer ○ http://code.google.com/p/git-osx-installer/● Use HomeBrew ○ http://mxcl.github.com/homebrew/ ○ brew install git● Use MacPorts ○ http://www.macports.org/ ○ sudo port install git
  7. 7. gitgit clonegit branch -agit fetchgit mergegit rebasegit statusgit log...
  8. 8. Introduction to Git May 21, 2012
  9. 9. Advanced Spring
  10. 10. Spring Modules
  11. 11. Spring Core Review
  12. 12. Spring Core Review● Container ○ Dependency Injection (DI) ○ Inversion of Control (IoC)● Java Configuration vs XML Configuration● Properties Injection● Spring Profile
  13. 13. BeyondDependency Injection
  14. 14. Advanced Spring● Threads● Web Service● Database
  15. 15. Threads
  16. 16. Review: Javapublic class MyThread extends Thread { public MyThread() { } @Override public void run() { }}
  17. 17. Review: Javapublic class MyThread implements Runnable { public MyThread() { } public void run() { }}
  18. 18. Review: Java 5public class MyThread implements Callable<Result> { public MyThread() { } public Result call() { Result result = ... ... return result; }}
  19. 19. Java 5 Thread Pool / Executor● SingleThreadExecutor● FixedThreadPool● CachedThreadPool● ScheduledThreadPool
  20. 20. Spring 2.5 Thread Pool / Executor● SimpleAsyncTaskExecutor● SyncTaskExecutor● SimpleThreadPoolTaskExecutor (Quartz)● ConcurrentTaskExecutor● * ThreadPoolTaskExecutor● TimerTaskExecutor● WorkManagerTaskExecutor
  21. 21. Demo
  22. 22. Spring 3.x// thread.delay.ms=500@Scheduled(fixedDelay = "${thread.delay.ms}")public void doSomething() { ...}// Every Night @ 1am// thread.cron=* * 1 * * ?// Every Monday to Friday @ 10:15am// thread.cron=0 15 10 ? * MON-FRI@Scheduled(cron = "${thread.cron}")public void doSomething() { ...}
  23. 23. Spring 3.x@Asyncpublic Future<Result> doSomething(int arg) { ... return new AsyncResult<Result>(...);}
  24. 24. Demo
  25. 25. Web Service (RESTful)
  26. 26. Spring 3 REST API● Based on Spring MVC● Spring API / Syntax● Support XML, JSON Out of the Box● Unofficial / Unstable ○ Fast Infoset ○ Protobuf
  27. 27. JAX-RS● "Standard" API● CXF● Jersey● Restlet● RESTEasy
  28. 28. Jersey● JSR-311 Reference Implementation● Light Weight (No extra Dependency)● Works well with Spring● Supports XML● Supports (with extensions) ○ JSON ○ Javascript / JSON-P ○ Fast Infoset ○ Protobuf, Protostuff ○ Avro, Thrift (?)
  29. 29. Spring + Jersey
  30. 30. JSX-RS● @GET● @PUT● @POST● @DELETE
  31. 31. JSX-RS (Cont)@GET@Consumes(MediaType.APPLICATION_XML)@Produces(MediaType.APPLICATION_XML)@Path("/feed/{feedCode}")public Response getFeed(@PathParam("feedCode") final String feedCode) { ... return Response.status(200).entity(...).build();}
  32. 32. Spring 3 REST vs JAX-RShttp://www.infoq.com/articles/springmvc_jsx-rs
  33. 33. Demo
  34. 34. RESTful Client● HTTP Client ○ Great Documentation ○ Foundation of most of Web Service Clients ○ Lots of Coding● Jersey Client ○ Poor Documentation ○ Easy to Use● Spring 3 RestTemplate ○ Great Documentation ○ Community / Lots of Examples ○ Easy to Use
  35. 35. Demo
  36. 36. Database
  37. 37. Connection & DataSources● JDBC Connection● JNDI● Embedded Database ○ HyperSQL/Hypersonic SQL (HSQL) ○ H2 ○ Derby (JavaDB)
  38. 38. JDBC DataSource<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${database.driverClassName}</value> </property> <property name="url"> <value>${database.url}</value> </property> <property name="username"> <value>${database.username}</value> </property> <property name="password"> <value>${database.password}</value> </property></bean>
  39. 39. Embedded Database<jdbc:embedded-database id="feedDataSource" type="H2"> <jdbc:script location="classpath:schema.sql"/> <jdbc:script location= "..."/> <jdbc:script location="..."/> ...</jdbc:embedded-database>
  40. 40. JDBC● JDBCTemplate● Spring Data ○ JDBC Extension● MyBatis (iBatis) ○ Spring 3.x
  41. 41. Demo
  42. 42. Object-Relational Mapping (ORM)● Hibernate● JPA / JPA2
  43. 43. Transaction Management
  44. 44. Java Transaction Overview● Local Transaction ○ Single Resource ■ Database Resource or Message Queue● Global Transaction ○ Java Transaction API (JTA) ○ via XA Protocol ○ Multiple Resources ■ e.g. Database Resources + Message Queues
  45. 45. Spring TX● Local Transaction● Global Transaction (Spring TX + JTA)● Annotation● XML Configuration● Programmatically
  46. 46. Spring TX Annotation● <tx:annotation-driven/>● @Transactional Attributes ○ propagation ○ isolation ○ timeout ○ readOnly ○ rollbackFor ○ rollbackForClassName ○ noRollbackFor ○ noRollbackForClassName ○ value
  47. 47. Spring TX XML Configuration<aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.citygrid.training.spring.advanced.tx.*Dao.*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/></aop:config><tx:advice id="txAdvice"> <tx:attributes> <tx:method name="getCount" read-only="true"/> <tx:method name="*" /> </tx:attributes></tx:advice>
  48. 48. Spring TX Annotation@Service("feedDao")@Repository@Transactionalclass FeedDao { @Transactional(readOnly=true) public int getCount() { ... } public void insert(final Feed feed) { ... }}
  49. 49. Global Transaction (XA)● Bullet Proof Guarantee● Very Complex● Difficult to Implement● Alternatives?
  50. 50. Summary● Thread ○ Task Executor, Thread Pool ○ @Scheduled, @Async● Web Service ○ Jersey + Spring 3 ○ RestTemplate● Database ○ JdbcTemplate, JdbcDaoSupport ○ MyBatis 3 + Spring 3 ○ Spring TX
  51. 51. Want More Spring?● Spring MVC● Spring AOP● Spring Data● Spring Batch● Spring Integration● Spring Hadoop
  52. 52. Q&A

×