26
Introduction to Spring Matt Wheeler

Matt Wheeler

  • Upload
    garron

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to Spring. Matt Wheeler. Notes. This is a training NOT a presentation Please ask questions Prerequisites Introduction to Java Stack Basic Java and XML skills. Overview. Learn the basics of the Spring Framework http://www.springsource.org/documentation. - PowerPoint PPT Presentation

Citation preview

Page 1: Matt Wheeler

Introduction to SpringMatt Wheeler

Page 2: Matt Wheeler

Notes

• This is a training NOT a presentation• Please ask questions• Prerequisites

– Introduction to Java Stack– Basic Java and XML skills

Page 3: Matt Wheeler

Overview

Learn the basics of the Spring Framework• http://www.springsource.org/documentation

Page 4: Matt Wheeler

Goals of the Spring Framework

• Simplify Java EE development• Solve problems not addressed by Java EE• Provide simple integration for best of breed

technologies

Page 5: Matt Wheeler

Defining Beans

• Define beans to be managed by Spring (IoC) container– Defined in xml (applicationContext.xml in the Stack)– Defined using annotations

• Basic configuration<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 definitions go here -->

</beans>

Page 6: Matt Wheeler

Defining beans

• Each bean should have– Unique name or preferrably id

<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=“whatever” class=“org.lds.whatever.BeanName” />

</beans>

Page 8: Matt Wheeler

Bean Scopes

• Where Spring starts to pay dividends• Sometimes you want to be able to control the life

of a bean– Bean scopes provide this ability

• By default all beans are singletons

Page 9: Matt Wheeler

Available Bean Scopes

Scope Documentation

singleton Creates a single bean instance per IoC container (default)

prototype Creates a new instance every time a bean of the type is requested

request Creates a single instance per HTTP request. Only valid in a web application context.

session Creates a single instance per HTTP session. Only valid in a web application context.

globalSession Creates a single instance per HTTP session. Only valid in a portal application.

Page 10: Matt Wheeler

Singleton Scope

• Remember these days

• The new world

public static synchronized MySingleton getInstance() { public static MySingleton getInstance() { if (_instance==null) { _instance = new MySingleton();   }   return _instance; }

<!– singleton is the default so these two definitions are equivalent -->

<bean id=“whatever” class=“org.lds.whatever.MyBean” />

<bean id=“whatever” class=“org.lds.whatever.MyBean” scope=“singleton” />

Page 11: Matt Wheeler

Prototype Scope

• Equivalent to calling new every time a an instance of a class is needed

• Spring does not manage he lifecycle of prototype bean

• The configuration is as follows:

<bean id=“whatever” class=“org.lds.whatever.MyBean” scope=“prototype” />

Page 12: Matt Wheeler

Web application scopes

• Without Spring you would have to manage bean creation and lifecycles manually

<bean id=“whatever” class=“org.lds.whatever.MyBean” scope=“request” />

<bean id=“whatever” class=“org.lds.whatever.MyBean” scope=“session” />

Page 14: Matt Wheeler

Advantages of Inversion of Control (IoC)

• Simplify component dependency and lifecycle management

• Injection eliminates need for:– Calling new – Looking up dependencies

• Decouples code from IoC container• Injection is easier – less code• Simplifies testing• Easier to maintain

Page 15: Matt Wheeler

Inversion of Control (IoC)

• Heart of Spring Framework is the IoC container• Two basic implementations of the IoC container

– ApplicationContext– BeanFactory– BeanFactory is stripped down version of

ApplicationContext• We will exclusively focus on ApplicationContext

Page 16: Matt Wheeler

Inversion of Control (IoC) the Concept

• Simplify component dependencies and lifecycle management– Dependency lookup vs. Dependency Injection

//dependency lookuppublic class Lookup{ private Bean bean;

public Bean findBean(Container container) { return (Bean) container.getBean(“someBean”); }}

//dependency injectionpublic class Injection { private Bean bean;

public void setBean(Bean bean) { this.bean = bean; }}

Page 17: Matt Wheeler

Dependency Injection (DI)

• Two basic types of injection– Setter injection– Constructor injection

Page 18: Matt Wheeler

DI (setter injection)

• Say we have to following Rabbit class

• Example

<bean id=“rabbit” class=“org.lds.farm.Rabbit”> <property name=“favoriteFood” value=“lettuce” /></bean>

public class Rabbit { private String favoriteFood;

public void setFavoriteFood(String favoriteFood) { this.favoriteFood = favoriteFood; }

public void printFavoriteFood() { System.out.println(favoriteFood); }}

Page 19: Matt Wheeler

DI (constructor injection)

• Say we have to following Rabbit class

• Example

public class Rabbit { private String favoriteFood;

public Rabbit(String favoriteFood) { this.favoriteFood = favoriteFood; }

public void printFavoriteFood() { System.out.println(favoriteFood); }}

<bean id=“rabbit” class=“org.lds.farm.Rabbit”> <constructor-arg value=“lettuce” /></bean>

Page 20: Matt Wheeler

DI (continued)

• Ability to inject many data types – Lists, Sets, Properties, Maps (most collection types)– Other beans

• Lets us look at a few examples:– http://static.springsource.org/spring/docs/3.0.x/sprin

g-framework-reference/html/beans.html#beans-collection-elements

Page 21: Matt Wheeler

DI Collections

• Say our rabbit has many favorite foodspublic class Rabbit { private Set<String> favoriteFoods;

public void setFavoriteFoods(List<String> favoriteFoods) { this.favoriteFoods = favoriteFoods; }

public void printFavoriteFood() { for (String favoriteFoods : favoriteFood) { System.out.println(favoriteFood); } }}

<bean id=“rabbit” class=“org.lds.farm.Rabbit”> <property name=“favoriteFoods”> <set>

<value>lettuce</value><value>carrot</value>

</set> </property></bean>

Page 22: Matt Wheeler

DI Bean References

• Lets expand our rabbit concept to an entire farm

• And then modify our rabbit class just a hair

public class Farm { private List<Rabbit> rabbits; public void setRabbits(List<Rabbit> rabbits) { this.rabbits = rabbits; }}

public class Rabbit { private String name; public Rabbit(String name) { this.name = name; } public void setName(String name) { this.name = name; } //…}

Page 23: Matt Wheeler

Bean Reference Examples

<bean id=“rabbit” class=“org.lds.model.Rabbit”> <property name=“name” value=“Bubba” /></bean>

<bean id=“farm” class=“org.lds.model.Farm”> <property name=“rabbits”>

<list> <ref bean=“rabbit” /> <!– anonymous inner bean -->

<bean class=“org.lds.model.Rabbit”><property name=“name” value=“Snowshoe” />

</bean> </list> </property></bean>

Page 24: Matt Wheeler

Another

public class Farm { private Rabbit prizeRabbit; public void setPrizeRabbit(Rabbit prizeRabbit) { this.prizeRabbit = prizeRabbit; }}

<bean id=“prize” class=“org.lds.model.Rabbit”> <property name=“name” value=“Queen Bee” /></bean>

<bean id=“farm” class=“org.lds.model.Farm”> <property name=“prizeRabbit” ref=“prize” /></bean>

Page 26: Matt Wheeler

Summary

• Info• More info