SlideShare una empresa de Scribd logo
1 de 55
Descargar para leer sin conexión
Tailoring Spring for Custom Usage

   • Josh Long, SpringSource, a division of VMware




   1

Thursday, January 24, 13
About Josh Long (龙
            之春)
 Spring Developer Advocate
   twitter: @starbuxman
   weibo: @springsource
   josh.long@springsource.com




                                2

Thursday, January 24, 13
Agenda

   Explore the value of a framework
   Exploit some of the lesser known, but powerful, extension
     hooks in the core Spring framework
   QA




   3

Thursday, January 24, 13
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!)




                                                                                                     4

Thursday, January 24, 13
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

Thursday, January 24, 13
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

Thursday, January 24, 13
Spring Integration rules!!




                           Not confidential. Tell everyone.   7


Thursday, January 24, 13
...but??!?
                           ...what if it doesn’t do what I want?




   8

Thursday, January 24, 13
What is Spring Integration?
FLEXIBLE




                                          v




                           Not confidential. Tell everyone.   9


Thursday, January 24, 13
10

Thursday, January 24, 13
do NOT reinvent
                           the Wheel!




   11

Thursday, January 24, 13
The Open/Closed Principle

   "software entities (classes, modules, functions, etc.) should
     be open for extension, but closed for modification”
          -Bob Martin




   12

Thursday, January 24, 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


   13

Thursday, January 24, 13
Spring, a walking tour

   • Demos:
          – Bean*PostProcessor
          – AspectJ




   14

Thursday, January 24, 13
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

Thursday, January 24, 13
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

Thursday, January 24, 13
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();
   }




   17

Thursday, January 24, 13
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();
   }




   17

Thursday, January 24, 13
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
   }




   17

Thursday, January 24, 13
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


   17

Thursday, January 24, 13
Spring, a walking tour

   • Demos:
          – life cycle callbacks
          – scopes
                • using
                • creating your own




   18

Thursday, January 24, 13
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

Thursday, January 24, 13
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

Thursday, January 24, 13
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

Thursday, January 24, 13
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

Thursday, January 24, 13
Getting Beans from Strange Places

   • An ApplicationContextInitializer

        public interface ApplicationContextInitializer
              <C extends ConfigurableApplicationContext> {

                   void initialize(C applicationContext);
        }




   23

Thursday, January 24, 13
Getting Beans from Strange Places

   • Demos:
          – FactoryBeans
          – SpEL
          – Profiles
                • ApplicationContextInitializers




   24

Thursday, January 24, 13
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() ;




   25

Thursday, January 24, 13
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();
               }

   26

Thursday, January 24, 13
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;
        }




   27

Thursday, January 24, 13
Object to XML Marshallers

   • Demos:
          – a custom object-to-XML marshaller




   28

Thursday, January 24, 13
REST

   • Spring MVC for the server


   @RequestMapping( value = “/crm/customers/{id}” ,
                     method =HttpMethod.GET)
   public @ResponseBody Customer lookupCustomerById(
                 @PathVariable(“id”) long customerId ) {

        ...
        return customer;
   }




   29

Thursday, January 24, 13
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);




   30

Thursday, January 24, 13
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




   31

Thursday, January 24, 13
Registering a custom HttpMessageConverter


        @EnableWebMvc
        public class WebConfiguration extends WebMvcConfigurerAdapter {

            @Override
            public void configureMessageConverters(
                 List<HttpMessageConverter<?>> converters) {


            }

        }




   32

Thursday, January 24, 13
REST

   • Demos:
          – Writing and using a customer HttpMessageConverter




   33

Thursday, January 24, 13
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.




   34

Thursday, January 24, 13
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;
        }




   35

Thursday, January 24, 13
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();
                                            }

   36

Thursday, January 24, 13
Writing a custom View and View Resolver




   37

Thursday, January 24, 13
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




   38

Thursday, January 24, 13
Writing a custom View and View Resolver



         public interface ViewResolver {
           View resolveViewName(String viewName, Locale locale)
               throws Exception;
         }




   39

Thursday, January 24, 13
Writing a custom View and View Resolver


public interface View {

	           String RESPONSE_STATUS_ATTRIBUTE =
                       View.class.getName() + ".responseStatus";
	
