Spring Fling
Total Page:16
File Type:pdf, Size:1020Kb
Spring Fling Phillip Warner Spring Framework ● Dependency Injection (DI) Framework – “Inversion of Control” ● Facilitates Good Programming Practices ● Facilitates Testing ● Unobtrusive ● Simplifies J2EE/Java EE Goals ● Let's see some code – Code examples ● Deployment through Spring configuration – XML – Spring configuration examples ● Interesting potential? – What might you do with Spring? Example 1 – StdoutPrinter ● StdoutPrinter.java – prints to STDOUT ● spring-config.xml – Spring configuration (by setter) ● SpringLoader.java – Spring container Loader ● spring-config-constructor.xml – Spring configuration example (by constructor) Example 2 – SimpleCalculator ● Operator.java – interface for defining operations ● Add.java, Subtract.java implementations of Operator interface ● Calculator.java – Calculator interface ● SimpleCalculator.java – Calculator implementation. ● SpringLoader.java and AutoSpringLoader.java – Two ways to run the code Example – VOEvent ● What is VOEvent? – IVOA standard for building and sending XML-based event notices (e.g., SNe, g-ray Bursts, Moving objects) – VOEvent is a broker developed by NOAO ● Provides event publishing, persistence, classification ● Provides event subscription mechanism Example – VOEvent ● Basic configuration for: – Server/broker ● Publishing, persistence, classification, relay (sending events), and subscription service – Client (author) ● Author events for publication – Client (subscriber) ● Subscription to TCP-based event streams VOEvent – Classification ● Rules-based ● Spring-configured – For initial implementation—before a database backend and supporting services are provided VOEvent – Classification (2) VOEvent – Classification (3) VOEvent – Classification (4) Example – Dynamic Workflows Example – Dynamic Workflows (2) Spring – Other Features ● Autowiring – Automatic dependency injection ● byType, byName, constructor, autodetect ● Bean scopes – singleton (default), prototype – request, session, global-session (web) – custom Spring – Other Features ● Annotation-based – Used with auto-wiring, e.g., ● @Autowired – tell Spring where the dependency should be injected ● @Resource – (JSR-250) resource injection a la Java EE 5 and 6 ● @PostConstruct, @PreDestroy (JSR-250) – could be used in legacy or code-based resource allocation – See www.springframework.org for more details Spring – Schema-based Configuration ● <util:.... /> – Provides for static definition of: ● Configuration (properties, etc.) ● Collections ● constants – simplifies defining static information – alternative? ● define a bean for each set of information Spring – Schema-based Configuration ● <jee:... /> – Provides for configuration of some Java Enterprise features ● JNDI lookup ● EJB lookup ● <jms:... /> – Provides for JMS support ● <tx:... /> – Support for Spring's declarative TX management Spring – Schema-based Configuration ● <aop:... /> – Support for AOP (Spring-based) ● <context:... /> – Further support for context configuration ● Spring context, e.g., ApplicationContext ● <lang:... /> – Provides support for dynamic languages ● Currently JRuby, Groovy, BSH – Provides support for inlining code Spring Projects http://www.springframework.org/projects And so on... For more details, see: http://www.springframework.org Spring Fling Phillip Warner 1 Spring Framework ● Dependency Injection (DI) Framework – “Inversion of Control” ● Facilitates Good Programming Practices ● Facilitates Testing ● Unobtrusive ● Simplifies J2EE/Java EE 2 DI entails writing JavaBeans with accessors for dependent classes (or rather, their interface). This removes implementation-specific dependence from code. Using Spring, the programmer can specify any given implementation of the interface for injection into the bean. Injection is through what is called the Spring configuration file, an XML file. No hard-coded instantiation allows for easier unit testing with mock objects. Dependent upon the framework to supply the implementation. Spring is as unobtrusive as you make it. Simplifies JEE by allowing you to write your business classes without requiring you to put JEE-specific code in your code. Specify and inject the JEE- dependence in the Spring configuration file. Goals ● Let's see some code – Code examples ● Deployment through Spring configuration – XML – Spring configuration examples ● Interesting potential? – What might you do with Spring? 3 Example 1 – StdoutPrinter ● StdoutPrinter.java – prints to STDOUT ● spring-config.xml – Spring configuration (by setter) ● SpringLoader.java – Spring container Loader ● spring-config-constructor.xml – Spring configuration example (by constructor) 4 Example 2 – SimpleCalculator ● Operator.java – interface for defining operations ● Add.java, Subtract.java implementations of Operator interface ● Calculator.java – Calculator interface ● SimpleCalculator.java – Calculator implementation. ● SpringLoader.java and AutoSpringLoader.java – Two ways to run the code 5 Example – VOEvent ● What is VOEvent? – IVOA standard for building and sending XML-based event notices (e.g., SNe, g-ray Bursts, Moving objects) – VOEvent is a broker developed by NOAO ● Provides event publishing, persistence, classification ● Provides event subscription mechanism 6 Example – VOEvent ● Basic configuration for: – Server/broker ● Publishing, persistence, classification, relay (sending events), and subscription service – Client (author) ● Author events for publication – Client (subscriber) ● Subscription to TCP-based event streams 7 Describe VOEvent – Classification ● Rules-based ● Spring-configured – For initial implementation—before a database backend and supporting services are provided 8 VOEvent – Classification (2) 9 VOEvent – Classification (3) 10 VOEvent – Classification (4) 11 Example – Dynamic Workflows 12 Example – Dynamic Workflows (2) 13 14 Spring – Other Features ● Autowiring – Automatic dependency injection ● byType, byName, constructor, autodetect ● Bean scopes – singleton (default), prototype – request, session, global-session (web) – custom 15 Spring – Other Features ● Annotation-based – Used with auto-wiring, e.g., ● @Autowired – tell Spring where the dependency should be injected ● @Resource – (JSR-250) resource injection a la Java EE 5 and 6 ● @PostConstruct, @PreDestroy (JSR-250) – could be used in legacy or code-based resource allocation – See www.springframework.org for more details 16 Spring – Schema-based Configuration ● <util:.... /> – Provides for static definition of: ● Configuration (properties, etc.) ● Collections ● constants – simplifies defining static information – alternative? ● define a bean for each set of information 17 Spring – Schema-based Configuration ● <jee:... /> – Provides for configuration of some Java Enterprise features ● JNDI lookup ● EJB lookup ● <jms:... /> – Provides for JMS support ● <tx:... /> – Support for Spring's declarative TX management 18 Spring – Schema-based Configuration ● <aop:... /> – Support for AOP (Spring-based) ● <context:... /> – Further support for context configuration ● Spring context, e.g., ApplicationContext ● <lang:... /> – Provides support for dynamic languages ● Currently JRuby, Groovy, BSH – Provides support for inlining code 19 Spring Projects http://www.springframework.org/projects 20 And so on... For more details, see: http://www.springframework.org 21 .