Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.

Similar presentations


Presentation on theme: "Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic."— Presentation transcript:

1 Introduction to Spring Matt Wheeler

2 Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic Java and XML skills

3 Overview Learn the basics of the Spring Framework 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

4 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

5 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.j spa

6 Spring Framework Modified from http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/images/spring-overview.png

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

8 Web Basic web integration features – File upload – Initialization of IoC container using servlet listeners – Contains Spring’s model view controller (MVC) implementation

9 Test Test module provides integration with test frameworks – JUnit – TestNG Provides ability to load test specific ApplicationContexts Also provides helpful mock objects

10 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

11 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 beans Bean definitions can be provided to Spring: – In xml – Using annotations – In Java code

12 Defining Beans ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBean someBean = context.getBean(SomeBean.class); someBean.callMethod(); Bean definition (beans.xml) Application Context

13 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

14 Defining Beans ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBean someBean = context.getBean("someBean", SomeBean.class); someBean.callMethod(); Bean definition (beans.xml) Application Context

15 Lab 1: Bean Creation https://tech.lds.org/wiki/Introduction_to_Spring#L ab_1_Bean_Configuration

16 Dependency Injection Dependency Injection (DI) & Inversion of Control (IoC)

17 Object Dependencies Most objects cannot do much by themselves – They depend on other objects to delegate some tasks – For instance if an object wanted to insert something into a database it might depend on another data source object 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 dependency – For instance a data source might be used by many objects to persist data

18 Inversion of Control (IoC) the Concept Objects define dependencies by: – Constructor arguments – Setter arguments – Arguments to factory method Container injects dependencies when the bean is created Inverse of bean controlling instantiation and/or location of its dependencies

19 Inversion of Control (IoC) the Concept Managing dependencies on other beans – Dependency lookup vs. Dependency Injection //dependency lookup public class Lookup { private SomeBean someBean; public SomeBean findBean(Container container) { return (SomeBean) container.getBean(“someBean”); } //dependency injection public class Injection { private SomeBean someBean; public void setSomeBean(SomeBean someBean) { this.someBean = someBean; }

20 Advantages of Inversion of Control (IoC) Simplifies component dependency and lifecycle management Eliminates need for: – Calling new or looking up dependencies Decouples code from IoC container Injection is easier – less code – easier to maintain Minimizes need for creational pattern implementation Simplifies testing

21 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

22 Dependency Injection (DI) Two basic types of injection – Setter injection – Constructor injection

23 DI (setter injection) Say we have to following Rabbit class Example public class Rabbit { private String favoriteFood; public void setFavoriteFood(String favoriteFood) { this.favoriteFood = favoriteFood; } public void printFavoriteFood() { System.out.println(favoriteFood); }

24 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); }

25 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/spri ng-framework-reference/html/beans.html#beans- collection-elements

26 DI Collections Say our rabbit has many favorite foods public class Rabbit { private Set favoriteFoods; public void setFavoriteFoods(List favoriteFoods) { this.favoriteFoods = favoriteFoods; } public void printFavoriteFood() { for (String favoriteFoods : favoriteFood) { System.out.println(favoriteFood); } lettuce carrot

27 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 rabbits; public void setRabbits(List 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; } //… }

28 Bean Reference Examples

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

30 Lab 3: Dependency Injection https://tech.lds.org/wiki/Introduction_to_Spring#L ab_3_Dependency_Injection

31 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

32 Available Bean Scopes ScopeDocumentation singletonCreates a single bean instance per IoC container (default) prototypeCreates a new instance every time a bean of the type is requested requestCreates a single instance per HTTP request. Only valid in a web application context. sessionCreates a single instance per HTTP session. Only valid in a web application context. globalSessionCreates a single instance per HTTP session. Only valid in a portal application.

33 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; }

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

35 Web application scopes Without Spring you would have to manage bean creation and lifecycles manually

36 Lab 2: Bean Scopes https://tech.lds.org/wiki/Introduction_to_Spring#L ab_2_Bean_Scopes

37 Summary


Download ppt "Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic."

Similar presentations


Ads by Google