1 spring framework april, 2012 lam ho lam to. © 2010 cisco and/or its affiliates. all rights...

47
1 Spring Framework April, 2012 Lam Ho Lam To

Upload: leona-payne

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Spring Framework

April, 2012

Lam Ho

Lam To

Page 2: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 3: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 4: 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

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

Spring Overview• What is Spring ?

oSpring 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.

oSpring 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)

Page 5: 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

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

Spring Overview• Benefits of Using Spring Framework

oFlexible dependency injection with XML and annotation-based configuration styles, using POJOs.

oAdvanced support for aspect-oriented programming with proxy-based and AspectJ-based variants.

oSupport for declarative transactions, declarative caching, declarative validation, and declarative formatting.

oPowerful abstractions for working with common Java EE specifications such as JDBC, JPA, JTA and JMS.

oSpring'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.

oRich testing facilities for unit tests as well as for integration tests.

oDependency Injection (DI).

Page 6: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 7: 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

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

Framework Architecture

Page 8: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 9: 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

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

Environment Setup• 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

Page 10: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 11: 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

© 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

Page 12: 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

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

Aspect Oriented Programming (AOP)• That AOP is a framework is one of key components of

Spring Framework.

• Is AOP replacement for OOP or not?

Page 13: 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

© 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

Page 14: 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

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

Aspect Oriented Programming (AOP)• Some concepts of AOP:

Aspect: aspect modules

Jointpoint:

Advice:

Pointcut:

Page 15: 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

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

Aspect Oriented Programming (AOP)

Page 16: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 17: 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

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

Beanso Bean Definition

o Bean Scopes

o Bean Life Cycles

o Bean Definition Inheritance

o Bean Auto-Wiring

Page 18: 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

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

Bean Definition• 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.

Page 19: 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

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

Bean Definition

•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.

Page 20: 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

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

Bean DefinitionProperties Description

class This attribute is mandatory and specify the bean class to be used to create the bean.

name This attribute specifies the bean identifier uniquely.

scope This attribute specifies the scope of the objects created from a particular bean definition.

constructor-arg This is used to inject the dependencies.

property This is used to inject the dependencies.

autowire This is used to inject the dependencies

lazy-init A 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-method A callback to be called just after all necessary properties on the bean have been set by the container

destroy-method A callback to be used when the container containing the bean is destroyed.

Page 21: 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

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

Bean Definition• 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); }}

Page 22: 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

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

Bean Definition in XML• Example: Beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"> <property name="message" value="Hello World!"/> </bean></beans>

Page 23: 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

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

Bean Definition• 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(); }}

Page 24: 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

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

Bean Scopes• When defining a <bean> in Spring, you have the option of declaring a

scope for that bean.

• The default scope is always singleton.

<!-- A bean definition with singleton scope --><bean id="..." class="..." scope="singleton"> <!-- collaborators and configuration for this bean go here --></bean>

Page 25: 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

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

Bean ScopesScope Description

singleton This scopes the bean definition to a single instance per Spring IoC container (default).

prototype This scopes a single bean definition to have any number of object instances.

request This scopes a bean definition to an HTTP request.

session This scopes a bean definition to an HTTP session.

global-session This scopes a bean definition to a global HTTP session.

Page 26: 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

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

Bean Life Cycles• 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

Page 27: 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

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

Bean Life Cycles• 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"/>

Page 28: 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

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

Bean Life Cycles• 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"/>

Page 29: 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

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

Bean Life Cycles• 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> :

<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"> <bean id="..." class="..."> ... </bean></beans>

Page 30: 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

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

Bean Definition Inheritance• 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="helloWorld" class="com.tutorialspoint.HelloWorld"> <property name="message1" value="Hello World!"/> <property name="message2" value="Hello Second World!"/></bean>

<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld"> <property name="message1" value="Hello India!"/> <property name="message3" value="Namaste India!"/></bean>

Page 31: 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

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

Bean Definition Inheritance• Bean Definition Template

• Specify abstract attribute with a value of true.

• Should not specify class attribute.

<bean id="beanTeamplate" abstract="true"> <property name="message1" value="Hello World!"/> <property name="message2" value="Hello Second World!"/></bean>

<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="beanTeamplate"> <property name="message1" value="Hello India!"/> <property name="message3" value="Namaste India!"/></bean>

Page 32: 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

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

Agenda1.Spring Overview

2.Framework Architecture

3.Environment Setup

5.Beans6.Dependency Injection

4.AOP Framework

7.References

Page 33: 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

© 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.

Dependency Injection (Wiring)

Page 34: 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

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

Dependency Injection (Wiring)

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.

Page 35: 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

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

Dependency Injection (Wiring)

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

Page 36: 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

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

Dependency Injection (Wiring)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.

Page 37: 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

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

Dependency Injection (Wiring)• Constructor-based dependency injection: use constructor-arg element.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor"> <constructor-arg ref="spellChecker"/> </bean>

<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>

Page 38: 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

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

2. Dependency Injection (Wiring)• Constructor arguments resolution.

• Use order in the list.

• Use different types.

• Use index attribute.

<bean id="example" class="example.Example"> <constructor-arg ref="x"/> <constructor-arg ref="y"/></bean>

<bean id="example" class="example.Example"> <constructor-arg type="int" ref="x"/> <constructor-arg type="java.lang.String" ref="y"/></bean>

<bean id="example" class="example.Example"> <constructor-arg index="0" ref="x"/> <constructor-arg index="1" ref="y"/></bean>

Page 39: 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

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

Dependency Injection (Wiring)• Setter-based dependency injection: use property element and use ref

attribute for passing a reference to an object.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor"> <property name="spellChecker" ref="spellChecker"/> </bean>

<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>

Page 40: 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

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

Dependency Injection (Wiring)• 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 <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor"> <property name="spellChecker"> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </property> </bean>

</beans>

Page 41: 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

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

Auto-WiringMode Description

no This is default setting which means no autowiring and you should use explicit bean reference for wiring.

byName Autowiring 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.

byType Autowiring 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.

constructor Similar to byType, but type applies to constructor arguments.

autodetect Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

Page 42: 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

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

Auto-Wiring• Auto-wiring by name.

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byName"> </bean>

<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>

Page 43: 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

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

Auto-Wiring• Auto-wiring by type.

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byType"> </bean>

<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>

Page 44: 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

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

Auto-Wiring• Auto-wiring by constructor.

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="constructor"> </bean>

<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>

Page 45: 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

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

Auto-Wiring• Auto-wiring by constructor.

<beans xmlns="http://www.springframework.org/schema/beans"...>

<bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="constructor"> </bean>

<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean></beans>

Page 46: 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

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

Reference• http://www.tutorialspoint.com/spring/

Page 47: 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

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