Presentation is loading. Please wait.

Presentation is loading. Please wait.

J2EE Lecture 6: Spring – IoC and Dependency Injection

Similar presentations


Presentation on theme: "J2EE Lecture 6: Spring – IoC and Dependency Injection"— Presentation transcript:

1 J2EE Lecture 6: Spring – IoC and Dependency Injection
Dr. Ming Qiu Xiamen University Software School

2 Spring in Action, Chap 1 6.1 What is Spring Spring is an open-source framework, created by Rod Johnson makes it possible to use plain-vanilla JavaBeans to achieve things that were previously only possible with EJBs.

3 Spring in Action, Chap 1 6.1 What is Spring Spring is a lightweight inversion of control and aspect-oriented container framework Lightweight Can be distributed in a single JAR file that weighs in at just over 1 MB. The processing overhead required by Spring is negligible. Nonintrusive objects in a Spring-enabled application typically have no dependencies on Spring-specific classes.

4 6.1 What is Spring Inversion of control--- loose coupling
Spring in Action, Chap 1 6.1 What is Spring Inversion of control--- loose coupling Objects are passively given their dependencies instead of creating or looking for dependent objects for themselves. Spring gives the dependencies to the object at instantiation without waiting to be asked.

5 6.1 What is Spring Aspect-oriented Container
Spring in Action, Chap 1 6.1 What is Spring Aspect-oriented Comes with rich support for aspect-oriented programming that enables cohesive development by separating application business logic from system services. Container A container in the sense that it contains and manages the life cycle and configuration of application objects. How it is created– one single instance or a new instance

6 6.1 What is Spring Framework
Spring in Action, Chap 1 6.1 What is Spring Framework Makes it possible to configure and compose complex applications from simpler components. Provides much infrastructure functionality Transaction management Persistence framework integration Security

7 Reference 1 6.1 What is Spring

8 6.1 What is Spring The Core Container
Reference 1 6.1 What is Spring The Core Container The spring-core and spring-beans modules provides the IoC features. BeanFactory -- an implementation of the factory pat- tern that applies IoC to separate your application’s configuration and dependency specifications from the actual application code.

9 Reference 1 6.1 What is Spring The Context (spring-context) module builds on the solid base provided by the Core and Beans modules: Extends the concept of BeanFactory, adding support for internationalization (I18N) messages, event propagation, resource loading and the transparent creation of contexts by a servlet container. Provides support for integrating common third-party libraries for caching (EhCache, Guava, JCache), mailing (JavaMail), scheduling (CommonJ, Quartz) and template engines (FreeMarker, JasperReports, Velocity) The ApplicationContext interface is the focal point of the Context module.

10 Reference 1 6.1 What is Spring The spring-expression module provides a powerful Expression Language for querying and manipulating an object graph at runtime. an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification

11 6.1 What is Spring AOP and Instrumentation
Reference 1 6.1 What is Spring AOP and Instrumentation The spring-aop module provides an AOP Alliance-compliant aspect-oriented programming implementation to cleanly decouple code that implements functionality that should be separated. The spring-aspects module provides integration with AspectJ. The spring-instrument module provides class instrumentation support and classloader implementations to be used in certain application servers.

12 6.1 What is Spring Messaging
Reference 1 6.1 What is Spring Messaging spring-messaging module with key abstractions from the Spring Integration project such as Message, MessageChannel, MessageHandler, and others to serve as a foundation for messaging-based applications

13 6.1 What is Spring Data Access/Integration
Reference 1 6.1 What is Spring Data Access/Integration The spring-jdbc module provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes. The spring-tx module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs. The spring-orm module provides integration layers for popular object-relational mapping APIs.

14 Reference 1 6.1 What is Spring The spring-oxm module provides an abstraction layer that supports Object/XML mapping implementations such as JAXB, Castor, XMLBeans, JiBX and XStream. The spring-jms module (Java Messaging Service) contains features for producing and consuming messages. Since Spring Framework 4.1, it provides integration with the spring-messaging module.

15 Reference 1 6.1 What is Spring Web The spring-web module provides basic web-oriented integration features such as multipart file upload functionality and the initialization of the IoC container using Servlet listeners and a web-oriented application context. The spring-webmvc module contains Spring’s model-view-controller (MVC) and REST Web Services implementation for web applications. The spring-webmvc-portlet module (also known as the Web-Portlet module) provides the MVC implementation to be used in a Portlet environment and mirrors the functionality of the spring-webmvc module.

16 Reference 1 6.1 What is Spring Test The spring-test module supports the unit testing and integration testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring ApplicationContexts and caching of those contexts. It also provides mock objects that you can use to test your code in isolation.

17 6.2 Usage Scenarios Reference 1
Typical full-fledged Spring web application

18 6.2 Usage Scenarios Reference 1
Spring middle-tier using a third-party web framework

19 Remoting usage scenario
Reference 1 6.2 Usage Scenarios Remoting usage scenario

20 EJBs - Wrapping existing POJOs
Reference 1 6.2 Usage Scenarios EJBs - Wrapping existing POJOs

21 Struts in Action, Chap 9 6.3 The IoC container A JavaEE application consists of a set of objects. These objects cooperate to solve the problems facing the application How do all these objects get instantiated? How do they come to have references to one another?

22 6.3 The IoC container Some objects are created by frameworks
Struts in Action, Chap 9 6.3 The IoC container Some objects are created by frameworks Struts 2 decides which action class maps to a request Model objects are instantiated by Hibernate, when we use methods of Session to fetch objects. But some objects, such as Service object, Dao object are not automatically created by frameworks.

23 Spring in Action, Chap 2 6.3 The IoC container Spring actually comes with two distinct types of containers the BeanFactory provides the configuration framework and basic functionality the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory,

24 Reference 1 6.3 The IoC container

25 Reference 1 6.3 The IoC container XML-based configuration metadata

26 6.3 The IoC container Instantiating a container Reference 1
Services.xml

27 Reference 1 6.3 The IoC container daos.xml

28 Reference 1 6.3 The IoC container

29 6.3 The IoC container Instantiating a using an instance factory method
Reference 1 6.3 The IoC container Instantiating a using an instance factory method

30 Reference 1 6.4 Dependencies The container performs bean dependency resolution as follows: The ApplicationContext is created and initialized with configuration metadata. For each bean, its dependencies are expressed in the form of properties, constructor arguments, or arguments to the static-factory method. These dependencies are provided to the bean, when the bean is actually created. Each property or constructor argument is an actual definition of the value to set, or a reference to another bean in the container. Each property or constructor argument which is a value is converted from its specified format to the actual type of that property or constructor argument.

31 Reference 1 6.4 Dependencies

32 Reference 1 6.4 Dependencies

33 Reference 1 6.4 Dependencies

34 6.4 Dependencies Lazy-initialized beans
Reference 1 6.4 Dependencies Lazy-initialized beans By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process.

35 6.4 Dependencies Autowiring Colaborators
Reference 1 6.4 Dependencies Autowiring Colaborators significantly reduce the need to specify properties or constructor arguments. update a configuration as your objects evolve.

36 Reference 1 6.4 Dependencies Autowiring Modes

37 Reference 1 6.5 Bean Scopes

38 Reference 1 6.5 Bean Scopes The Singleton Scope

39 Reference 1 6.5 Bean Scopes The Prototype Scope

40 Spring in Action, Chap 2 6.6 Bean’s Life

41 Spring in Action, Chap 2 6.7 Basic Wiring

42 Reference Spring Framework Reference Documentation.


Download ppt "J2EE Lecture 6: Spring – IoC and Dependency Injection"

Similar presentations


Ads by Google