Introduction to the Spring Framework Quick Start

Source: Internet
Author: User
Tags error handling to domain

Spring is an open-source application framework on the Java platform. The first version of it was written by Rod Johnson. Rod is the first to release this framework in his expert one-on-one Java EE Design and development, a one-to-man Java Enterprise Application Designer and developer. The framework can also be ported to. NET environment,

The spring framework was first published in June 2003 with the Apache 2.0 license. The first landmark version was released in March 2004 in 1.0. In September 2004 and March 2005 there were also important editions.

The Spring framework itself does not impose any special programming patterns. In the Java community, Spring is popular as an alternative to the EJB model and even as a substitute. From a design point of view, Spring gives Java programmers a lot of freedom, but also provides good documentation and easy-to-use methods for common problems in the industry.

The core functionality of the Spring Framework is applicable in any Java application. In the Web application based on the Java Enterprise platform, a great deal of expansion and improvement can be formed. To this end, spring has been widely welcomed and recognized by many companies as a strategic and important framework.

1. History of the Spring framework

The first part of the Spring framework was written by Rod Johnson in 2000 to provide an independent consulting business for the financial community in London. In "Java Enterprise application design and development experts one-to-man" book, Rod further expanded his code to explain "How to enable the application to be more than the general public is accustomed to accept the ease of use and stability with the different components on the Java EE platform to cooperate" view.

In the 2001, the mainstream programming patterns for Web applications were Java Servlet APIs and EJBS. Both are made by Sun Micro Systems and other developers and interest groups, and have gained a broad consensus in the Java industry. Non-web applications, such as client-side or batch applications, can also be based on open source or business tools and projects that provide the required functionality.

The spring framework, which is based on optimal methods and is suitable for a variety of application types, is attributed to Rod Johnson. These ideas are also described in his book. After publication, the source code is provided under the Open source Use agreement, based on the reader's requirements.

A group of program developers who volunteered to expand the spring framework formed a project on SourceForge in February 2003. After working on the spring framework for a year, the team released its first version (1.0) in March 2004. After this version, the spring framework became extremely popular in the Java community, partly due to its better documentation capabilities and references, especially for an open source project.

But the spring framework was also criticized in the 2004, and sometimes it became a hotly debated subject. When the first version of Spring was released, many programmers and leading designers viewed it as a step away from the traditional programming paradigm, especially for EJBS. An important design goal of the spring framework is to make it easier to integrate with existing Java EE Standards and commercial tools. To a large extent, this goal makes it dispensable to define functionality through a normative document controlled by a controversial official committee.

The spring framework makes the previously unpopular technology quickly popular in a short period of time, and the most famous example is the reverse control (IOC). The adoption rate of the spring framework was very high in 2004, and spring made AOP generally popular in the Java community by introducing its own AOP (orientation-oriented programming).

In 2005, Spring was introduced with a new milestone, a more versatile addition, resulting in a higher adoption rate than in 2004. The spring forum, created at the end of 2004, also contributed to the promotion of the framework. Forum for the vast number of users has become the most important source of information and help.

In 2005, Spring Framework developers set up their own company to provide business support for spring, the most notable of which is the cooperation with BEA. In December 2005, the first spring meeting was held in Miami, and the 3-day course attracted 300 developers. The conference held in Antwerp in June 2006 was attended by more than 400 developers.

2. Key features of the spring framework
Configuration management based on Java beans, using IOC principles, especially for the use of dependent injection technology. These are used to reduce the interdependence of the implementation rules among the components.
A core, globally applicable bean factory
A general abstraction layer to manage data processing between databases
A general strategy built within the framework for the Java Data Processing API and a separate JDBC source. Therefore, the dependency on the Java Enterprise version environment is eliminated on data processing support
And some sustainability frameworks, such as Hibernate,jdo,ibatis and db4o, are integrated
The MVC framework in Web applications, based on the core spring features, supports a variety of techniques for generating views, including Jsp,freemarker,velocity,tiles,itext, and POI
A large number of AOP frameworks to provide services such as data processing management. As with IOC, the aim is to improve the modularity of the system.

Spring is an open source framework that is created to address the complexities of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows you to choose which components to use while providing an integrated framework for Java EE application development.

In the 1th part of this three-part spring series, I'll cover the spring framework. I first describe the framework's functionality from the perspective of the framework's underlying model, and then we'll discuss the two most interesting modules: Spring aspect-oriented programming (AOP) and control inversion (IOC) containers. A few examples are then used to demonstrate the application of the IOC container in a typical application use case scenario. These examples will also serve as the basis for an expanded discussion later in this series, which will explain how the spring framework implements AOP constructs through Spring AOP later in this article.

   Spring Framework

