spring framework. spring overview spring is an open source layered java/javaee application framework...

65
Spring Framework

Upload: clara-mccarthy

Post on 24-Dec-2015

238 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Framework

Page 2: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Overview

• Spring is an open source layered Java/JavaEE application framework

• Created by Rod Johnson• Based on book “Expert one-on-one J2EE Design and

Development” (October, 2002)

• Current version 3.2.4 (released on 2013-08-06)

• Licensed under the terms of the Apache License, Version 2.0 • http://projects.spring.io/spring-framework/

A software framework is a re-usable design for a software system.

Page 3: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Philosophy

• Java Enterprise Edition should be easier to use!

• “Lightweight Container” concept

Page 4: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

JavaEE example: EJB 2.1public class MyTimeBean implements javax.ejb.SessionBean { public void ejbCreate() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(javax.ejb.SessionContext ctx) {} public void unsetSessionContext() {} public void ejbRemove() {} public String getTime() {

String s = new java.util.Date().toString();return s;

}}

public interface MyTimeLocalHome extends javax.ejb.EJBLocalHome { public MyTimeLocal create() throws javax.ejb.CreateException;}

public interface MyTimeLocal extends javax.ejb.EJBLocalObject { public java.lang.String getTime();}

Page 5: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

What are Lightweight Frameworks?

• Non-intrusive• No container requirements• Simplify application development

• Remove re-occurring pattern code• Productivity friendly• Unit test friendly

• Very pluggable• Usually open source• Examples:

• Spring, Pico, Hivemind• Hibernate, IBatis, Castor• WebWork, Sitemesh, Quartz

Page 6: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Mission Statement [1/3]

• Java EE should be easier to use

• It's best to program to interfaces, rather than classes. Spring reduces the complexity cost of using interfaces to zero.

• JavaBeans offer a great way of configuring applications

• OO design is more important than any implementation technology, such as Java EE

Page 7: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Mission Statement [2/3]

• Checked exceptions are overused in Java. A framework shouldn't force you to catch exceptions you're unlikely to be able to recover from.

• Testability is essential, and a framework such as Spring should help make your code easier to test

• Spring should be a pleasure to use

• Your application code should not depend on Spring APIs

Page 8: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Mission Statement [3/3]

• Spring should not compete with good existing solutions, but should foster integration.

For example, JDO and Hibernate are great object- relational mapping solutions. Don't need to develop another one.

Page 9: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Off-topic

• SpringSource was purchased for ~$400 million by VMWare in August 2009

Page 10: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Modules of the Spring Framework

The Spring Framework consists of features organized into about 20 modules

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html

Page 11: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Modules of the Spring Framework

• Core Container: • Core and Beans

• Inversion of Control, Dependency Injection• Context• Expression Language

• Data Access/Integration• JDBC, ORM, OXM, JMS and Transaction

• Web MVC• Model-View-Controller implementation for web

applications

Page 12: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Modules of the Spring Framework

• AOP (Aspect-Oriented Programming)• Remote Access • Authentication and Authorization• Remote Management• Messaging Framework• Web Services• Email• Testing

Spring is very loosely coupled, components widely reusable and separately packaged.

Page 13: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Details [1/3]

• Spring allows to decouple software layers by injecting a component’s dependencies at runtime rather than having them declared at compile time via importing and instantiating classes.

• Spring provides integration for Java EE services such as EJB, JDBC, JNDI, JMS, JTA. It also integrates several popular ORM toolkits such as Hibernate and JDO and assorted other services as well.

Page 14: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Details [2/3]

• Spring is built on the principle of unchecked exception handling.

This also reduces code dependencies between layers.

Spring provides a granular exception hierarchy for data access operations and maps JDBC, EJB, and ORM exceptions to Spring exceptions so that applications can get better information about the error condition.

Page 15: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Details [3/3]

• With highly decoupled software layers and programming to interfaces, each layer is easier to test. Mock objects is a testing pattern that is very useful in this regard.

• One of the highly touted features is declarative transactions, which allows the developer to write transaction-unaware code and configure transactions in Spring configuration files.

Page 16: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Solutions

• Solutions address major Java EE problem areas:• Web application development (MVC)

• Enterprise Java Beans (EJB, JNDI)

• Database access (JDBC, iBatis, ORM)

