View
228
Download
0
Category
Preview:
Citation preview
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 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.
Philosophy
• Java Enterprise Edition should be easier to use!
• “Lightweight Container” concept
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();}
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
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
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
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.
Off-topic
• SpringSource was purchased for ~$400 million by VMWare in August 2009
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
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
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.
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.
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.
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.
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
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
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
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
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
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; }}
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
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
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;
}
}
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
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
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
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
Non-IoC versus IoC
Non Inversion of Control approach
Inversion of Control approach
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; }}
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;
}
}
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)
@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)
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
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");
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
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
XML configuration metadataConfiguration consists of at least one and typically more than one bean definition that the container must manage
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>
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>
Annotations
Some of the most common annotations:
•@Component•@Autowired
•@PostConstruct•@PreDestroy
•@Required•@Resource•@Repository•@Service
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"/>
@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 {
@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
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.
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
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
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
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
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:
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"/>
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.
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.
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.
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>
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 }}
@PostConstruct and @PreDestroy
• Annotations-based support for initialization and destruction callbacks
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
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()
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
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)
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
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
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
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
Recommended