The Spring framework is a layered architecture consisting of 7 well-defined modules. The Spring modules are built on top of the core container, and the core container defines how the beans are created, configured, and managed, as shown in 1.


Figure 1. 7 Modules of the Spring framework

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

Core Container: The core container provides the basic functionality of the Spring framework. The main component of the core container is Beanfactory, which is the implementation of the factory pattern. The beanfactory uses the inversion of control (IOC) pattern to separate the application's configuration and dependency specifications from the actual application code.

Spring Context: The spring context is a configuration file that provides contextual information to the spring framework. The Spring context includes enterprise services such as JNDI, EJB, e-mail, internationalization, checksum scheduling.

Spring AOP: With the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming capabilities into the spring framework. Therefore, it is easy to enable any object managed by the Spring framework to support 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 your application without relying on EJB components.

Spring DAO: The JDBC DAO Abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of exception codes that need to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exception conforms to the common DAO exception hierarchy.

Spring ORM: The Spring framework inserts several ORM frameworks, providing ORM object-relational tools, including JDO, Hibernate, and IBatis SQL Map. All of these conform to Spring's common transaction and DAO exception hierarchies.

Spring Web Module: The Web context module is built on top of the application context module and provides the context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the process of working with multipart requests and binding request parameters to domain objects.

Spring MVC Framework: The MVC framework is a full-featured MVC implementation of building WEB applications. With the policy interface, the MVC framework becomes highly configurable, and MVC accommodates a large number of view technologies, including JSP, Velocity, Tiles, IText, and POI.

The functionality of the Spring framework can be used in any Java EE server, and most features are also available in an out-of-management environment. The core point of Spring is to support reusable business and data access objects that are not tied to a particular Java EE service. There is no doubt that such objects can be reused across different Java EE environments (Web or EJB), standalone applications, and test environments.

   IOC and AOP

The basic concept of control reversal mode (also known as dependency intervention) is that you do not create objects, but describe how they are created. The code does not directly connect to objects and services, but describes in the configuration file which component requires which service. Containers (which are IOC containers in the Spring framework) are responsible for linking these together.

In a typical IOC scenario, the container creates all the objects and sets the necessary properties to connect them together, deciding when to invoke the method. The following table lists an implementation pattern for the IOC.

Type 1 Services need to implement specialized interfaces through interfaces that provide these services from objects, and can query dependencies from objects (for example, additional services needed)
Type 2 Assigning dependencies through JavaBean properties, such as setter methods
Type 3 Dependencies are provided as constructors and are not exposed as JavaBean properties

The IOC container for the Spring framework is implemented with type 2 and type 3.

   aspect-oriented programming

Aspect-oriented programming, AOP, is a programming technique that allows programmers to modularize the behavior of crosscutting concerns or crosscutting typical lines of responsibility, such as logging and transaction management. The core structure of AOP is the facet, which encapsulates behaviors that affect multiple classes into reusable modules.

AOP and IOC are complementary technologies that use a modular approach to solve complex problems in enterprise application development. In a typical object-oriented development approach, logging statements may be placed in all methods and Java classes in order to implement logging functionality. In the AOP approach, the log service can be modularized in turn and applied declaratively to the component that needs the log. Of course, the advantage is that the Java class does not need to know the existence of the log service and does not need to consider the associated code. Therefore, application code written in Spring AOP is loosely coupled.

The functionality of AOP is fully integrated into the context of Spring transaction management, logging, and various other features.

   IOC Container

The core of the Spring design is the Org.springframework.beans package, which is designed to be used with the JavaBean component. This package is not usually used directly by the user, but is used by the server as the underlying intermediary for most other functions. The next highest-level abstraction is the Beanfactory interface, which is the implementation of the factory design pattern, allowing objects to be created and retrieved by name. Beanfactory can also manage the relationships between objects.

The beanfactory supports two object models.

- Single StateThe model provides a shared instance of an object with a specific name that can be retrieved at query time. Singleton is the default and most commonly used object model. Ideal for stateless service objects.

- prototypesThe model ensures that each retrieval creates a separate object. The prototype model works best when each user needs his or her own object.

The Bean Factory concept is the basis of Spring as an IOC container. The IOC shifts responsibility for handling things from application code to frameworks. As I will demonstrate in the next example, the Spring framework uses the JavaBean property and configuration data to indicate the dependencies that must be set.

Introduction to the Spring Framework Quick Start

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.