Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.

Slides:



Advertisements
Similar presentations
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
Advertisements

Spring, Hibernate and Web Services 13 th September 2014.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
© 2005, Cornell University. Rapid Application Development using the Kuali Architecture (Struts, Spring and OJB) A Case Study Bryan Hutchinson
The Spring Framework: A brief introduction to Inversion of Control James Brundege
Intro to Spring CJUG - January What is Spring? “The Spring framework provides central transaction control of various objects.” This means that any.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring Framework Adam Waldal Senior Consultant. About me..  OPI is a leader in J2EE consulting with Relationships with BEA, IBM, Tibco, and many other.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Data Persistence and Intro to Hibernate.
UNIT-V The MVC architecture and Struts Framework.
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Introduction to the Spring Framework By: Nigusse A. Duguma Kansas State university Department of Computer Science Nov 20, 2007.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Introduction to AOP.
Introduction to the Spring Framework Rajesh. Spring Mission Statement J2EE should be easier to use OO design is more important than any implementation.
Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13.
OpusCollege and the use of Spring and iBatis
Parsley Introduction Kui Huang Oct. 13, Topics Background Dependency Injection Object Lifecycle Message Bus Sample FW Extensions.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
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.
Dependency Injection in Action Jan Västernäs. CADEC2006, DI, Slide 2 Copyright 2006, Callista Enterprise AB Agenda (DI=Dependency Injection) Background.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Opus College - overview. OpusCollege - background First project: ICT Capacity Building Mozambican Higher Education Institutions Partners: RUG Groningen,
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring Training 17/09/ Dependency Injection.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework,
Introduction to Web Dimitar Nenchev Ivan Nakov
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Persistence and Hibernate Aaron Zeckoski
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Sakai WebApp Structure
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
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.
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Introduction to Reasonable Server Faces Aaron Zeckoski
Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Comparison of Different AOP Approaches Presented by: Xiaojing Wang.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions and Spring MVC Buenos Aires, June 2009.
Dependency Injection with Guice Technion – Institute of Technology Author: Gal Lalouche - Technion 2016 ©
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
J2EE Lecture 6: Spring – IoC and Dependency Injection
MPCS – Advanced java Programming
CS520 Web Programming Spring – Inversion of Control
Sakai WebApp Structure
By Ru Shen Department of Computer Science UAlbany, 2008
Intro to Spring CJUG - January 2013.
Introduction to Spring Framework and Dependency Injection
Developing and testing enterprise Java applications
Leveraging ColdSpring To Make Better Applications
Designing For Testability
Aspect Oriented Programming
Presentation transcript:

Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework and Dependency Injection Aaron Zeckoski

2 Spring Framework A popular and stable Java application framework for enterprise developmentapplication framework –Ubiquitous for Java development –Well established in enterprise Java apps –Time tested and proven reliable A primary purpose is to reduce dependencies and even introduce negative dependencies –Different from almost every other framework out there –Part of the reason it has been adopted so quickly URL:

3 Spring code structure Spring code base is proven to be well structured (possibly the best) – Analysis using Structure 101Structure packages No dependency cycles

4 More Spring Considered an alternative / replacement for the Enterprise JavaBean (EJB) model Flexible –Programmers decide how to program Not exclusive to Java (e.g..NET) Solutions to typical coding busywork –JDBC –LDAP –Web Services URL:

5 What does Spring offer? Dependency Injection –Also known as IoC (Inversion of Control) Aspect Oriented Programming –Runtime injection-based Portable Service Abstractions –The rest of spring ORM, DAO, Web MVC, Web, etc. Allows access to these without knowing how they actually work

6 Dependency Injection defined Method to create needed dependencies or look them up somehow without doing it in the dependent code –Often called Inversion of Control (IoC) IoC injects needed dependencies into the object instead –Setters or Contructor Primary goal is reduction of dependencies in code –an excellent goal in any case –This is the central part of Spring URL:

7 Aspect Oriented Programming defined Attempts to separate concerns, increase modularity, and decrease redundancy –Separation of Concerns (SoC) Break up features to minimize overlap –Don’t Repeat Yourself (DRY) Minimize code duplication –Cross-Cutting Concerns Program aspects that affect many others (e.g. logging) AspectJ is the top AOP packageAspectJ –Java like syntax, IDE integration URL:

8 Portable Service Abstractions defined Services that easily move between systems without heavy reworking –Ideally easy to run on any system –Abstraction without exposing service dependencies LDAP access without knowing what LDAP is Database access without typical JDBC hoops Basically everything in Spring that is not IoC or AOP

9 What is a bean? Typical java bean with a unique id In spring there are basically two types –Singleton One instance of the bean created and referenced each time it is requested –Prototype (non-singleton) New bean created each time Same as new ClassName() Beans are normally created by Spring as late as possible

10 What is a bean definition? Defines a bean for Spring to manage –Key attributes class (required): fully qualified java class name id: the unique identifier for this bean configuration: (singleton, init-method, etc.) constructor-arg: arguments to pass to the constructor at creation time property: arguments to pass to the bean setters at creation time Collaborators: other beans needed in this bean (a.k.a dependencies), specified in property or constructor-arg Typically defined in an XML file

