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.

Slides:



Advertisements
Similar presentations
Spring, Hibernate and Web Services 13 th September 2014.
Advertisements

Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Enterprise Application Development Sadegh Aliakbary An Introduction to Spring Framework www. JavaCup.ir.
Aspect-Oriented Programming In Eclipse ® Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January.
Spring.Net Steinar Dragsnes steinar.dragsnes at viz.no.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
1 November 21st 2009 Shaun Abram An Introduction to Spring.
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Struts 2.0 an Overview ( )
Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Spring Roo CS476 Aleksey Bukin Peter Lew. What is Roo? Productivity tool Allows for easy creation of Enterprise Java applications Runs alongside existing.
Intermediate Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
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.
Spring. Spring Overview Spring Container Concepts Spring and AOP Spring and Data Access Managing Transactions and Resources Remoting and Accessing Enterprise.
The Spring Framework Training Get to know Spring Framework Rohit Prabhakar
Introduction to the Spring Framework Rajesh. Spring Mission Statement J2EE should be easier to use OO design is more important than any implementation.
1 3. Implementing Web Services 1.Create SOAP proxy interfaces and WSDL based service descriptions 2.Register/publish services 3.Stores service descriptions.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
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.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
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.
© 2006 IBM Corporation IBM WebSphere Portlet Factory Architecture.
Text 16 de mayo de 2009 Spring Framework Part I. Dependency Injection Buenos Aires, May 2009.
PicoContainer Presented by: Jim O’Hara Ed Kausmeyer Jingming Zhang.
Copyright © 2005 Finetix LLC All Rights Reserved 0 SpringFramework.Net Developer Session Chris Donnan The Peer Frameworks Series -.Net and Java.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
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.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
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,
Tu sca ny 1 Extending The Tuscany Java SCA Runtime 21 August 2006.
Introduction to Web Dimitar Nenchev Ivan Nakov
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Spring Framework. About spring Spring is the most popular application development framework for enterprise Java. Millions of developers around the world.
Chính phủ điện tử TS. Phạm Văn Tính Khoa CNTT, ĐH Nông Lâm TP.HCM
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
JAVA EE 6 Best Practices for Migrating Spring to WTF ?!?
Dependency Injection JAVA EE - Spring Framework. Inner Beans As you know Java inner classes are defined within the scope of other classes, similarly,
SCA and Java n SCA provides support for multiple Java-based component implementation types l Java POJOs (implementation.java) l Spring Beans (implementation.spring)
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.
SwE 455 Tutorial. What is Evolve? Evolve is a tool for defining components and connecting them together to form new components. Also provides a runtime.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Introduction – ORM, Helloworld Application
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.
Introduction to Inversion Of Control (IOC). IOC Definition (based on Wikipedia)  Consider the way in which an object obtains references to its dependencies.
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
J2EE Lecture 6: Spring – IoC and Dependency Injection
CS520 Web Programming Spring – Inversion of Control
Inversion of Control and ColdFusion: Using ColdSpring
By Ru Shen Department of Computer Science UAlbany, 2008
Introduction to Spring Framework and Dependency Injection
Leveraging ColdSpring To Make Better Applications
Presentation transcript:

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 Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 3 1.Spring Overview 2.Framework Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 4 What is Spring ? o Spring framework is an open source Java platform and it was initially written by Rod Johnson and was first released under the Apache 2.0 license in June o Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB. 2 Things: o An Inversion of Control (IoC) Container o Utilities that provide a consistent (and simple!) API to many other technologies (JDBC, ORM, AOP, Declarative Transactions, etc)

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 5 Benefits of Using Spring Framework o Flexible dependency injection with XML and annotation-based configuration styles, using POJOs. o Advanced support for aspect-oriented programming with proxy-based and AspectJ- based variants. o Support for declarative transactions, declarative caching, declarative validation, and declarative formatting. o Powerful abstractions for working with common Java EE specifications such as JDBC, JPA, JTA and JMS. o Spring's web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks such as Struts or other over engineered or less popular web frameworks. o Rich testing facilities for unit tests as well as for integration tests. o Dependency Injection (DI).

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 6 1.Spring Overview 2.Framework Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 7

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 8 1.Spring Overview 2.Framework Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 9 JDK and Eclipse IDE. antlr-runtime org.springframework.aop M2 org.springframework.asm M2 org.springframework.aspects M2 org.springframework.beans M2 org.springframework.context.support M2 org.springframework.context M2 org.springframework.core M2 org.springframework.expression M2 commons-logging-1.1.1

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 10 1.Spring Overview 2.Framework Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 11 Show student Info Register Security Transaction Logging Security Transaction Logging Duplication of code Try/catch and logging Aspect modules Register Show Info AOP

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 12 That AOP is a framework is one of key components of Spring Framework. Is AOP replacement for OOP or not?

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 13 Show student Info Register Security Logging Aspect modules Beans AOP Transaction Show Info Transaction Register

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 14 Some concepts of AOP:  Aspect: aspect modules  Jointpoint:  Advice:  Pointcut:

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 15

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 16 1.Spring Overview 2.Framework Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 17 o Bean Definition o Bean Scopes o Bean Life Cycles o Bean Definition Inheritance o Bean Auto-Wiring

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 18 Spring IoC Containers The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete lifecycle from creation till destruction. The Spring container uses dependency injection (DI) to manage the components that make up an application. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 19 The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application. The objects that are managed by IoC container are called beans. These beans are created with the configuration metadata.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 20 PropertiesDescription classThis attribute is mandatory and specify the bean class to be used to create the bean. nameThis attribute specifies the bean identifier uniquely. scopeThis attribute specifies the scope of the objects created from a particular bean definition. constructor-argThis is used to inject the dependencies. propertyThis is used to inject the dependencies. autowireThis is used to inject the dependencies lazy-initA lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup (true or false). init-methodA callback to be called just after all necessary properties on the bean have been set by the container destroy-methodA callback to be used when the container containing the bean is destroyed.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 21 Example: HelloWorld.java package com.tutorialspoint; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void printMessage(){ System.out.println("Your Message : " + message); }

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 22 Example: Beans.xml <beans xmlns=" xmlns:xsi=" xsi:schemaLocation="

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 23 Example: MainApp.java Out-put: Your Message : Hello World! package com.tutorialspoint; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class MainApp { public static void main(String[] args) { ApplicationContext context= new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.printMessage(); }

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 24 When defining a in Spring, you have the option of declaring a scope for that bean. The default scope is always singleton.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 25 ScopeDescription singletonThis scopes the bean definition to a single instance per Spring IoC container (default). prototypeThis scopes a single bean definition to have any number of object instances. requestThis scopes a bean definition to an HTTP request. sessionThis scopes a bean definition to an HTTP session. global-sessionThis scopes a bean definition to a global HTTP session.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 26 When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required. o Initialization callbacks o Destruction callbacks

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 27 Initialization callbacks  Implement interface: org.springframework.beans.factory.InitializingBean Method: void afterPropertiesSet() throws Exception;  Use "init-method" attribute: and declare method "init" (without parameters) in class examples.ExampleBean. <bean id="exampleBean" class="examples.ExampleBean" init-method="init"/>

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 28 Destruction callbacks  Implement interface: org.springframework.beans.factory.DisposableBean Method: void destroy();  Use "init-method" attribute: and declare method "destroy" (without parameters) in class examples.ExampleBean. <bean id="exampleBean" class="examples.ExampleBean" destroy-method="destroy"/>

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 29 Default initialization and destroy methods If you have too many beans having initialization and or destroy methods with the same name, you can use default-init-method and default- destroy-method attributes on the : <beans xmlns=" xmlns:xsi=" default-init-method="init" default-destroy-method="destroy">...

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 30 A child bean definition inherits configuration data from a parent definition. The child definition can override some values, or add others, as needed. Using the parent attribute, specifying the parent bean as the value of this attribute: <bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 31 Bean Definition Template Specify abstract attribute with a value of true. Should not specify class attribute. <bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="beanTeamplate">

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 32 1.Spring Overview 2.Framework Architecture 3.Environment Setup 5.Beans 6.Dependency Injection 4.AOP Framework 7.References

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 33 The technology that Spring is most identified with is the Dependency Injection (DI) flavor of Inversion of Control. The Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways and Dependency Injection is merely one concrete example of Inversion of Control. When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while doing unit testing. Dependency Injection helps in gluing these classes together and same time keeping them independent.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 34 public class TextEditor { private SpellChecker spellChecker; public TextEditor() { spellChecker = new SpellChecker(); } Normal Consider you have an application which has a text editor component and you want to provide spell checking.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 35 public class TextEditor { private SpellChecker spellChecker; public void setSpellChecker(SpellChecker spellChecker){ this.spellChecker = spellChecker; } Property-based DI public class TextEditor { private SpellChecker spellChecker; public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } Constructor-based DI

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 36 Dependency Injection Type Description Constructor-based dependency injection Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class. Setter-based dependency injection Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no- argument constructor or no-argument static factory method to instantiate your bean.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 37 Constructor-based dependency injection: use constructor-arg element. <beans xmlns="

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 38 Constructor arguments resolution. Use order in the list. Use different types. Use index attribute.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 39 Setter-based dependency injection: use property element and use ref attribute for passing a reference to an object. <beans xmlns="

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 40 Spring Injecting Inner Beans As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a element inside the or elements is called inner bean. <beans xmlns="

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 41 ModeDescription noThis is default setting which means no autowiring and you should use explicit bean reference for wiring. byNameAutowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. byTypeAutowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown. constructorSimilar to byType, but type applies to constructor arguments. autodetectSpring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 42 Auto-wiring by name. <beans xmlns=" <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byName">

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 43 Auto-wiring by type. <beans xmlns=" <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byType">

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 44 Auto-wiring by constructor. <beans xmlns=" <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="constructor">

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 45 Auto-wiring by constructor. <beans xmlns=" <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="constructor">

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 46

© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 47