Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.

Slides:



Advertisements
Similar presentations
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Advertisements

Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Integrating Servlets and JavaServer Pages Vijayan Sugumaran School of Business Administration Oakland University Parts of this presentation provided by.
Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
LDS Account and the Java Stack. Disclaimer This is a training NOT a presentation. – Be prepared to learn and participate in labs Please ask questions.
Servlets Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
The Spring Framework: A brief introduction to Inversion of Control James Brundege
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
UNIT-V The MVC architecture and Struts Framework.
Lecture 2 - Struts ENTERPRISE JAVA. 2 Contents  Servlet Deployment  Servlet Filters  Model View Controllers  Struts  Dependency Injection.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
WebWork in Action An introduction to WebWork Patrick A. Lightbody.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Internationalization and the Java Stack Part 1 Matt Wheeler.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Internationalization and the Java Stack Matt Wheeler.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Parsley Introduction Kui Huang Oct. 13, Topics Background Dependency Injection Object Lifecycle Message Bus Sample FW Extensions.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
Introduction to Spring (part 2) Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Standalone Java Application vs. Java Web Application
Internationalization in the Java Stack Matt Wheeler.
CGS – 4854 Summer 2012 Web Site Construction and Management Instructor: Francisco R. Ortega Chapter 2.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
Internationalization in the Java Stack Matt Wheeler.
MCS 270 Spring 2014 Object-Oriented Software Development.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework,
WAM and the Java Stack. Disclaimer Please ask questions There are hands on labs Prerequisites: – Basic Java knowledge – Basic Spring knowledge – LDS Account.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Servlets Part 3. Topics Session Tracking ServletToServletCommunication-Servlet Chaining ServerSideIncludes AppletToServlet.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
INTEGRATION OF BACKBONE.JS WITH SPRING 3.1. Agenda New Features and Enhancements in Spring 3.1 What is Backbone.js and why I should use it Spring 3.1.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
Introduction to Servlets Allen Day. Notes This is a training NOT a presentation Please ask questions Prerequisites.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
1 Spring Framework April, 2012 Lam Ho Lam To. © 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2 1.Spring Overview 2.Framework.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
Java Servlets and Java Server Pages
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Impala A dynamic module framework for Java web development Phil Zoio Realsolve Solutions Devoxx 12 December, 2008.
2006/2007 Licence Apache 2.0 Castle.Igloo. Castle Igloo Basics Pre-require Concept Scopes PageFlow Configuration Controller View Exemple Castle.Igloo.
LDS Account and the Java Stack. Disclaimer This is a training NOT a presentation. – Be prepared to learn and participate in labs Please ask questions.
Chapter 4 Request and Response. Servlets are controlled by the container.
Chapter 14 Using JavaBeans Components in JSP Documents.
Presented By:. What is JavaHelp: Most software developers do not look forward to spending time documenting and explaining their product. JavaSoft has.
WAM and the Java Stack. Disclaimer This is a training NOT a presentation. – Be prepared to learn and participate in labs Please ask questions Prerequisites:
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Intermediate Spring Matt Wheeler.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Internationalization
Java Servlets By: Tejashri Udavant..
Corso di “Sviluppo di applicazioni Web”
Introduction to Java Servlets
Presentation transcript:

Intermediate Spring Matt Wheeler

Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic Java and XML skills – Installed LdsTech IDE (or other equivalent – good luck there ;)

Review Bean lifecycle XML Schema-based Configuration (namespace handlers) Lifecycle hooks Bean Initialization (JSR …) Bean post processors Component scanning Spring Component Annotations DI Annotations

Overview Advanced Injection Spring EL Additional Injection Annotations Providers Application Context web integration Testing framework

Spring EL (SpEL) Allows access to Spring beans and properties Supports querying and manipulating object graph at runtime Similar syntax to Unified EL (but more extensive) – Method invocation, string templating Namespace handlers use it to inject references into attributes For more specifics, please see the Spring docs: – g-framework-reference/html/expressions.html

Spring EL Examples We recommend only using this when necessary – For example When extracting a property from a map Or injecting a reference into a namespace handler <data-source driver-class="org.h2.Driver" url="jdbc:h2:mem:stack-starter;MODE=Oracle" user="#{databaseUsername}" password="#{databasePassword}" override="true" db-env="EMBEDDED"/>

