Introduction to Spring Matt Wheeler
Feb 22, 2016
Introduction to SpringMatt Wheeler
Notes
• This is a training NOT a presentation• Please ask questions• Prerequisites
– Introduction to Java Stack– Basic Java and XML skills– Installed LdsTech IDE (or other equivalent – good luck
there ;)
Overview
• Become familiar with Spring documentation• http://www.springsource.org/documentation
• Learn basics of the Spring architecture• Learn about bean definition and creation• Learn about the application context
• Inversion of Control (IoC)• Dependency Injection (DI)
• Learn about bean scopes
Goals of the Spring Framework
• Simplify Java EE development• Solve problems not addressed by Java EE• Provide simple integration for best of breed
technologies• Provide modular/pluggable architecture
– Use what you want – don’t use what you don’t• http://www.springsource.org/about
Explore the Spring Ecosystem
• Main Page: http://www.springsource.org/• Documentation:
http://www.springsource.org/documentation• Forum: http://forum.springsource.org/• Jira:
https://jira.springsource.org/secure/Dashboard.jspa
Spring Framework
Modified from http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/images/spring-overview.png
Data Access Integration
• Data Access Integration layer includes:– JDBC (abstraction layer over native JDBC)– ORM (integration support for JPA, Hibernate, …)– Transaction support (declarative and programmatic)
Web
• Basic web integration features– File upload– Initialization of IoC container using servlet listeners– Contains Spring’s model view controller (MVC)
implementation
Test
• Test module provides integration with test frameworks– JUnit– TestNG
• Provides ability to load test specific ApplicationContexts
• Also provides helpful mock objects
Core Container
• Core and Beans modules provide framework fundamentals– Inversion of Control (IoC) and Dependency Injection
(DI)– BeanFactory provides factory pattern implementation
for creating Java objects– Allows decoupling of configuration and dependencies
from actual code
Spring Container / Application Context
• Spring container / application context contains instances of objects
• Spring managed object instances are called beans• Spring manages the creation, configuration, and destruction of
these beans• Beans are defined for use in the application context
– The definitions are a template for creating new bean instances• Bean definitions can be provided to Spring:
– In xml– Using annotations– In Java code
Defining Beans
<?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 class="org.lds.training.SomeBean" />
<!-- more bean definitions go here --> </beans>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");SomeBean someBean = context.getBean(SomeBean.class);someBean.callMethod();
• Bean definition (beans.xml)
• Application Context
Defining beans
• Each bean has a unique id– If none is provided, Spring will create one– Class should contain fully qualified package and name
of the object<?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="someBean" class="org.lds.training.SomeBean" />
<!-- more bean definitions go here --> </beans>
Defining Beans
<?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">
<!– defines an id by which the bean can be uniquely referenced --> <bean id="someBean" class="org.lds.training.SomeBean" />
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");SomeBean someBean = context.getBean("someBean", SomeBean.class);someBean.callMethod();
• Bean definition (beans.xml)
• Application Context
Lab 1: Bean Creation
https://tech.lds.org/wiki/Introduction_to_Spring#Lab_1_Bean_Configurati
on
Dependency Injection
Dependency Injection (DI)&
Inversion of Control (IoC)
Object Dependencies
• This is where your Spring investment starts to pay dividends• Most objects cannot do much by themselves
– They depend on other objects to accomplish some tasks– For instance if an object wanted to insert something into a
database it will need a connection to accomplish this• Most objects have properties that should be configurable
– For example if you have an object that times out after a certain period, you might want to be able to configure the timeout period
• Additionally, what if two objects need the same object dependency– For instance a data source might be used by many objects to persist data
Inversion of Control (IoC) the Concept
• Container injects dependencies when (or right after) the bean is created
• Inverse of bean controlling instantiation and/or location of its dependencies
• Objects instead obtain dependencies through– Constructor arguments– Setter arguments
Inversion of Control (IoC) the Concept
• Managing dependencies on other beans– Dependency lookup vs. Dependency Injection
//dependency lookuppublic class Lookup { private SomeBean someBean;
public SomeBean findBean(Container container) { //or new SomeBean("some param values"); return (SomeBean) container.getBean("someBean"); }}
//dependency injectionpublic class Injection { private SomeBean someBean;
public void setSomeBean(SomeBean someBean) { this.someBean = someBean; }}
Advantages of Inversion of Control (IoC)• Separates configuration from code• Simplifies component dependency and lifecycle
management• Eliminates need for:
– Calling new and managing dependencies– Looking up dependencies
• Decouples code from IoC container• Injection is easier – less code – easier to maintain• Minimizes need for creational pattern implementation• Simplifies testing
Dependency Injection (DI)
• Two basic types of injection– Setter injection– Constructor injection
DI (setter injection)
• Say we have the following Rabbit class
• Example<bean id="rabbit" class="org.lds.training.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); }}
DI (constructor injection)
• Say we have the 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>
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/
spring-framework-reference/html/beans.html#beans-collection-elements
Constructor Injection vs. Setter Injection
• Which should you use?• Rule of thumb – write the bean as though Spring
were not managing it– If the bean will not operate without a particular
member variable use constructor injection– Otherwise use setter injection– This is a change from the past
• Let’s look at some more examples
DI Collections
• Say our rabbit has many favorite foodspublic class Rabbit { private Set<String> favoriteFoods;
public void setFavoriteFoods(Set<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>
DI Bean References• Lets expand our rabbit concept to an entire farm
• And then modify our rabbit class as follows
public class Farm { private List<Rabbit> rabbits; public void setRabbits(List<Rabbit> rabbits) { this.rabbits = rabbits; }}
public class Rabbit { private String name; private float weight; public Rabbit(float weight) { this.weight = weight; } public void setName(String name) { this.name = name; } //…}
Bean Reference Examples
<bean id="rabbit" class="org.lds.training.Rabbit"> <constructor-arg name="weight" value=" 3.0" /> <property name="name" value="Bubba" /></bean>
<bean id="farm" class="org.lds.training.Farm"> <property name="rabbits">
<list> <ref bean="rabbit" /> <!– anonymous inner bean -->
<bean class="org.lds.model.Rabbit"><constructor-arg name="weight" value="2.5" />
</bean> </list> </property></bean>
More Examplespublic class Farm { private Rabbit prizeRabbit; public void setPrizeRabbit(Rabbit prizeRabbit) { this.prizeRabbit = prizeRabbit; }}
<bean id="prize" class="org.lds.model.Rabbit"> <constructor-arg name="weight" value="12" /> <property name="name" value="Queen Bee" /></bean>
<!– these are basically equivalent --><bean id="farm" class="org.lds.model.Farm"> <property name="prizeRabbit" ref="prize" /></bean><bean id="farm" class="org.lds.model.Farm"> <property name="prizeRabbit">
<bean class="org.lds.model.Rabbit"> <constructor-arg name="weight" value="12" /> <property name="name" value="Queen Bee" /></bean> </property></bean>
Lab 2: Dependency Injection
https://tech.lds.org/wiki/Introduction_to_Spring#Lab_2_Dependency_Inje
ction
Bean Scopes
• Sometimes you want to be able to control the life of a bean– Bean scopes provide this ability
• For instance, you may want a new instance of a bean every time it is requested, or you may want one instance for the entire application
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.
Singleton Scope
• Singleton - only one instance in a given context• Any time you request an instance of this bean
you will get the single instance of this bean• For instance (if SomeBean is a singleton):
• The new world
<!– 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" />
context.getBean(SomeBean.class) == context.getBean(SomeBean.class) is true
Prototype Scope
• Equivalent to calling new every time an instance of a class is needed
• Spring does not manage the lifecycle of prototype beans
• For instance if SomeBean is prototype scope:
• The configuration is as follows:
<bean id="whatever" class="org.lds.whatever.MyBean" scope="prototype" />
context.getBean(SomeBean.class) == context.getBean(SomeBean.class) is false
Lab 3: Bean Scopes
https://tech.lds.org/wiki/Introduction_to_Spring#Lab_3_Bean_Scopes
Credit where credit is due
• http://springsource.org• Spring Recipies 2nd Edition (Gary Mak, Josh Long
and Daniel Rubio)
Dedication
• Mike Youngstrom and Bruce Campbell for their undying love and support through this process– And for reviewing and offering suggestions
• And for Spencer Uresk and Mike Heath for the inspiration
• And a special thanks to the rabbit farm who made this all possible