	           String getContentType();

	           void render(Map<String, ?> model,
                         HttpServletRequest request,
                         HttpServletResponse response) throws Exception;

}




    40

Thursday, January 24, 13
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()
    {
       ...
     }




   41

Thursday, January 24, 13
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




   41

Thursday, January 24, 13
Writing a custom View and View Resolver

   • Demo: writing a custom view/view resolver




   42

Thursday, January 24, 13
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);
       ...

   }




   43

Thursday, January 24, 13
Writing Adapters in Spring Integration




   44

Thursday, January 24, 13
Writing Adapters in Spring Integration

   • MessageSource for inbound adapters
   • MessageHandler for outbound adapters




    MessageSource                      MessageHandler




   45

Thursday, January 24, 13
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>


   46

Thursday, January 24, 13
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” />

   47

Thursday, January 24, 13
Spring Integration File System Adapters

   • Spring Integration provides rich file system adapters
          – FTP, SFTP, FTPS, files in general
          – But... what about SMB/CIFS?




   48

Thursday, January 24, 13
Writing Readers and Writers in Spring Batch

   • ItemReader for inbound adapters
   • ItemWriters for outbound adapters




   49

Thursday, January 24, 13
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




   50

Thursday, January 24, 13
Q&A




 51   © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.

Thursday, January 24, 13

Más contenido relacionado

La actualidad más candente

Stop Worrying & Love the SQL - A Case Study
Stop Worrying & Love the SQL - A Case StudyStop Worrying & Love the SQL - A Case Study
Stop Worrying & Love the SQL - A Case StudyAll Things Open
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup PerformanceJustin Cataldo
 
12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocrat12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocratJonathan Linowes
 
#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015
#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015
#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015Matt Raible
 
Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Brian Cavalier
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gearsdion
 
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaroundGet Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaroundDataGeekery
 
Step by Step Personal Drive to One Drive Migration using SPMT
Step by Step Personal Drive to One Drive Migration using SPMTStep by Step Personal Drive to One Drive Migration using SPMT
Step by Step Personal Drive to One Drive Migration using SPMTIT Industry
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDBMongoDB
 
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...mfrancis
 
HTML5 JavaScript APIs
HTML5 JavaScript APIsHTML5 JavaScript APIs
HTML5 JavaScript APIsRemy Sharp
 
Performance Improvements in Browsers
Performance Improvements in BrowsersPerformance Improvements in Browsers
Performance Improvements in Browsersjeresig
 
Open Hack London - Introduction to YQL
Open Hack London - Introduction to YQLOpen Hack London - Introduction to YQL
Open Hack London - Introduction to YQLChristian Heilmann
 
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...David Kaneda
 
Building Event-Based Systems for the Real-Time Web
Building Event-Based Systems for the Real-Time WebBuilding Event-Based Systems for the Real-Time Web
Building Event-Based Systems for the Real-Time Webpauldix
 
jOOQ at Topconf 2013
jOOQ at Topconf 2013jOOQ at Topconf 2013
jOOQ at Topconf 2013Lukas Eder
 
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...Codemotion
 
Aligning Ember.js with Web Standards
Aligning Ember.js with Web StandardsAligning Ember.js with Web Standards
Aligning Ember.js with Web StandardsMatthew Beale
 
Forensic Tools for In-Depth Performance Investigations
Forensic Tools for In-Depth Performance InvestigationsForensic Tools for In-Depth Performance Investigations
Forensic Tools for In-Depth Performance InvestigationsNicholas Jansma
 

La actualidad más candente (20)

Stop Worrying & Love the SQL - A Case Study
Stop Worrying & Love the SQL - A Case StudyStop Worrying & Love the SQL - A Case Study
Stop Worrying & Love the SQL - A Case Study
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
 
12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocrat12 core technologies you should learn, love, and hate to be a 'real' technocrat
12 core technologies you should learn, love, and hate to be a 'real' technocrat
 
#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015
#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015
#NoXML: Eliminating XML in Spring Projects - SpringOne 2GX 2015
 
Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014Differential Sync and JSON Patch @ SpringOne2GX 2014
Differential Sync and JSON Patch @ SpringOne2GX 2014
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gears
 
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaroundGet Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
Get Back in Control of your SQL with jOOQ - GeekOut by ZeroTurnaround
 
