Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Spring Framework

2 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.

3 Philosophy Java Enterprise Edition should be easier to use! “Lightweight Container” concept

4 JavaEE example: EJB 2.1 public 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(); }

5 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

6 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

7 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

8 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.

9 Off-topic SpringSource was purchased for ~$400 million by VMWare in August 2009

10 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

11 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

12 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.

13 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.

14 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.

15 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.

16 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

17 How to start Add Maven dependency to application’s pom.xml Use the following command to download also sources spring-context provides core functionality org.springframework spring-context 3.2.4.RELEASE mvn eclipse:eclipse -DdownloadSources=true

18 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

19 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

20 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

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

22 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

23 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

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

25 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

26 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

27 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

28 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

29 Non-IoC versus IoC Non Inversion of Control approach Inversion of Control approach

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

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

32 public class UsersDataImportProcessor implements DataImportProcessor { private XmlParser xmlParser; private CommonJpaDAO jpaDAO;... public void importData() { xmlParser = new DomXmlParser(...); jpaDAO = new CommonJpaDAOImpl (...);... List users = xmlParser.parseUsers(usersXml);... jpaDAO.save(user);... } Example from Lab 1 (non-IoC)

33 @Component("usersProcessor") public class UsersDataImportProcessor implements DataImportProcessor { @Autowired private XmlParser xmlParser; @Autowired private CommonJpaDAO jpaDAO;... public void importData() {... List users = xmlParser.parseUsers(usersXml);... jpaDAO.save(user);... } Example from Lab 1 (IoC)

34 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

35 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");

36 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

37 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

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

39 XML configuration: example public class VenueDataImportProcessor implements DataImportProcessor { private boolean cleanDatabase; private Map > request; private String count;... public void setCleanDatabase(boolean cleanDatabase) { this.cleanDatabase = cleanDatabase; } // setters for all properties } Riga, LV food coffee

40 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......

41 Annotations Some of the most common annotations: @Component @Autowired @PostConstruct @PreDestroy @Required @Resource @Repository @Service

42 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"/>

43 @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 {

44 @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 @Qualifier http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework- reference/html/beans.html#beans-autowired-annotation

45 Using the Container The 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 ) : 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.

46 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

47 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

48 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

49 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

50 Setter/Constructor Injection Dependency Injection exists in two major variants: element --> element --> Setter injection: Constructor injection:

51 Example: Constructor Injection public 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; }

52 Scope The Spring Framework supports exactly five scopes: ScopeDescription singletonScopes a single bean definition to a single object instance per Spring IoC container. prototypeScopes a single bean definition to any number of object instances. requestScopes 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.

53 Autowiring Mode It is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. ModeExplanation noNo autowiring at all. byNameAutowiring by property name. Will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. byTypeAllows a property to be autowired if there is exactly one bean of the property type in the container. constructorThis is analogous to byType, but applies to constructor arguments. autodetectChooses constructor or byType through introspection of the bean class.

54 Dependency Checking Mode This feature is sometimes useful when you want to ensure that all properties are set on a bean. ModeExplanation noneNo dependency checking. Properties of the bean which have no value specified for them are simply not set. simpleDependency checking is performed for primitive types and collections (everything except collaborators). objectDependency checking is performed for collaborators only. allDependency checking is done for collaborators, primitive types and collections.

55 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 element: If lazy bean is the dependency of a not lazy singleton bean – don't be confused if the IoC container creates it!

56 Init/Destroy Methods Two ways to implement initialization/destruction callbacks: implementing InitializingBean interface using the 'init-method' attribute public class AnotherExampleBean implements InitializingBean { public void afterPropertiesSet() { // do some initialization work } public class ExampleBean { public void init() { // do some initialization work }

57 @PostConstruct and @PreDestroy Annotations-based support for initialization and destruction callbacks

58 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

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

60 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

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

62 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

63 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

64 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 http://www.martinfowler.com/articles/injection.html

65 References Spring Framework home http://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


Download ppt "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."

Similar presentations


Ads by Google