11 Sample bean definition 1 public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public void setBeanOne(AnotherBean beanOne) { this.beanOne = beanOne; } public void setBeanTwo(YetAnotherBean beanTwo) { this.beanTwo = beanTwo; } public void setIntegerProperty(int i) { this.i = i; } … }

12 What is a bean factory? Often seen as an ApplicationContext –BeanFactory is not used directly often –ApplicationContext is a complete superset of bean factory methods Same interface implemented Offers a richer set of features Spring uses a BeanFactory to create, manage and locate “beans” which are basically instances of a class –Typical usage is an XML bean factory which allows configuration via XML files

13 Beans are created in order based on the dependency graph –Often they are created when the factory loads the definitions –Can override this behavior in bean –You can also override this in the factory or context but this is not recommended Spring will instantiate beans in the order required by their dependencies 1.app scope singleton - eagerly instantiated at container startup 2.lazy dependency - created when dependent bean created 3.VERY lazy dependency - created when accessed in code How are beans created?

14 How are beans injected? A dependency graph is constructed based on the various bean definitions Beans are created using constructors (mostly no-arg) or factory methods Dependencies that were not injected via constructor are then injected using setters Any dependency that has not been created is created as needed

15 Multiple bean config files There are 3 ways to load multiple bean config files (allows for logical division of beans) –Load multiple config files from web.xml contextConfigLocation classpath:/WEB-INF/spring-config.xml, classpath:/WEB- INF/applicationContext.xml –Use the import tag –Load multiple config files using Resources in the application context constructor Recommended by the spring team Not always possible though ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] {"applicationContext.xml", "applicationContext-part2.xml"});

16 Bean properties? The primary method of dependency injection Can be another bean, value, collection, etc. This can be written in shorthand as follows

17 Anonymous vs ID Beans that do not need to be referenced elsewhere can be defined anonymously This bean is identified (has an id) and can be accessed to inject it into another bean This bean is anonymous (no id)

18 What is an inner bean? It is a way to define a bean needed by another bean in a shorthand way –Always anonymous (id is ignored) –Always prototype (non-singleton) Aaron 31

19 Bean init-method The init method runs AFTER all bean dependencies are loaded –Constructor loads when the bean is first instantiated –Allows the programmer to execute code once all dependencies are present <bean id="exampleBean" class=”org.example.ExampleBean" init-method=”init” /> public class ExampleBean { public void init() { // do something }

20 Bean values Spring can inject more than just other beans Values on beans can be of a few types –Direct value (string, int, etc.) –Collection (list, set, map, props) –Bean –Compound property Example of injecting a string value

21 Abstract (parent) beans Allows definition of part of a bean which can be reused many times in other bean definitions <bean id="abstractBean" abstract="true" class="org.example.ParentBean"> <bean id="childBean" class="org.example.ChildBean" parent="abstractBean" init-method="init">  The parent bean defines 2 values (name, age)  The child bean uses the parent age value (31)  The child bean overrides the parent name value (from parent-AZ to child-AZ)  Parent bean could not be injected, child could

22 AOP in Spring Provides way to create declarative services and custom aspects Transaction management is the most common aspect (or concern) Spring handles AOP via advisors or interceptors –Interception point is a joinpoint –A set of joinpoints are called a pointcut pointcuts are key to Spring AOP, they allow intercepts without explicit knowledge of the OO hierarchy –Action taken by an interceptor is called advice

23 AOP advice types Around –Most common and powerful –Execute code before and after joinpoint Before –Executes before joinpoint, cannot stop execution Throws –Executes code if exception is thrown After return –Executes code after normal joinpoint execution

24 Spring AOP key points Pure java implementation Allows method interception –No field or property intercepts yet AOP advice is specified using typical bean definitions –Closely integrates with Spring IoC Proxy based AOP –J2SE dynamic proxies or CGLIB proxies Not a replacement for AspectJ

25 Example transaction proxy PROPAGATION_REQUIRED This wraps a transaction interceptor around a DAO

26 Working example Let’s look at some example code pre and post spring –Simple application that allows a user to add, remove, and list a set of strings Pre spring code –Programmers Cafe - Example AppProgrammers Cafe - Example App Post spring code –Programmers Cafe - Example App SpringProgrammers Cafe - Example App Spring

27 Example App The example app is a simple command line Java app which is meant to demonstrate a reasonable dependency structure This app allows a user to save, delete, and list a set of strings associated with their username

28 Example App Structure Alpha is the main class Bravo handles user interaction Charlie handles application logic Delta handles data access Dependency graph is non-cyclical –No A => B => C => A Alpha Charlie Bravo Delta A B = A depends on B DeltaImpl

29 Non-spring version Involves using new to create needed dependencies Each class must know about the dependencies that it needs Singletons have to be created and handed to the classes that need them at the same time or you need a static way to access them (or a framework) Tightly coupled code structure

30 Spring version No more new use Classes only have to know about the interface –or class if no interface available Singletons easy to handle Loose coupling allows flexible changes

31 Questions? Spring framework –