Step by Step Personal Drive to One Drive Migration using SPMT
Step by Step Personal Drive to One Drive Migration using SPMTStep by Step Personal Drive to One Drive Migration using SPMT
Step by Step Personal Drive to One Drive Migration using SPMT
 
Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with MongoDB
 
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
 
HTML5 JavaScript APIs
HTML5 JavaScript APIsHTML5 JavaScript APIs
HTML5 JavaScript APIs
 
Performance Improvements in Browsers
Performance Improvements in BrowsersPerformance Improvements in Browsers
Performance Improvements in Browsers
 
Open Hack London - Introduction to YQL
Open Hack London - Introduction to YQLOpen Hack London - Introduction to YQL
Open Hack London - Introduction to YQL
 
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
How to Develop a Rich, Native-quality User Experience for Mobile Using Web St...
 
Building Event-Based Systems for the Real-Time Web
Building Event-Based Systems for the Real-Time WebBuilding Event-Based Systems for the Real-Time Web
Building Event-Based Systems for the Real-Time Web
 
jOOQ at Topconf 2013
jOOQ at Topconf 2013jOOQ at Topconf 2013
jOOQ at Topconf 2013
 
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
Tech Webinar: Offline First: Creare un'app Phonegap che funzioni offline e si...
 
Aligning Ember.js with Web Standards
Aligning Ember.js with Web StandardsAligning Ember.js with Web Standards
Aligning Ember.js with Web Standards
 
CouchDB Google
CouchDB GoogleCouchDB Google
CouchDB Google
 
Forensic Tools for In-Depth Performance Investigations
Forensic Tools for In-Depth Performance InvestigationsForensic Tools for In-Depth Performance Investigations
Forensic Tools for In-Depth Performance Investigations
 

Similar a Extending spring

Extending Spring for Custom Usage
Extending Spring for Custom UsageExtending Spring for Custom Usage
Extending Spring for Custom UsageJoshua Long
 
Spring in the Cloud - using Spring with Cloud Foundry
Spring in the Cloud - using Spring with Cloud FoundrySpring in the Cloud - using Spring with Cloud Foundry
Spring in the Cloud - using Spring with Cloud FoundryJoshua Long
 
Fact-Based Monitoring
Fact-Based MonitoringFact-Based Monitoring
Fact-Based MonitoringDatadog
 
Fact based monitoring
Fact based monitoringFact based monitoring
Fact based monitoringDatadog
 
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"Daniel Bryant
 
Python Load Testing - Pygotham 2012
Python Load Testing - Pygotham 2012Python Load Testing - Pygotham 2012
Python Load Testing - Pygotham 2012Dan Kuebrich
 
Writing Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason LeeWriting Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason Leejaxconf
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with SpringJoshua Long
 
Building Next-Gen Web Applications with the Spring 3 Web Stack
Building Next-Gen Web Applications with the Spring 3 Web StackBuilding Next-Gen Web Applications with the Spring 3 Web Stack
Building Next-Gen Web Applications with the Spring 3 Web StackJeremy Grelle
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO DevsWO Community
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking TourJoshua Long
 
비동기 회고 발표자료
비동기 회고 발표자료비동기 회고 발표자료
비동기 회고 발표자료Benjamin Kim
 
인피니스팬데이터그리드따라잡기 (@JCO 2014)
인피니스팬데이터그리드따라잡기 (@JCO 2014)인피니스팬데이터그리드따라잡기 (@JCO 2014)
인피니스팬데이터그리드따라잡기 (@JCO 2014)Jaehong Cheon
 
Enterprise Java Web Application Frameworks Sample Stack Implementation
Enterprise Java Web Application Frameworks   Sample Stack ImplementationEnterprise Java Web Application Frameworks   Sample Stack Implementation
Enterprise Java Web Application Frameworks Sample Stack ImplementationMert Çalışkan
 
Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...
Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...
Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...HostedbyConfluent
 
Plone FSR
Plone FSRPlone FSR
Plone FSRfulv
 
What to expect from Java 9
What to expect from Java 9What to expect from Java 9
What to expect from Java 9Ivan Krylov
 