Additional Injection Annotations Many additional injection annotations Please refer to the Spring documentation here: – ng-framework-reference/html/beans.html#beans- annotation-config AnnotationExampleDescription private String something Spring EL can be used in correlation to inject a property private SomeBean someBean; Injects by name instead of type. Slight differences Used for injecting Collections. If name not explicitly specified it uses the name of the property being private SomeBean someBean; Spring proprietary annotation almost equivalent to JSR but with a required attribute.

Providers Providers allow us to defer instantiation or accessing a resource until it is accessed Providers facilitate (from the JavaDoc): – Retrieving multiple instances – Lazy or optimal retrieval of an instance – Breaking circular dependencies – Abstracting scope so you can look up an instance in a smaller scope from an instance in a containing scope

Previous Training Lab We used the Named annotation to select the prototypeRabbit to inject into the farm as the prize rabbit The result was something like the following Does anyone see any problem with public class private Provider prizeRabbit; … }

Provider Demo DEMO

Lab 1: Providers _1_Advanced_Injection

Web Context Listener Loading application context in a web environment

Traditionally Previously we have loaded application contexts with something like: In a web environment however – You will want the context to automatically be loaded on startup – And be shared across the entire application ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBean someBean = context.getBean(SomeBean.class);

Servlet Listeners The Java Servlet spec provides a listener (startup hook) – Triggers these listeners to run on startup Spring utilized this functionality and created a listener – Will load the application context on start up

Context Loader Listener Specifically the following configures this listener in web.xml: And utilizes the following context parameter org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:META- INF/spring/applicationContext.xml,classpath:*beans.xml classpath:anotherContext.xml

Application Contexts and Servlets Servlets not instantiated by Spring – Instantiated by the servlet container – Spring unable to inject dependencies However Spring provides a way to access the application context

Application Context and Servlet For the given servlet configuration (web.xml) Application Context accessed as follows: servlet org.lds.training.TrainingServlet servlet /servlet public class TrainingServlet extends HttpServlet protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Context loader listener stores context in the servlet context - which is why it is required ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); SomeBean someBean = (SomeBean) applicationContext.getBean(SomeBean.class); someBean.printSomething(); }

A Better Way Spring provides a servlet that delegates to a bean that is Spring managed – Allowing annotations and injection – Called an HttpRequestHandler – Create a Spring bean that matches the name of the servlet name This provides the mapping between the two

Utilizing a Spring Request Handler The configuration: trainingHandler org.springframework.web.context.support.HttpRequestHandlerServlet trainingHandler /trainingHandler //Java Implementations public class TrainingRequestHandler implements HttpRequestHandler private SomeBean someBean; public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { someBean.printSomething(); }

Spring MVC and Application Contexts Spring MVC provides and even better way to integrate with the web container – Look forward to further discussion of this in a future training

Lab 2: Web Context Listener _2_Web_Context_Listener

Spring Testing Spring promotes testing in two prominent ways – Making the code more testable – Proving a testing framework

Testable Code Dependency injection decouples code from the container – POJOs are normally easier to test Dependencies are clearly defined

Testing Help Not to mention we have an application context that can inject dependencies But reloading the context for each test would not be performant Spring provides support to load the context once per JVM (unless you explicitly tell it to reload)

Testing Framework Many good testing frameworks available – Junit – TestNG – Living up the their stated goal they have not written their own just made integration with existing frameworks easy Spring provides test context classes that can be extended for the given test providers You can specify application contexts to be loaded

Spring Test – Allows you to specify application contexts to load – Important: Loaded in the order specified Searches last loaded first for a match and continues up chain until a matching bean is found Allows us to utilize everything in the project context – But override anything in the test public class FarmIT extends AbstractTestNGSpringContextTests { //… }

For example Regular application context Test application context

Another example Regular application context Test application context <stack-db:data-source driver-class="org.h2.Driver" url="jdbc:h2:mem:stack-starter;MODE=Oracle" user="" password="" override="true" db-env="EMBEDDED"/>

Lab 3: Spring Testing Integration _3_Spring_Testing_Integration

Credit where credit is due Spring Recipies 2 nd Edition (Gary Mak, Josh Long and Daniel Rubio)