Tailoring Spring for Custom Usage   • Josh Long, SpringSource, a division of VMware   1Thursday, January 24, 13
About Josh Long (龙            之春) Spring Developer Advocate   twitter: @starbuxman   weibo: @springsource   josh.long@spri...
Agenda   Explore the value of a framework   Exploit some of the lesser known, but powerful, extension     hooks in the cor...
Spring’s aim:    bring simplicity to java development                                                                     ...
The Spring ApplicationContext   • Spring Manages the beans you tell it to manage          –   use annotations (JSR 250, JS...
Spring, a walking tour   • Demos:          – introduce the tool chain          – how to “setup” Spring          – basic de...
Spring Integration rules!!                           Not confidential. Tell everyone.   7Thursday, January 24, 13
...but??!?                           ...what if it doesn’t do what I want?   8Thursday, January 24, 13
What is Spring Integration?FLEXIBLE                                          v                           Not confidential....
10Thursday, January 24, 13
do NOT reinvent                           the Wheel!   11Thursday, January 24, 13
The Open/Closed Principle   "software entities (classes, modules, functions, etc.) should     be open for extension, but c...
Working with Lots of Beans   • One way to selectively augment beans at the lower level:          – BeanPostProcessor      ...
Spring, a walking tour   • Demos:          – Bean*PostProcessor          – AspectJ   14Thursday, January 24, 13
Life Cycles   • Life Cycles for different folks          – “safe and consistent” - use the interfaces                • Ini...
Scopes   • Spring beans have scopes          – default: singleton          – can be:                •   prototype         ...
Scopes           – Implement o.s.beans.factory.config.Scope           – register the scope with a             o.s.beans.fac...
Scopes           – Implement o.s.beans.factory.config.Scope           – register the scope with a             o.s.beans.fac...
Scopes           – Implement o.s.beans.factory.config.Scope           – register the scope with a             o.s.beans.fac...
Scopes           – Implement o.s.beans.factory.config.Scope           – register the scope with a             o.s.beans.fac...
Spring, a walking tour   • Demos:          – life cycle callbacks          – scopes                • using                ...
Getting Beans from Strange Places   • FactoryBeans   • Spring Expression Language          – convenient way to get at valu...
Getting Beans from Strange Places   • FactoryBeans          – interface that’s used to provide a reusable definition of how...
Getting Beans from Strange Places   • Spring Expression Language          –   convenient way to get at values and inject t...
Getting Beans from Strange Places   • Spring profiles                • @Profile(“production”) @Configuration ...             ...
Getting Beans from Strange Places   • An ApplicationContextInitializer        public interface ApplicationContextInitializ...
Getting Beans from Strange Places   • Demos:          – FactoryBeans          – SpEL          – Profiles                • A...
Proxies!     CustomerService cs = new CustomerService();     cs...     ProxyFactory pf = new ProxyFaxctory();     pf.setTa...
Using Spring’s Resources   • Spring supports out of the box ClassPathResource,     FileResource system, etc.   • Writing y...
Object to XML Marshallers   • Easy to add your own Marshaller (and Unmarshaller)        public interface Marshaller {     ...
Object to XML Marshallers   • Demos:          – a custom object-to-XML marshaller   28Thursday, January 24, 13
REST   • Spring MVC for the server   @RequestMapping( value = “/crm/customers/{id}” ,                     method =HttpMeth...
REST   • RestTemplate for the client (Android, SE, web     applications, etc.)        RestTemplate rt = new RestTemplate()...
REST   • Both need o.s.http.converter.HttpMessageConverters   • Spring supports:          –   object-to-XML (JAXB as well ...
Registering a custom HttpMessageConverter        @EnableWebMvc        public class WebConfiguration extends WebMvcConfigurer...
REST   • Demos:          – Writing and using a customer HttpMessageConverter   33Thursday, January 24, 13
Transactions   • Spring supports declarative transaction management          – @EnableTransactionManagement or            ...
Transactions   • PlatformTransactionManager abstracts the notion     of a transactional “unit of work.”        public inte...
Caching   • CacheManager’s maintain Caches.          – CacheManagers are like ‘connections’          – Caches are like reg...
Writing a custom View and View Resolver   37Thursday, January 24, 13
Writing a custom View and View Resolver   • Easy to add your own View          – supported views out of the box: FreeMarke...
Writing a custom View and View Resolver         public interface ViewResolver {           View resolveViewName(String view...
Writing a custom View and View Resolverpublic interface View {	           String RESPONSE_STATUS_ATTRIBUTE =              ...
Writing a custom View and View Resolver @Bean public ViewResolver myCustomViewResolver(){   ... }                         ...
Writing a custom View and View Resolver @Bean public ViewResolver myCustomViewResolver(){   ... }                         ...
Writing a custom View and View Resolver   • Demo: writing a custom view/view resolver   42Thursday, January 24, 13
A Custom NameSpace  public class ASimpleParser extends AbstractSingleBeanDefinitionParser {   @Override   protected void d...
Writing Adapters in Spring Integration   44Thursday, January 24, 13
Writing Adapters in Spring Integration   • MessageSource for inbound adapters   • MessageHandler for outbound adapters    ...
Writing Adapters in Spring Integration   • Inbound channel adapter “receives” message from     external system inward rela...
Writing Adapters in Spring Integration   • Outbound channel adapter “publishes” message from Spring     Integration outwar...
Spring Integration File System Adapters   • Spring Integration provides rich file system adapters          – FTP, SFTP, FTP...
Writing Readers and Writers in Spring Batch   • ItemReader for inbound adapters   • ItemWriters for outbound adapters   49...
Summary / Questions   • code: git.springsource.org:spring-samples/spring-     samples.git   • github.com/SpringSource   • ...
Q&A 51   © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.Thursday, January 24, 13
Upcoming SlideShare
Loading in...5
×

Extending spring

847

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
847
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Extending spring

  1. 1. Tailoring Spring for Custom Usage • Josh Long, SpringSource, a division of VMware 1Thursday, January 24, 13
  2. 2. About Josh Long (龙 之春) Spring Developer Advocate twitter: @starbuxman weibo: @springsource josh.long@springsource.com 2Thursday, January 24, 13
  3. 3. Agenda Explore the value of a framework Exploit some of the lesser known, but powerful, extension hooks in the core Spring framework QA 3Thursday, January 24, 13
  4. 4. Spring’s aim: bring simplicity to java development data web tier integration batch access & service tier & mobile processing / NoSQL / RIA messaging Big Data The Spring framework the cloud: lightweight traditional CloudFoundry WebSphere tc Server VMForce JBoss AS Tomcat Google App Engine WebLogic Jetty Amazon Web Services (on legacy versions, too!) 4Thursday, January 24, 13
  5. 5. The Spring ApplicationContext • Spring Manages the beans you tell it to manage – use annotations (JSR 250, JSR 330, native) – XML – Java configuration – component scanning • You can of course use all of them! Mix ‘n match • All configuration styles tell the ApplicationContext how to manage your beans 5Thursday, January 24, 13
  6. 6. Spring, a walking tour • Demos: – introduce the tool chain – how to “setup” Spring – basic dependency injection • annotations (JSR 250, JSR 330, native) • xml • java configuration 6Thursday, January 24, 13
  7. 7. Spring Integration rules!! Not confidential. Tell everyone. 7Thursday, January 24, 13
  8. 8. ...but??!? ...what if it doesn’t do what I want? 8Thursday, January 24, 13
  9. 9. What is Spring Integration?FLEXIBLE v Not confidential. Tell everyone. 9Thursday, January 24, 13
  10. 10. 10Thursday, January 24, 13
  11. 11. do NOT reinvent the Wheel! 11Thursday, January 24, 13
  12. 12. The Open/Closed Principle "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” -Bob Martin 12Thursday, January 24, 13
  13. 13. Working with Lots of Beans • One way to selectively augment beans at the lower level: – BeanPostProcessor • are regular beans and are run after the configured beans have been created, but before the context is finished setting up – BeanFactoryPostProcessor • is run before any of the beans definitions are realized • comes before BPP • A more natural alternative is Spring’s AOP support – built on top of AspectJ – provides a very convenient, powerful way to solve cross cutting problems 13Thursday, January 24, 13
  14. 14. Spring, a walking tour • Demos: – Bean*PostProcessor – AspectJ 14Thursday, January 24, 13
  15. 15. Life Cycles • Life Cycles for different folks – “safe and consistent” - use the interfaces • InitializingBean, DisposableBean • correspond to init-method and destroy-method attributes – Simple and component-centric : use the annotations • @PostConstruct, @PreDestroy • correspond to init-method and destroy-method attributes – More power: SmartLifecycle • gives you the ability to dynamically start and stop beans in a certain order as well as to query whether the bean’s been started or not. 15Thursday, January 24, 13
  16. 16. Scopes • Spring beans have scopes – default: singleton – can be: • prototype • HTTP session • HTTP request • HTTP application (servlet, basically) • “step” in Spring batch • thread-local • Spring Web Flow “flow” scoped • Spring Web Flow “conversation scoped” • Spring Web Flow “view” scoped (in JSF) • Activiti BPMN2 process-scoped 16Thursday, January 24, 13
  17. 17. Scopes – Implement o.s.beans.factory.config.Scope – register the scope with a o.s.beans.factory.config.CustomScopeConfigurer public interface Scope { Object get(String name, ObjectFactory<?> objectFactory); Object remove(String name); void registerDestructionCallback(String name, Runnable callback); Object resolveContextualObject(String key); String getConversationId(); } 17Thursday, January 24, 13
  18. 18. Scopes – Implement o.s.beans.factory.config.Scope – register the scope with a o.s.beans.factory.config.CustomScopeConfigurer public interface Scope { Object get(String name, ObjectFactory<?> objectFactory); map-like lookup of beans in a Object remove(String name); given scope void registerDestructionCallback(String name, Runnable callback); Object resolveContextualObject(String key); String getConversationId(); } 17Thursday, January 24, 13
  19. 19. Scopes – Implement o.s.beans.factory.config.Scope – register the scope with a o.s.beans.factory.config.CustomScopeConfigurer public interface Scope { Object get(String name, ObjectFactory<?> objectFactory); map-like lookup of beans in a Object remove(String name); given scope void registerDestructionCallback(String name, Runnable callback); Object resolveContextualObject(String key); well known beans like the HttpServletRequest ‘request’ for String getConversationId(); ‘request’ scope } 17Thursday, January 24, 13
  20. 20. Scopes – Implement o.s.beans.factory.config.Scope – register the scope with a o.s.beans.factory.config.CustomScopeConfigurer public interface Scope { Object get(String name, ObjectFactory<?> objectFactory); map-like lookup of beans in a Object remove(String name); given scope void registerDestructionCallback(String name, Runnable callback); Object resolveContextualObject(String key); well known beans like the HttpServletRequest ‘request’ for String getConversationId(); ‘request’ scope } null, or storage specific ‘conversation’ ID 17Thursday, January 24, 13
  21. 21. Spring, a walking tour • Demos: – life cycle callbacks – scopes • using • creating your own 18Thursday, January 24, 13
  22. 22. Getting Beans from Strange Places • FactoryBeans • Spring Expression Language – convenient way to get at values and inject them • Spring environment specific beans (profiles) – introduced in Spring 3.1 – make it easy to conditionally define an object based on some sort of runtime condition 19Thursday, January 24, 13
  23. 23. Getting Beans from Strange Places • FactoryBeans – interface that’s used to provide a reusable definition of how to create a complicated object with many dependencies – Related: Java configuration, and builders • prefer both over FactoryBeans where possible 20Thursday, January 24, 13
  24. 24. Getting Beans from Strange Places • Spring Expression Language – convenient way to get at values and inject them – Andy Clement’s a genius – like the Unified JSF EL, on steroids – Can be used in Java, XML • @Value(“#{ ... }”) or value = “#{ .. }” 21Thursday, January 24, 13
  25. 25. Getting Beans from Strange Places • Spring profiles • @Profile(“production”) @Configuration ... • <beans profile = ‘production’> ... </beans> – Use System properties or simply specify the active profile on the environment – Use ApplicationContextInitializer in web applications 22Thursday, January 24, 13
  26. 26. Getting Beans from Strange Places • An ApplicationContextInitializer public interface ApplicationContextInitializer <C extends ConfigurableApplicationContext> { void initialize(C applicationContext); } 23Thursday, January 24, 13
  27. 27. Getting Beans from Strange Places • Demos: – FactoryBeans – SpEL – Profiles • ApplicationContextInitializers 24Thursday, January 24, 13
  28. 28. Proxies! CustomerService cs = new CustomerService(); cs... ProxyFactory pf = new ProxyFaxctory(); pf.setTarget( cs ); pf.addAdvice(new MethodInterceptor(){ public Object invoke(MethodInvocation mi) { // ... } } ); return (CustomerService) pf.getObject() ; 25Thursday, January 24, 13
  29. 29. Using Spring’s Resources • Spring supports out of the box ClassPathResource, FileResource system, etc. • Writing your own Resource implementations public interface Resource extends InputStreamSource { boolean exists(); boolean isReadable(); boolean isOpen(); URL getURL() throws IOException; URI getURI() throws IOException; File getFile() throws IOException; long contentLength() throws IOException; long lastModified() throws IOException; Resource createRelative(String relativePath) throws IOException; String getFilename(); String getDescription(); } 26Thursday, January 24, 13
  30. 30. Object to XML Marshallers • Easy to add your own Marshaller (and Unmarshaller) public interface Marshaller { boolean supports(Class<?> clazz); void marshal(Object graph, Result result) throws IOException, XmlMappingException; } 27Thursday, January 24, 13
  31. 31. Object to XML Marshallers • Demos: – a custom object-to-XML marshaller 28Thursday, January 24, 13
  32. 32. REST • Spring MVC for the server @RequestMapping( value = “/crm/customers/{id}” , method =HttpMethod.GET) public @ResponseBody Customer lookupCustomerById( @PathVariable(“id”) long customerId ) { ... return customer; } 29Thursday, January 24, 13
  33. 33. REST • RestTemplate for the client (Android, SE, web applications, etc.) RestTemplate rt = new RestTemplate() ; String url = “http://mysvc.cloudfoundry.com/crm/customer/{id}”; Customer customer = rt.getForObject( url, Customer.class, 22); 30Thursday, January 24, 13
  34. 34. REST • Both need o.s.http.converter.HttpMessageConverters • Spring supports: – object-to-XML (JAXB as well as any Spring OXM impl) – object-to-JSON – binary data (o.s.resource.Resource references or byte[]) – ATOM/RSS – images • Easy to add your own 31Thursday, January 24, 13
  35. 35. Registering a custom HttpMessageConverter @EnableWebMvc public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { } } 32Thursday, January 24, 13
  36. 36. REST • Demos: – Writing and using a customer HttpMessageConverter 33Thursday, January 24, 13
  37. 37. Transactions • Spring supports declarative transaction management – @EnableTransactionManagement or <tx:annotation-driven/> • PlatformTransactionManager implementations used to manage transactions – lots of options out of the box: • AMQP, JMS, JTA, JDBC, JDO, JPA, WebLogic-specific, WebSphere-specific, OC4J-specific, etc. 34Thursday, January 24, 13
  38. 38. Transactions • PlatformTransactionManager abstracts the notion of a transactional “unit of work.” public interface PlatformTransactionManager { TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; void commit(TransactionStatus status) throws TransactionException; void rollback(TransactionStatus status) throws TransactionException; } 35Thursday, January 24, 13
  39. 39. Caching • CacheManager’s maintain Caches. – CacheManagers are like ‘connections’ – Caches are like regions of a cache public interface CacheManager { public interface Cache { Cache getCache(String name); Collection<String> getCacheNames(); interface ValueWrapper { } Object get(); } String getName(); Object getNativeCache(); ValueWrapper get(Object key); void put(Object key, Object value); void evict(Object key); void clear(); } 36Thursday, January 24, 13
  40. 40. Writing a custom View and View Resolver 37Thursday, January 24, 13
  41. 41. Writing a custom View and View Resolver • Easy to add your own View – supported views out of the box: FreeMarker, Velocity, Excel, PDFs, JasperReports, XSLT, Jackson, JSTL, etc. – Lots of contributions from the open source community: • Thymeleaf http://www.thymeleaf.org/ • Mustache by Sean Scanlon https://github.com/sps/mustache-spring-view 38Thursday, January 24, 13
  42. 42. Writing a custom View and View Resolver public interface ViewResolver { View resolveViewName(String viewName, Locale locale) throws Exception; } 39Thursday, January 24, 13
  43. 43. Writing a custom View and View Resolverpublic interface View { String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus"; String getContentType(); void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception;} 40Thursday, January 24, 13
  44. 44. Writing a custom View and View Resolver @Bean public ViewResolver myCustomViewResolver(){ ... } if ‘detectAllViewResolvers’ is true, all ViewResolvers types will be registered. @Bean public MyCustomViewResolver viewResolver() { ... } 41Thursday, January 24, 13
  45. 45. Writing a custom View and View Resolver @Bean public ViewResolver myCustomViewResolver(){ ... } if ‘detectAllViewResolvers’ is true, all ViewResolvers types will be registered. @Bean public MyCustomViewResolver viewResolver() { ... if ‘detectAllViewResolvers’ is false, it’ll lookup a bean by a } well known name 41Thursday, January 24, 13
  46. 46. Writing a custom View and View Resolver • Demo: writing a custom view/view resolver 42Thursday, January 24, 13
  47. 47. A Custom NameSpace public class ASimpleParser extends AbstractSingleBeanDefinitionParser { @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { String exchangeName = element.getAttribute(NAME_ATTRIBUTE); builder.addConstructorArgValue(new TypedStringValue(exchangeName)); Element bindings = DomUtils.getChildElementByTagName(element, BINDINGS_ELE); if (bindings != null) { } NamespaceUtils.addConstructorArgBooleanValueIfAttributeDefined(builder, element, DURABLE_ATTRIBUTE, true); ... } 43Thursday, January 24, 13
  48. 48. Writing Adapters in Spring Integration 44Thursday, January 24, 13
  49. 49. Writing Adapters in Spring Integration • MessageSource for inbound adapters • MessageHandler for outbound adapters MessageSource MessageHandler 45Thursday, January 24, 13
  50. 50. Writing Adapters in Spring Integration • Inbound channel adapter “receives” message from external system inward relative to Spring Integration code package org.springframework.integration.core; public interface MessageSource<T> { org.springframework.integration.Message<T> receive(); } <int:channel id = “in” /> <int:inbound-channel-adapter channel = “in” ref = “myCustomMessageSource” > <int:cron-trigger ... /> </int:inbound-channel-adapter> 46Thursday, January 24, 13
  51. 51. Writing Adapters in Spring Integration • Outbound channel adapter “publishes” message from Spring Integration outward relative to Spring Integration code package org.springframework.integration.core; public interface MessageHandler { void handleMessage( org.springframework.integration.Message<?> message) throws org.springframework.integration.MessagingException; } <int:channel id = “out” /> <int:outbound-channel-adapter channel = “out” ref = “myCustomMessageHandler” /> 47Thursday, January 24, 13
  52. 52. Spring Integration File System Adapters • Spring Integration provides rich file system adapters – FTP, SFTP, FTPS, files in general – But... what about SMB/CIFS? 48Thursday, January 24, 13
  53. 53. Writing Readers and Writers in Spring Batch • ItemReader for inbound adapters • ItemWriters for outbound adapters 49Thursday, January 24, 13
  54. 54. Summary / Questions • code: git.springsource.org:spring-samples/spring- samples.git • github.com/SpringSource • weibo.com/SpringFramework • blog.springsource.org • josh.long@springsource.com • springsource.com/developer/sts 50Thursday, January 24, 13
  55. 55. Q&A 51 © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.Thursday, January 24, 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×