Similar a Extending spring (20)

Extending Spring for Custom Usage
Extending Spring for Custom UsageExtending Spring for Custom Usage
Extending Spring for Custom Usage
 
Spring in the Cloud - using Spring with Cloud Foundry
Spring in the Cloud - using Spring with Cloud FoundrySpring in the Cloud - using Spring with Cloud Foundry
Spring in the Cloud - using Spring with Cloud Foundry
 
Play framework
Play frameworkPlay framework
Play framework
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Fact-Based Monitoring
Fact-Based MonitoringFact-Based Monitoring
Fact-Based Monitoring
 
Fact based monitoring
Fact based monitoringFact based monitoring
Fact based monitoring
 
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
J1 2015 "Debugging Java Apps in Containers: No Heavy Welding Gear Required"
 
Python Load Testing - Pygotham 2012
Python Load Testing - Pygotham 2012Python Load Testing - Pygotham 2012
Python Load Testing - Pygotham 2012
 
Writing Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason LeeWriting Plugged-in Java EE Apps: Jason Lee
Writing Plugged-in Java EE Apps: Jason Lee
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
 
Building Next-Gen Web Applications with the Spring 3 Web Stack
Building Next-Gen Web Applications with the Spring 3 Web StackBuilding Next-Gen Web Applications with the Spring 3 Web Stack
Building Next-Gen Web Applications with the Spring 3 Web Stack
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO Devs
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking Tour
 
Lo nuevo en Spring 3.0
Lo nuevo  en Spring 3.0Lo nuevo  en Spring 3.0
Lo nuevo en Spring 3.0
 
비동기 회고 발표자료
비동기 회고 발표자료비동기 회고 발표자료
비동기 회고 발표자료
 
인피니스팬데이터그리드따라잡기 (@JCO 2014)
인피니스팬데이터그리드따라잡기 (@JCO 2014)인피니스팬데이터그리드따라잡기 (@JCO 2014)
인피니스팬데이터그리드따라잡기 (@JCO 2014)
 
Enterprise Java Web Application Frameworks Sample Stack Implementation
Enterprise Java Web Application Frameworks   Sample Stack ImplementationEnterprise Java Web Application Frameworks   Sample Stack Implementation
Enterprise Java Web Application Frameworks Sample Stack Implementation
 
Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...
Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...
Developing Kafka Streams Applications with Upgradability in Mind with Neil Bu...
 
Plone FSR
Plone FSRPlone FSR
Plone FSR
 
What to expect from Java 9
What to expect from Java 9What to expect from Java 9
What to expect from Java 9
 

Más de Joshua Long

Bootiful Code with Spring Boot
Bootiful Code with Spring BootBootiful Code with Spring Boot
Bootiful Code with Spring BootJoshua Long
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring BootJoshua Long
 
the Spring Update from JavaOne 2013
the Spring Update from JavaOne 2013the Spring Update from JavaOne 2013
the Spring Update from JavaOne 2013Joshua Long
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with SpringJoshua Long
 
the Spring 4 update
the Spring 4 updatethe Spring 4 update
the Spring 4 updateJoshua Long
 
The spring 32 update final
The spring 32 update finalThe spring 32 update final
The spring 32 update finalJoshua Long
 
Integration and Batch Processing on Cloud Foundry
Integration and Batch Processing on Cloud FoundryIntegration and Batch Processing on Cloud Foundry
Integration and Batch Processing on Cloud FoundryJoshua Long
 
using Spring and MongoDB on Cloud Foundry
using Spring and MongoDB on Cloud Foundryusing Spring and MongoDB on Cloud Foundry
using Spring and MongoDB on Cloud FoundryJoshua Long
 
Spring in-the-cloud
Spring in-the-cloudSpring in-the-cloud
Spring in-the-cloudJoshua Long
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with SpringJoshua Long
 
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - EuropeThe Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - EuropeJoshua Long
 
A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom Joshua Long
 
Multi client Development with Spring
Multi client Development with SpringMulti client Development with Spring
Multi client Development with SpringJoshua Long
 
Spring Batch Behind the Scenes
Spring Batch Behind the ScenesSpring Batch Behind the Scenes
Spring Batch Behind the ScenesJoshua Long
 
