Understanding the Java Spring Framework

Source: Internet
Author: User
Tags java spring framework

Talking about Java development is always inseparable from a variety of frameworks, when we use Java in the early days are surrounded by various frameworks, and. Net development is relatively simple, from WinForm to ASP to Aps.net MVC, one go, developed it called a cool, but also brought some problems, For example,. Net developers use Microsoft's already packaged library, programmers in the daily development process, do not know how sophisticated the class library they use, but Java is not the same, it is completely open source, can be loaded according to their own needs of the appropriate class library, as a developer can know the bull code written by Daniel , you can also see the beauty of refactoring the code. Today we are going to talk about the spring framework of Java, where there are discrepancies that I would like you to point out.

1. What is the Spring framework

Baidu Encyclopedia explanation is: Spring is an open source framework, it is to solve the complexity of enterprise application development and created. We know from the literal meaning that he is to solve the complexity of the development, he is how to solve the complexity of the problem, we must start from the characteristics of spring

Spring Features:

    1. The main advantage is the hierarchical structure, the spring framework consists of a total of seven parts, each can be split independently (not strictly speaking), the hierarchical architecture allows users to choose which component to use, while providing an integrated framework for Java EE application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. The core of spring is control rollover (IOC) and facet-oriented (AOP). In short, spring is a layered, javase/eefull-stack (one-stop) lightweight, open-source framework.
    2. Lightweight: Spring is lightweight in terms of size and overhead, and spring is a non-intrusive framework, meaning that spring does not depend on a particular class (praise). Spring is based on the inversion of Control (IoC) that facilitates low coupling

    3. Aspect-oriented (AOP): In today's object-oriented programming of the current process, AOP presents a new way of programming, and Spring provides rich support for aspect-oriented programming, the business logic of separate applications, and the cohesive development of system services, such as transaction control, exception handling, and so on. So we don't have to worry about how we're going to catch the anomaly when we're developing each function module.
    4. Container: Spring contains and manages the configured life cycle of the Application object, in this sense it is a container where you can configure how each of your beans is created-based on a configurable prototype (prototype), Your bean can create a separate instance or generate a new instance each time it is needed-and how they relate to each other.

We talked about IOC and AOP, so here's the question, what is the IOC? Let's talk specifically about IOC and AOP

    • IoC inversion of control, Chinese meaning we translate into controlled inversion, we may not understand the literal meaning, we still specifically look at the chart:

Assuming a business scenario, we declare a, B, C, D four modules, they need collaborative configuration cooperation, in the traditional development such as:

We need to quote OBJECTB and C in a to what we should do:

1 class objecta{2     OBJECTB objb=New  OBJECTB (); 3     OBJECTC objc=New  objectc ();   4 }

In the above code, the Objecta class directly refers to OBJECTB and OBJECTC, so the control of OBJECTB and OBJECTC is created in objecta, so what's the problem? With the increasing scale of industrial applications and the increasing complexity of dependencies between objects, there are often multiple dependencies between objects, so architects and designers will face greater challenges in system analysis and design. The coupling between the object and the high system will inevitably appear reaching, the coupling degree is too high

In order to solve this problem, we have emerged the IOC thought

Add a mediation class between Objecta and OBJECTB and C so that all object creation work is mediated

So what would happen if we removed the intermediate IOC container?

In this way, there is no coupling between the objecta\b\c\d, these four parts of the component can be independently developed testing, finally through the IOC integration can be, participate in the development of each member as long as the implementation of their own classes can be, and no relationship with others!

So control inversion is the reversal of the control that creates the object, which is delegated to the IOC container by the original user. Because the inversion of control is not good to understand, Martin Fowler in 2004 raised the conclusion that what was reversed was that "the process of acquiring dependent objects was reversed", and that the process of acquiring dependent objects was changed from self-management to active injection by the IOC container after the control was reversed. So he gave the "inversion of control" a more appropriate name called "Dependency Injection (Dependency injection)". His answer, in effect, gives a way to implement the IOC: inject. The so-called dependency injection is that the IOC container dynamically injects some kind of dependency into the object during operation.

    • AOP: Plane-oriented programming

AOP is the derivation of OOP, we are now object-oriented programming, we implement a module function is, the code is written as follows:

 Public class objecta{    publicvoid  dosomething ()    {        connbegin ();        Tranbegn ();        Dobesiness ();        Tranend ();        Connend ();    }}

In the above code, we can see that Connbegin () and Connend () are links and close links, Tranbegn () and Tranend () are the start and end of transactional operations, while Dobesiness () is the real business processing logic, I believe we can see the problem, our real business processing logic is surrounded by a lot of system operations, so we need to spend time and energy to write business-independent code, such a programming style violates the design pattern of a single responsibility principle.

AOP aspect-oriented programming is horizontal slicing of individual modules, centralizing the common code and functionality between modules, as shown in:

Abstract the various system functions that are not related to business logic in the module

The main features of AOP:

The code for logging, performance statistics, security control, transaction processing, exception handling, and other code is separated from the business logic code, and by separating these behaviors, we want to be able to separate them into non-instructional methods of business logic, and then change these behaviors without impacting the business logic.

Of course, AOP has drawbacks too: it's too complex; it destroys encapsulation; it requires a special compiler.

Understanding the Java 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.