• Transaction management (JTA, Hibernate, JDBC)

• Remote access (Web Services, RMI)

• Each solution builds on the core architecture

• Solutions foster integration, they do not re-invent the wheel

Page 17: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

How to start

• Add Maven dependency to application’s pom.xml

• Use the following command to download also sources

• spring-context provides core functionality

<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency></dependencies>

mvn eclipse:eclipse -DdownloadSources=true

Page 18: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Inversion of Control (IoC)

• Central in the Spring is its Inversion of Control container

• Based on “Inversion of Control Containers and the Dependency Injection pattern” (by Martin Fowler)

• Inversion of Control and Dependency Injection, in fact, are not the same thing

• Inversion of Control is a much more general concept

• Dependency Injection is merely one concrete example of IoC

Page 19: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Inversion of Control

• IoC covers a broad range of techniques that allow an object to become a passive participant in the system

• Provides centralized, automated configuration, managing and wiring of application Java objects

• Container responsibilities: • creating objects• configuring objects• calling initialization methods• etc

Java objects that are managed by the Spring IoC container are referred to as beans

Object’s lifecycle

Page 20: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Dependency Injection

• Dependency Injection is a technique that frameworks use to wire together an application

• In nearly all applications there are at least two participants, and these participants are required to somehow collaborate

• The trick is to connect these two objects without locking in the connection

Page 21: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Example

public interface CashRegister { public BigDecimal calculateTotalPrice(ShoppingCart cart);}

public interface PriceMatrix { public BigDecimal lookupPrice(Item item);}

public class CashRegisterImpl implements CashRegister { private PriceMatrix priceMatrix = new PriceMatrixImpl(); public BigDecimal calculateTotalPrice(ShoppingCart cart) { BigDecimal total = new BigDecimal("0.0"); for (Item item : cart.getItems()) { total.add(priceMatrix.lookupPrice(item)); } return total; }}

Page 22: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Three major issues

1. Every instance of CashRegisterImpl has a separate instance of PriceMatrixImpl• With heavy services it is preferable to share a single instance

across multiple clients

2. CashRegisterImpl now has concrete knowledge of the implementation of PriceMatrix• Tight coupling

3. By explicitly creating its own dependent objects, the CashRegisterImpl creates a difficult test situation• Unit tests have to be divorced from any environment

requirements

Page 23: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

First attempt – Service Locator

• The Service Locator pattern encapsulates the actions taken to obtain a reference to the object required

• This shields the client from knowing how, or even where, to obtain a reference to the object

Page 24: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Updated code

public class CashRegisterImpl implements CashRegister {

private PriceMatrix priceMatrix;

public CashRegisterImpl() {

priceMatrix = ServiceLocator.getPriceMatrix();

}

public BigDecimal calculateTotalPrice(ShoppingCart cart) {

BigDecimal total = new BigDecimal("0.0");

for (Item item : cart.getItems()) {

total.add(priceMatrix.lookupPrice(item));

}

return total;

}

}

Page 25: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Result

• Using this Service Locator, the class no longer has to manage object creation

• Next benefit - client has no knowledge of the concrete implementation of PriceMatrix

• The third problem, the lack of testability, is unfortunately still with us• Creating a unit test is extremely difficult, because the

implementation relies on a functioning PriceMatrix object

Page 26: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Testing with mock objects

• The test should be written with a mock PriceMatrix, but there is no way to insert the mock object during testing

• The static ServiceLocator.getPriceMatrix is difficult to change during a test run

• There is a need to swap different implementations of PriceMatrix without affecting the client

Page 27: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

How to achieve it ?

• To effectively do this, the client must not actively participate in the construction or retrieval of the resource

• The resource must be given to the client

• Solution Dependency Injection

Page 28: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Dependency Injection

• Dependency Injection is a technique to wire an application together without any participation by the code that requires the dependency

• The object provides a property that can hold a reference to specific service

• When the object is created a reference to an implementation of that type of service will automatically be injected into that property - by an external mechanism

Page 29: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Non-IoC versus IoC

Non Inversion of Control approach

Inversion of Control approach

Page 30: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Dependency Injection Basics• Basic JavaBean pattern:

• include a “getter” and “setter” method for each private field:

• Rather than locating needed resources, application components provide setters through which resources are passed in during initialization