Cloud Foundry Bootcamp
Cloud Foundry BootcampCloud Foundry Bootcamp
Cloud Foundry BootcampJoshua Long
 
Spring and Cloud Foundry; a Marriage Made in Heaven
Spring and Cloud Foundry; a Marriage Made in HeavenSpring and Cloud Foundry; a Marriage Made in Heaven
Spring and Cloud Foundry; a Marriage Made in HeavenJoshua Long
 
Using Spring's IOC Model
Using Spring's IOC ModelUsing Spring's IOC Model
Using Spring's IOC ModelJoshua Long
 
Enterprise Integration and Batch Processing on Cloud Foundry
Enterprise Integration and Batch Processing on Cloud FoundryEnterprise Integration and Batch Processing on Cloud Foundry
Enterprise Integration and Batch Processing on Cloud FoundryJoshua Long
 
a Running Tour of Cloud Foundry
a Running Tour of Cloud Foundrya Running Tour of Cloud Foundry
a Running Tour of Cloud FoundryJoshua Long
 

Más de Joshua Long (20)

Bootiful Code with Spring Boot
Bootiful Code with Spring BootBootiful Code with Spring Boot
Bootiful Code with Spring Boot
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring Boot
 
Boot It Up
Boot It UpBoot It Up
Boot It Up
 
the Spring Update from JavaOne 2013
the Spring Update from JavaOne 2013the Spring Update from JavaOne 2013
the Spring Update from JavaOne 2013
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
the Spring 4 update
the Spring 4 updatethe Spring 4 update
the Spring 4 update
 
The spring 32 update final
The spring 32 update finalThe spring 32 update final
The spring 32 update final
 
Integration and Batch Processing on Cloud Foundry
Integration and Batch Processing on Cloud FoundryIntegration and Batch Processing on Cloud Foundry
Integration and Batch Processing on Cloud Foundry
 
using Spring and MongoDB on Cloud Foundry
using Spring and MongoDB on Cloud Foundryusing Spring and MongoDB on Cloud Foundry
using Spring and MongoDB on Cloud Foundry
 
Spring in-the-cloud
Spring in-the-cloudSpring in-the-cloud
Spring in-the-cloud
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
 
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - EuropeThe Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
 
A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom
 
Multi client Development with Spring
Multi client Development with SpringMulti client Development with Spring
Multi client Development with Spring
 
Spring Batch Behind the Scenes
Spring Batch Behind the ScenesSpring Batch Behind the Scenes
Spring Batch Behind the Scenes
 
Cloud Foundry Bootcamp
Cloud Foundry BootcampCloud Foundry Bootcamp
Cloud Foundry Bootcamp
 
Spring and Cloud Foundry; a Marriage Made in Heaven
Spring and Cloud Foundry; a Marriage Made in HeavenSpring and Cloud Foundry; a Marriage Made in Heaven
Spring and Cloud Foundry; a Marriage Made in Heaven
 
Using Spring's IOC Model
Using Spring's IOC ModelUsing Spring's IOC Model
Using Spring's IOC Model
 
Enterprise Integration and Batch Processing on Cloud Foundry
Enterprise Integration and Batch Processing on Cloud FoundryEnterprise Integration and Batch Processing on Cloud Foundry
Enterprise Integration and Batch Processing on Cloud Foundry
 
a Running Tour of Cloud Foundry
a Running Tour of Cloud Foundrya Running Tour of Cloud Foundry
a Running Tour of Cloud Foundry
 

