extending spring for custom usage
DESCRIPTION
The Spring framework packs a lot of punch, out of the box! The surface-level component model's extraordinarily flexible, and works well with in most situations, but the real power of Spring lays just underneath, in the numerous SPIs that Spring exposes, so that you can tailor the component model to your own use cases. Spring's SPI's are a great example of what Bob Martin describes as the open-closed principle, and it provides the solid underpinnings upon which the other Spring frameworks, including Spring Integration, Spring MVC and Spring Batch are built. In this talk, Josh Long, the Spring developer advocate from SpringSource, provides a walking tour of Spring's extension points.TRANSCRIPT
![Page 1: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/1.jpg)
• Josh Long, SpringSource, a division of VMware
1
Tailoring Spring for Custom Usage
Sunday, February 19, 12
![Page 2: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/2.jpg)
2
About Josh Long
this is important!
(Spring Developer Advocate)
Sunday, February 19, 12
![Page 3: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/3.jpg)
Agenda
Explore the value of a framework Exploit some of the lesser known, but powerful, extension
hooks in the core Spring frameworkQA
3
Sunday, February 19, 12
![Page 4: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/4.jpg)
Spring’s aim:
bring simplicity to java development
4
web tier &
RIAservice tier batch
processing
integration &
messaging
data access
/ NoSQL / Big Data
mobile
tc ServerTomcatJetty
lightweightCloudFoundry
VMForce Google App Engine
Amazon Web Services
the cloud: WebSphereJBoss ASWebLogic
(on legacy versions, too!)
traditional
The Spring framework
Sunday, February 19, 12
![Page 5: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/5.jpg)
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
5
Sunday, February 19, 12
![Page 6: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/6.jpg)
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
6
Sunday, February 19, 12
![Page 7: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/7.jpg)
Not confidential. Tell everyone. 7
Spring Integration rules!!
Sunday, February 19, 12
![Page 8: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/8.jpg)
8
...what if it doesn’t do what I want?...but??!?
Sunday, February 19, 12
![Page 9: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/9.jpg)
Not confidential. Tell everyone. 9
What is Spring Integration?FLEXIBLE
v
Sunday, February 19, 12
![Page 10: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/10.jpg)
10
Sunday, February 19, 12
![Page 11: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/11.jpg)
11
do NOT reinvent the Wheel!
Sunday, February 19, 12
![Page 12: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/12.jpg)
The Open/Closed Principle
"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”
-Bob Martin
12
Sunday, February 19, 12
![Page 13: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/13.jpg)
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
13
Sunday, February 19, 12
![Page 14: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/14.jpg)
Spring, a walking tour
• Demos:– Bean*PostProcessor– AspectJ
14
Sunday, February 19, 12
![Page 15: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/15.jpg)
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.
15
Sunday, February 19, 12
![Page 16: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/16.jpg)
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
16
Sunday, February 19, 12
![Page 17: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/17.jpg)
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();}
Scopes– Implement o.s.beans.factory.config.Scope– register the scope with a o.s.beans.factory.config.CustomScopeConfigurer
17
Sunday, February 19, 12
![Page 18: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/18.jpg)
map-like lookup of beans in a given scope
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();}
Scopes– Implement o.s.beans.factory.config.Scope– register the scope with a o.s.beans.factory.config.CustomScopeConfigurer
17
Sunday, February 19, 12
![Page 19: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/19.jpg)
map-like lookup of beans in a given scope
well known beans like the HttpServletRequest ‘request’ for ‘request’ scope
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();}
Scopes– Implement o.s.beans.factory.config.Scope– register the scope with a o.s.beans.factory.config.CustomScopeConfigurer
17
Sunday, February 19, 12
![Page 20: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/20.jpg)
null, or storage specific ‘conversation’ ID
map-like lookup of beans in a given scope
well known beans like the HttpServletRequest ‘request’ for ‘request’ scope
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();}
Scopes– Implement o.s.beans.factory.config.Scope– register the scope with a o.s.beans.factory.config.CustomScopeConfigurer
17
Sunday, February 19, 12
![Page 21: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/21.jpg)
Spring, a walking tour
• Demos:– life cycle callbacks– scopes
• using• creating your own
18
Sunday, February 19, 12
![Page 22: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/22.jpg)
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
19
Sunday, February 19, 12
![Page 23: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/23.jpg)
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
20
Sunday, February 19, 12
![Page 24: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/24.jpg)
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 = “#{ .. }”
21
Sunday, February 19, 12
![Page 25: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/25.jpg)
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
22
Sunday, February 19, 12
![Page 26: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/26.jpg)
Getting Beans from Strange Places
• An ApplicationContextInitializer
23
public interface ApplicationContextInitializer <C extends ConfigurableApplicationContext> {
void initialize(C applicationContext);}
Sunday, February 19, 12
![Page 27: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/27.jpg)
Getting Beans from Strange Places
• Demos:– FactoryBeans– SpEL– Profiles
• ApplicationContextInitializers
24
Sunday, February 19, 12
![Page 28: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/28.jpg)
Using Spring’s Resources
• Spring supports out of the box ClassPathResource, FileResource system, etc.
• Writing your own Resource implementations
25
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();}
Sunday, February 19, 12
![Page 29: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/29.jpg)
Object to XML Marshallers
• Easy to add your own Marshaller (and Unmarshaller)
26
public interface Marshaller {
boolean supports(Class<?> clazz); void marshal(Object graph, Result result) throws IOException, XmlMappingException;}
Sunday, February 19, 12
![Page 30: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/30.jpg)
Object to XML Marshallers
• Demos:– a custom object-to-XML marshaller
27
Sunday, February 19, 12
![Page 31: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/31.jpg)
REST
• Spring MVC for the server
28
@RequestMapping( value = “/crm/customers/{id}” , method =HttpMethod.GET)public @ResponseBody Customer lookupCustomerById( @PathVariable(“id”) long customerId ) { ... return customer; }
Sunday, February 19, 12
![Page 32: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/32.jpg)
REST
• RestTemplate for the client (Android, SE, web applications, etc.)
29
RestTemplate rt = new RestTemplate() ;
String url = “http://mysvc.cloudfoundry.com/crm/customer/{id}”;
Customer customer = rt.getForObject( url, Customer.class, 22);
Sunday, February 19, 12
![Page 33: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/33.jpg)
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
30
Sunday, February 19, 12
![Page 34: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/34.jpg)
Registering a custom HttpMessageConverter
31
@EnableWebMvcpublic class WebConfiguration extends WebMvcConfigurerAdapter {
@Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) {
}
}
Sunday, February 19, 12
![Page 35: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/35.jpg)
REST
• Demos:– Writing and using a customer HttpMessageConverter
32
Sunday, February 19, 12
![Page 36: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/36.jpg)
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.
33
Sunday, February 19, 12
![Page 37: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/37.jpg)
Transactions
• PlatformTransactionManager abstracts the notion of a transactional “unit of work.”
34
public interface PlatformTransactionManager { TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; void commit(TransactionStatus status) throws TransactionException; void rollback(TransactionStatus status) throws TransactionException;}
Sunday, February 19, 12
![Page 38: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/38.jpg)
Caching
• CacheManager’s maintain Caches.– CacheManagers are like ‘connections’– Caches are like regions of a cache
35
public interface CacheManager { Cache getCache(String name); Collection<String> getCacheNames();}
public interface Cache {
interface ValueWrapper { Object get(); }
String getName(); Object getNativeCache(); ValueWrapper get(Object key); void put(Object key, Object value); void evict(Object key); void clear();}
Sunday, February 19, 12
![Page 39: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/39.jpg)
Writing a custom View and View Resolver
36
Sunday, February 19, 12
![Page 40: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/40.jpg)
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
37
Sunday, February 19, 12
![Page 41: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/41.jpg)
Writing a custom View and View Resolver
38
public interface ViewResolver { View resolveViewName(String viewName, Locale locale) throws Exception;}
Sunday, February 19, 12
![Page 42: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/42.jpg)
Writing a custom View and View Resolver
39
public interface View {
String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus"; String getContentType(); void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
Sunday, February 19, 12
![Page 43: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/43.jpg)
if ‘detectAllViewResolvers’ is true, all ViewResolvers types will be registered.
Writing a custom View and View Resolver
40
@Beanpublic ViewResolver myCustomViewResolver(){ ... }
@Beanpublic MyCustomViewResolver viewResolver(){ ... }
Sunday, February 19, 12
![Page 44: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/44.jpg)
if ‘detectAllViewResolvers’ is true, all ViewResolvers types will be registered.
if ‘detectAllViewResolvers’ is false, it’ll lookup a bean by a well known name
Writing a custom View and View Resolver
40
@Beanpublic ViewResolver myCustomViewResolver(){ ... }
@Beanpublic MyCustomViewResolver viewResolver(){ ... }
Sunday, February 19, 12
![Page 45: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/45.jpg)
Writing a custom View and View Resolver
• Demo: writing a custom view/view resolver
41
Sunday, February 19, 12
![Page 46: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/46.jpg)
Writing Adapters in Spring Integration
42
Sunday, February 19, 12
![Page 47: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/47.jpg)
Writing Adapters in Spring Integration
• MessageSource for inbound adapters• MessageHandler for outbound adapters
43
MessageHandlerMessageSource
Sunday, February 19, 12
![Page 48: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/48.jpg)
Writing Adapters in Spring Integration
44
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>
• Inbound channel adapter “receives” message from external system inward relative to Spring Integration code
Sunday, February 19, 12
![Page 49: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/49.jpg)
Writing Adapters in Spring Integration
45
<int:channel id = “out” />
<int:outbound-channel-adapter channel = “out” ref = “myCustomMessageHandler” />
package org.springframework.integration.core;
public interface MessageHandler { void handleMessage( org.springframework.integration.Message<?> message) throws org.springframework.integration.MessagingException;
}
• Outbound channel adapter “publishes” message from Spring Integration outward relative to Spring Integration code
Sunday, February 19, 12
![Page 50: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/50.jpg)
Spring Integration File System Adapters
• Spring Integration provides rich file system adapters– FTP, SFTP, FTPS, files in general – But... what about SMB/CIFS?
46
Sunday, February 19, 12
![Page 51: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/51.jpg)
Writing Readers and Writers in Spring Batch
• ItemReader for inbound adapters• ItemWriters for outbound adapters
47
Sunday, February 19, 12
![Page 52: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/52.jpg)
Summary / Questions
• code: git.springsource.org:spring-samples/spring-samples.git
• blog.springsource.org • [email protected] • springsource.com/developer/sts
48
Sunday, February 19, 12
![Page 53: Extending Spring for Custom Usage](https://reader034.vdocuments.site/reader034/viewer/2022042700/555cba82d8b42a5f718b478f/html5/thumbnails/53.jpg)
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Q&A
49
Sunday, February 19, 12