Download presentation
Presentation is loading. Please wait.
Published byLeona Payne Modified over 9 years ago
1
1 Spring Framework April, 2012 Lam Ho Lam To
2
© 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
3
© 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
4
© 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 2003. 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)
5
© 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).
6
© 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
7
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 7
8
© 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
9
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 9 JDK and Eclipse IDE. antlr-runtime-3.0.1 org.springframework.aop-3.1.0.M2 org.springframework.asm-3.1.0.M2 org.springframework.aspects-3.1.0.M2 org.springframework.beans-3.1.0.M2 org.springframework.context.support-3.1.0.M2 org.springframework.context-3.1.0.M2 org.springframework.core-3.1.0.M2 org.springframework.expression-3.1.0.M2 commons-logging-1.1.1
10
© 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
11
© 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
12
© 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?
13
© 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
14
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 14 Some concepts of AOP: Aspect: aspect modules Jointpoint: Advice: Pointcut:
15
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 15
16
© 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
17
© 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
18
© 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.
19
© 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.
20
© 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.
21
© 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); }
22
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 22 Example: Beans.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
23
© 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(); }
24
© 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.
25
© 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.
26
© 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
27
© 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"/>
28
© 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"/>
29
© 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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"... default-init-method="init" default-destroy-method="destroy">...
30
© 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">
31
© 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">
32
© 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
33
© 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.
34
© 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.
35
© 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
36
© 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.
37
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 37 Constructor-based dependency injection: use constructor-arg element. <beans xmlns="http://www.springframework.org/schema/beans"...>
38
© 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.
39
© 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="http://www.springframework.org/schema/beans"...>
40
© 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="http://www.springframework.org/schema/beans"...>
41
© 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.
42
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 42 Auto-wiring by name. <beans xmlns="http://www.springframework.org/schema/beans"...> <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byName">
43
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 43 Auto-wiring by type. <beans xmlns="http://www.springframework.org/schema/beans"...> <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byType">
44
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 44 Auto-wiring by constructor. <beans xmlns="http://www.springframework.org/schema/beans"...> <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="constructor">
45
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 45 Auto-wiring by constructor. <beans xmlns="http://www.springframework.org/schema/beans"...> <bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="constructor">
46
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 46 http://www.tutorialspoint.com/spring/
47
© 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 47
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.