Extending spring

  • 1. Tailoring Spring for Custom Usage • Josh Long, SpringSource, a division of VMware 1 Thursday, January 24, 13
  • 2. About Josh Long (龙 之春) Spring Developer Advocate twitter: @starbuxman weibo: @springsource josh.long@springsource.com 2 Thursday, January 24, 13
  • 3. Agenda Explore the value of a framework Exploit some of the lesser known, but powerful, extension hooks in the core Spring framework QA 3 Thursday, January 24, 13
  • 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!) 4 Thursday, January 24, 13
  • 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 5 Thursday, January 24, 13
  • 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 6 Thursday, January 24, 13
  • 7. Spring Integration rules!! Not confidential. Tell everyone. 7 Thursday, January 24, 13
  • 8. ...but??!? ...what if it doesn’t do what I want? 8 Thursday, January 24, 13
  • 9. What is Spring Integration? FLEXIBLE v Not confidential. Tell everyone. 9 Thursday, January 24, 13
  • 11. do NOT reinvent the Wheel! 11 Thursday, January 24, 13
  • 12. The Open/Closed Principle "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” -Bob Martin 12 Thursday, January 24, 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 13 Thursday, January 24, 13
  • 14. Spring, a walking tour • Demos: – Bean*PostProcessor – AspectJ 14 Thursday, January 24, 13
  • 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. 15 Thursday, January 24, 13
  • 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 16 Thursday, January 24, 13
  • 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(); } 17 Thursday, January 24, 13
  • 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(); } 17 Thursday, January 24, 13
  • 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 } 17 Thursday, January 24, 13
  • 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 17 Thursday, January 24, 13
  • 21. Spring, a walking tour • Demos: – life cycle callbacks – scopes • using • creating your own 18 Thursday, January 24, 13
  • 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 19 Thursday, January 24, 13
  • 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 20 Thursday, January 24, 13
  • 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 = “#{ .. }” 21 Thursday, January 24, 13
  • 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 22 Thursday, January 24, 13
  • 26. Getting Beans from Strange Places • An ApplicationContextInitializer public interface ApplicationContextInitializer <C extends ConfigurableApplicationContext> { void initialize(C applicationContext); } 23 Thursday, January 24, 13
  • 27. Getting Beans from Strange Places • Demos: – FactoryBeans – SpEL – Profiles • ApplicationContextInitializers 24 Thursday, January 24, 13
  • 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() ; 25 Thursday, January 24, 13
  • 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(); } 26 Thursday, January 24, 13
  • 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; } 27 Thursday, January 24, 13
  • 31. Object to XML Marshallers • Demos: – a custom object-to-XML marshaller 28 Thursday, January 24, 13
  • 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; } 29 Thursday, January 24, 13
  • 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); 30 Thursday, January 24, 13
  • 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 31 Thursday, January 24, 13
  • 35. Registering a custom HttpMessageConverter @EnableWebMvc public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { } } 32 Thursday, January 24, 13
  • 36. REST • Demos: – Writing and using a customer HttpMessageConverter 33 Thursday, January 24, 13
  • 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. 34 Thursday, January 24, 13
  • 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; } 35 Thursday, January 24, 13
  • 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(); } 36 Thursday, January 24, 13
  • 40. Writing a custom View and View Resolver 37 Thursday, January 24, 13
  • 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 38 Thursday, January 24, 13
  • 42. Writing a custom View and View Resolver public interface ViewResolver { View resolveViewName(String viewName, Locale locale) throws Exception; } 39 Thursday, January 24, 13
  • 43. Writing a custom View and View Resolver public interface View { String RESPONSE_STATUS_ATTRIBUTE = View.class.getName() + ".responseStatus"; String getContentType(); void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception; } 40 Thursday, January 24, 13
  • 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() { ... } 41 Thursday, January 24, 13
  • 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 41 Thursday, January 24, 13
  • 46. Writing a custom View and View Resolver • Demo: writing a custom view/view resolver 42 Thursday, January 24, 13
  • 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); ... } 43 Thursday, January 24, 13
  • 48. Writing Adapters in Spring Integration 44 Thursday, January 24, 13
  • 49. Writing Adapters in Spring Integration • MessageSource for inbound adapters • MessageHandler for outbound adapters MessageSource MessageHandler 45 Thursday, January 24, 13
  • 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> 46 Thursday, January 24, 13
  • 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” /> 47 Thursday, January 24, 13
  • 52. Spring Integration File System Adapters • Spring Integration provides rich file system adapters – FTP, SFTP, FTPS, files in general – But... what about SMB/CIFS? 48 Thursday, January 24, 13
  • 53. Writing Readers and Writers in Spring Batch • ItemReader for inbound adapters • ItemWriters for outbound adapters 49 Thursday, January 24, 13
  • 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 50 Thursday, January 24, 13
  • 55. Q&A 51 © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission. Thursday, January 24, 13