• In Spring Framework, this pattern is used extensively, and initialization is usually done through configuration file rather than application code

class MyBean { private int counter;

public int getCounter() { return counter; }

public void setCounter(int counter) { this.counter = counter; }}

Page 31: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Updated Code

public class CashRegisterImpl implements CashRegister {

private PriceMatrix priceMatrix;

public setPriceMatrix(PriceMatrix priceMatrix) {

this.priceMatrix = priceMatrix;

}

public BigDecimal calculateTotalPrice(ShoppingCart cart) {

BigDecimal total = new BigDecimal("0.0");

for (Item item : cart.getItems()) {

total.add(priceMatrix.lookupPrice(item));

}

return total;

}

}

Page 32: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

public class UsersDataImportProcessor implements DataImportProcessor {

private XmlParser xmlParser; private CommonJpaDAO jpaDAO; . . . public void importData() {

xmlParser = new DomXmlParser(. . .); jpaDAO = new CommonJpaDAOImpl (. . .);

. . . List<User> users =

xmlParser.parseUsers(usersXml);

. . .

jpaDAO.save(user); . . . }}

Example from Lab 1 (non-IoC)

Page 33: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

@Component("usersProcessor")public class UsersDataImportProcessor

implements DataImportProcessor {

@Autowired private XmlParser xmlParser; @Autowired private CommonJpaDAO jpaDAO; . . . public void importData() { . . . List<User> users =

xmlParser.parseUsers(usersXml);

. . .

jpaDAO.save(user); . . . }}

Example from Lab 1 (IoC)

Page 34: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring’s Dependency Injection

• Beans define their dependencies through constructor arguments or properties

• Container resolves (injects) dependencies of components by setting implementation object during runtime

• BeanFactory interface – the core that loads bean definitions and manages beans

• Superset ApplicationContext - represents the Spring IoC container and is responsible for instantiating, configuring, and assembling beans

Page 35: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Creating ApplicationContext

• In standalone applications it is common to create an instance of ClassPathXmlApplicationContext

The ApplicationContext interface builds on top of the BeanFactory (it is a sub-interface) and adds other functionality

ApplicationContext context = new ClassPathXmlApplicationContext(

"applicationContext.xml");

Page 36: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Configuration metadata• Application classes are combined with

configuration metadata so that after the ApplicationContext is created and initialized, you have a fully configured and executable system

Page 37: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Configuration metadata• Configuration metadata defines how you as an

application developer tell the Spring container to instantiate, configure, and assemble the objects in your application

• There exists several forms of defining configuration metadata:• XML-based (traditional form)

• Annotation-based configuration

• Java-based configuration

Page 38: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

XML configuration metadataConfiguration consists of at least one and typically more than one bean definition that the container must manage

Page 39: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

XML configuration: examplepublic class VenueDataImportProcessor

implements DataImportProcessor {

private boolean cleanDatabase;

private Map<String, List<String>> request;

private String count;

. . .

public void setCleanDatabase(boolean cleanDatabase) {

this.cleanDatabase = cleanDatabase;

}

// setters for all properties

}

<bean id="venuesProcessor" class="lv.lu.meetings.impl.venues.VenueDataImportProcessor"> <property name="cleanDatabase" value="true"/> <property name="count" value="10"/> <property name="request"> <map> <entry> <key><value>Riga, LV</value></key> <list> <value>food</value> <value>coffee</value> </list> </entry> </map> </property></bean>

Page 40: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Annotation-based configuration• Instead of using XML, it is possible to move the

configuration into the component class by using annotations on the relevant class, method, or field declaration

• Enable annotation support by adding the following tag to XML configuration

<beans ... > ... <context:annotation-config/> ...</beans>

Page 41: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Annotations

Some of the most common annotations:

•@Component•@Autowired

•@PostConstruct•@PreDestroy

•@Required•@Resource•@Repository•@Service

Page 42: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Classpath Scanning

• There is a way to avoid registering each bean in XML file - using classpath scanning

• Enable by adding the following tag to XML configuration:

• Then, add @Component annotation to Java class definitions

http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/beans.html#beans-classpath-scanning

<context:component-scan base-package="lv.lu.meetings.impl"/>

Page 43: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

@Component

• To register a bean simply add annotation @Component to the class definition

• Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning

http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/beans.html#beans-classpath-scanning

@Component("usersProcessor")public class UsersDataImportProcessor

implements DataImportProcessor {

Page 44: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

@Autowired

• Marks a constructor, field, setter method or configuration method as to be autowired by Spring's dependency injection facilities

• If there are multiple candidates for autowiring one option is to use

@Qualifierhttp://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/beans.html#beans-autowired-annotation

Page 45: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Using the ContainerThe BeanFactory interface has the following methods for client code to call:• boolean containsBean(String): returns true if the BeanFactory

contains a bean definition or bean instance that matches the given name.

• Object getBean(String): returns an instance of the bean registered under the given name.

• Object getBean(String, Class<T>): returns a bean, registered under the given name. The bean returned will be cast to the given Class.

• Class getType(String name): returns the Class of the bean with the given name.

• boolean isSingleton(String): determines if the bean under the given name is a singleton.

• String[] getAliases(String): Return the aliases for the given bean name, if any were defined in the bean definition.

Page 46: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Off-topic: Singleton

• Singleton pattern is a design pattern that is used to restrict instantiation of a class to one object

• “Ensure a class has only one instance, and provide a global point of access to it”

• Typical prototypical examples: window managers, print spoolers, file systems

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

Page 47: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Bean Definition

• The bean class is the actual implementation of the bean being described by the BeanFactory

• Bean examples – Data Access Object, Data Source, Transaction Manager, Persistence Managers, Service objects, etc

• Spring configuration contains implementation classes while your code should program to interfaces

Page 48: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Bean Definition

• Bean behaviors include:• Singleton or prototype

• Autowiring

• Initialization and destruction methods • init-method

• destroy-method

• Beans can be configured to have property values set• Can read simple values, collections, maps,

references to other beans, etc

Page 49: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Bean Definition

Each bean definition consists of the following set of properties:

• class • name • scope • constructor arguments • properties • autowiring mode • dependency checking mode • lazy-initialization mode • initialization method • destruction method

Beans are created using the instructions defined in the configuration metadata that has been supplied to the container

Page 50: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Setter/Constructor Injection

Dependency Injection exists in two major variants:<bean id="exampleBean" class="examples.ExampleBean"> <!-- setter injection using the nested <ref/> element --> <property name="beanOne"><ref bean="anotherExampleBean"/></property> <!-- setter injection using the neater 'ref' attribute --> <property name="beanTwo" ref="yetAnotherBean"/> <property name="integerProperty" value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

<bean id="exampleBean" class="examples.ExampleBean"> <!-- constructor injection using the nested <ref/> element --> <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg> <!-- constructor injection using the neater 'ref' attribute --> <constructor-arg ref="yetAnotherBean"/> <constructor-arg type="int" value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

Setter injection:

Constructor injection:

Page 51: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Example: Constructor Injectionpublic class CashRegisterImpl implements CashRegister {

private PriceMatrix priceMatrix;

public CashRegisterImpl(PriceMatrix priceMatrix) {this.priceMatrix = priceMatrix;

}

public BigDecimal calculateTotalPrice(ShoppingCart cart) {BigDecimal total = new BigDecimal("0.0");for (Item item : cart.getItems()) {

total.add(priceMatrix.lookupPrice(item));}return total;

}}

<bean id=“cashRegister" class="examples.CashRegisterImpl"> <constructor-arg ref=“priceMatrix"/> </bean>

<bean id=“priceMatrix" class="examples.PriceMatrixImpl"/>

Page 52: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Scope

The Spring Framework supports exactly five scopes:

Scope Descriptionsingleton Scopes a single bean definition to a single object instance per

Spring IoC container.

prototype Scopes a single bean definition to any number of object instances.

request Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition.

session Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware ApplicationContext.

global session

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context.

Page 53: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Autowiring ModeIt is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory.

Mode Explanationno No autowiring at all.

byName Autowiring by property name. Will inspect the container and look for a bean named exactly the same as the property which needs to be autowired.

byType Allows a property to be autowired if there is exactly one bean of the property type in the container.

constructor This is analogous to byType, but applies to constructor arguments.

autodetect Chooses constructor or byType through introspection of the bean class.

Page 54: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Dependency Checking Mode

This feature is sometimes useful when you want to ensure that all properties are set on a bean.

Mode Explanation

none No dependency checking. Properties of the bean which have no value specified for them are simply not set.

simple Dependency checking is performed for primitive types and collections (everything except collaborators).

object Dependency checking is performed for collaborators only.

all Dependency checking is done for collaborators, primitive types and collections.

Page 55: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Lazy-Initialization Mode• The default behavior for ApplicationContext implementations

is to eagerly pre-instantiate all singleton beans at startup

• If you do not want such a behavior, you can selectively control this by marking a bean definition as lazy-initialized

• It is also possible to control lazy-initialization at the container level by using the 'default-lazy-init' attribute on the <beans/> element:

• If lazy bean is the dependency of a not lazy singleton bean – don't be confused if the IoC container creates it!

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/><bean name="not.lazy" class="com.foo.AnotherBean"/>

<beans default-lazy-init="true"> <!-- no beans will be pre-instantiated... --></beans>

Page 56: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Init/Destroy MethodsTwo ways to implement initialization/destruction callbacks:

• implementing InitializingBean interface

• using the 'init-method' attribute

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>

public class AnotherExampleBean implements InitializingBean { public void afterPropertiesSet() { // do some initialization work }}

<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>

public class ExampleBean { public void init() { // do some initialization work }}

Page 57: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

@PostConstruct and @PreDestroy

• Annotations-based support for initialization and destruction callbacks

Page 58: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Bean Lifecycle

• Beans managed by Spring have a distinct and predictable lifecycle

• Spring provides

simple ways of

receiving callbacks

from lifecycle events

• Hooks are purely

optional, no

dependency on Spring in your

beans – unless you want to

Page 59: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Advantages of Spring Architecture [1/3]

• Enable you to write powerful, scalable applications using POJOs

• Dependencies - Spring handles injecting dependent components without a component knowing where they came from (IoC)

• Lifecycle – responsible for managing all your application components, particularly those in the middle tier container sees components through well-defined lifecycle: init(), destroy()

Page 60: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Advantages of Spring Architecture [2/3]

• Configuration information - Spring provides one consistent way of configuring everything, separate configuration from application logic, varying configuration

• In JavaEE (e.g. EJB) it is easy to become dependent on container and deployment environment, proliferation of pointless classes (locators/delegates); Spring eliminates them

Page 61: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Advantages of Spring Architecture [3/3]

• Cross-cutting behavior (resource management is cross-cutting concern, easy to copy-and-paste everywhere)

• Portable (can use server-side in web/ejb app, client-side in swing app, business logic is completely portable)

Page 62: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

SpringSource Tool Suite

• SpringSource Tool Suite™ (STS) provides the best Eclipse-powered development environment for building Spring-powered applications

• It's built as a set of plugins for the Eclipse platform

• Web page:• http://www.springsource.com/developer/sts

Page 63: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

Spring Application Tools Features• Spring project, bean and XML file wizards

• Graphical Spring configuration editor

• Spring 3.0 support

• Spring Web Flow and Spring Batch visual development tools

• Spring Roo project wizard and development shell

Page 64: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

References

Clarence Ho, Rob Harrop: Pro Spring 3 (Professional Apress), Apress, 2012, pp. 944 (LUB pieejami 2 eksemplāri)http://www.apress.com/9781430241072

Gary Mak: Spring recipes: a problem-solution approach, Berkeley, Calif.: Apress, 2008, pp. 727 (LUB pieejami 5 eksemplāri)http://www.amazon.com/Spring-Recipes-Problem-Solution-Approach-Professionals/dp/1590599799

Craig Walls, Ryan Breidenbach: Spring in action, Greenwich, CT: Manning, 2008, pp. 730 (LUB pieejams 1 eksemplārs)http://www.amazon.com/Spring-Action-Craig-Walls/dp/1933988134/

Inversion of Control Containers and the Dependency Injection pattern http://www.martinfowler.com/articles/injection.html

Page 65: Spring Framework. Spring Overview Spring is an open source layered Java/JavaEE application framework Created by Rod Johnson Based on book “Expert one-on-one

References

• Spring Framework homehttp://projects.spring.io/spring-framework/

• Spring IoC Container:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html

• SpringSource Tool Suite (Eclipse plugin)http://www.springsource.com/developer/sts