Basic knowledge of Spring framework

Source: Internet
Author: User
Tags to domain

Spring framework

The Spring framework is a layered architecture consisting of seven well-defined modules. The Spring module is built on the core container. The core container defines how to create, configure, and manage beans, as shown in figure 1.

Figure 1. Seven modules of the Spring framework

Each module (or component) that makes up the Spring framework can exist independently or be implemented together with one or more modules. The functions of each module are as follows:

  • Core container: The Core container provides the basic functions of the Spring framework. The main component of the core container isBeanFactoryIt is the implementation of the factory model.BeanFactoryUseControl reversal(IOC) mode separates application configuration and dependency specifications from actual application code.
  • Spring Context: Spring context is a configuration file that provides context information to the Spring framework. Spring context includes enterprise services, such as JNDI, EJB, email, internationalization, checksum and scheduling.
  • Spring AOP:
    Through the configuration management feature, the Spring AOP module directly integrates Aspect-oriented programming functions into the Spring framework. Therefore, it is easy to make Spring
    Any object managed by the Framework supports AOP. The Spring AOP module provides transaction management services for objects in Spring-based applications. By using Spring
    AOP, You can integrate declarative transaction management into applications without relying on EJB components.
  • Spring Dao: JDBC
    DAO
    The abstraction layer provides a meaningful exception hierarchy for managing Exception Handling and error messages thrown by different database vendors. Exception hierarchies simplify error handling and greatly reduce the need to write
    Number of abnormal Code (for example, opening or closing a connection ). JDBC-oriented exceptions of Spring DAO follow the common DAO exception hierarchy.
  • Spring ORM: The Spring framework inserts several ORM frameworks to provide ORM Object Relational tools, including JDO, Hibernate, and iBatis SQL Map. All of these comply with the general transactions and DAO exception hierarchies of Spring.
  • Spring web module: The Web context module is built on the application context module, providing context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The web module also simplifies processing multiple requests and Binding Request Parameters to domain objects.
  • Spring MVC Framework: The MVC Framework is a full-featured MVC implementation for Building Web applications. Through the policy interface, the MVC framework becomes highly configurable. MVC supports a large number of view technologies, including JSP, velocity, tiles, itext, and poi.

Spring
The functions of the framework can be used on any J2EE server. Most of the functions are also applicable to unmanaged environments. The core point of Spring is that it does not support binding to a specific J2EE.
Service reusable business and data access objects. There is no doubt that such objects can be reused between different J2EE environments (Web or EJB), independent applications, and test environments.

IOC and AOP

The basic concept of the inversion mode (also known as dependency intervention) is to describe how to create objects without creating them. The Code does not directly connect to objects and services, but describes which component needs which service in the configuration file. Containers (IOC containers in the Spring framework) are responsible for connecting these together.

In a typical IOC scenario, the container creates all objects and sets the necessary properties to connect them together to determine the time to call the method. The following table lists an implementation mode of IOC.

Type 1 Services need to implement special interfaces through which objects provide these services and can query Dependencies from objects (for example, additional services required)
Type 2 Allocate Dependencies by using JavaBean attributes (such as the setter method)
Type 3 Dependencies are provided in the form of constructor and not published in the form of JavaBean attributes.

The IOC container of the Spring framework is implemented by Type 2 and Type 3.

Aspect-Oriented Programming

Aspect-Oriented Programming(AOP) is a programming technology that allows programmers to modularize cross-concern or cross-responsibility line behavior (such as logging and transaction management. The core structure of AOP isAspectIt encapsulates actions that affect multiple classes into reusable modules.

AOP and IOC are complementary technologies that use modular methods to solve complex problems in enterprise application development. In a typical object-oriented development method, you may need to place the logging statement in all methods and Java classes to implement the logging function. In the AOP method, the log ServiceModular, And apply them to the components that require logs in a declarative manner. Of course, the advantage is that the Java class does not need to know the existence of the log service or consider the relevant code. Therefore, application code written with Spring AOP is loosely coupled.

The functions of AOP are fully integrated into the context of Spring transaction management, logs, and other features.

IOC container

The core of Spring design isorg.springframework.beansPackage, which is designed to be used together with the JavaBean Component. This package is usually not directly used by users, but is used by the server as the underlying intermediary for most other functions. The next highest level abstraction isBeanFactoryInterface, which is the implementation of the factory design mode and allows the creation and retrieval of objects by name.BeanFactoryYou can also manage relationships between objects.

BeanFactoryTwo object models are supported.

  • Single StateThe model provides shared instances of objects with specific names, which can be searched during queries. Singleton is the default and most commonly used object model. It is ideal for stateless service objects.
  • PrototypeMake sure that a separate object is created for each search. When each user needs their own objects, the prototype model is the most suitable.

The concept of bean Factory is that Spring serves as the basis of IOC containers. IOC transfers the responsibility for handling tasks from application code to the framework. As I will demonstrate in the next example, the Spring framework uses the JavaBean attribute and configuration data to indicate the dependencies that must be set.

BeanFactory Interface

Becauseorg.springframework.beans.factory.BeanFactoryIs a simple interface, so it can be implemented for various underlying storage methods. The most commonBeanFactoryDefinition isXmlBeanFactoryIt loads beans according to the definition in the XML file, as shown in Listing 1.

Listing 1. xmlbeanfactory

BeanFactory factory = new XMLBeanFactory(new FileInputSteam("mybean.xml"));

The Bean defined in the XML file is passively loaded, which means that the bean itself will not be initialized before the bean is needed. ToBeanFactoryTo retrieve beans, you only need to callgetBean()Method. enter the name of the bean to be retrieved, as shown in Listing 2.

Listing 2. getbean ()

MyBean mybean = (MyBean) factory.getBean("mybean");

The definition of each bean can be POJO (Class Name and JavaBean initialization attribute definition) orFactoryBean.FactoryBeanThe interface adds an indirect level to the application built using the Spring framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.