Spring Framework and MVC principles

Source: Internet
Author: User

Spring Framework and MVC principles

Directory

    • Spring Framework
    • SPRINGMVC Working principle
    • Resources
Back to Top Spring Framework

Spring's current framework has a jar package that can be roughly divided into 6 main modules:

    • Core Container
    • AOP and Instrumentation
    • Messaging
    • Data access/integration
    • Web
    • Test

The spring framework provides a very rich feature, so the entire architecture is huge.

In our actual application development, it is not necessary to use all the functions, but to select the appropriate spring modules as needed.

Figure-spring Frame Chart (official website document)

Spring Key Strategy

Before introducing the six modules, it is important to first explain spring's two key strategies, which are the cornerstone of the spring framework.

(1) controlled inversion (inversion of control), English abbreviation IOCApplying control inversion, when an object is created, it is passed to it by an external entity that controls all objects in the system by a reference to its dependent object.

It can also be said that the dependency is injected into the object. So, control reversal is a reversal of this responsibility about how an object obtains a reference to the object he relies on.

There are two main aspects of control inversion:

    • Dependency Injection (Dependency injection), abbreviated as DI
    • Dependency lookup (Dependency lookup)

Among them, the application of dependency injection is more extensive.

Dependency Injectionis a technique that refers to a component that does not make a location query, but only provides common Java methods for the container to determine dependencies.

The Assembly of the container's full-fledged component, which passes the object that conforms to the dependency to the desired object through the JavaBean property or constructor function.

The practice of injecting dependencies through the JavaBean attribute is called SetPoint method Injection (Setter injection); the practice of passing dependencies as constructor arguments is called constructor injection (Constructor injection) .

(2) Plane-oriented programming (Aspect oriented Programming), abbreviated to AOP

A technology that implements the unified maintenance of program functions by means of precompilation and runtime dynamic agent.

AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.

core containers (cores Container)

The IOC container is the core of the spring framework. The spring container uses Dependency Injection Management to form the component of the application, which creates an association between the components that work together. Without a doubt, these objects are simpler, cleaner, easier to understand, and easier to reuse and test.

Spring comes with implementations of several containers, which can be summed up in two categories:

(1) Bean Factory

Defined by the org.springframework.beans.factory.BeanFactory interface.

It is the simplest container that provides basic DI support.

(2) Application context

Defined by the org.springframework.context.ApplicationContext interface.

It is built on top of Beanfactory and provides application-oriented services, such as the ability to parse textual information from a property file, and the ability to publish application events to listeners of interest.

Note: The Bean factory is often too low-level for most applications, so the application context is more widely used. It is recommended to use the application context container in development.

Spring comes with a variety of application contexts, most likely to be encountered in the following ways:

    • classpathxmlapplicationcontext--load the context definition from the XML configuration file under the Classpath and use the context definition file as a class resource.
    • Filesystemxmlapplicationcontext-Reads the XML configuration file under the file system and loads the context definition.
    • Xmlwebapplicationcontext-Reads the XML configuration file under the Web application and loads the context definition.

The following is an example of loading an app context

ApplicationContext context = new Filesystemxmlapplicationcontext ("D:\\temp\\build.xml");
ApplicationContext context2 = new Classpathxmlapplicationcontext ("Build.xml");

As you can see, loading filesystemxmlapplicationcontext and Classpathxmlapplicationcontext are very similar.

The difference is that the former locates the build.xml file under the specified file system path, and then finds the Build.xml file under all classpath (including jar files).

By referencing the application context, it is convenient to call the Getbean () method to get the Bean from the Spring container.

Related jar Packages spring-core, Spring-beans, provides a fundamental part of the framework, including IOC and dependency injection characteristics.
Spring-context, built on the basis of Spring-core, Spring-beans. It provides a way to access the object in a frame style.
It also supports Java EE-like features such as: EJB,JMX and basic remoting. The ApplicationContext interface is its focal point.
Springcontext-support, integrating third-party libraries into spring application context.
Spring-expressionprovides a powerful expression language at runtime to query and manipulate an object graph.

AOP and Instrumentation The associated jar package SPRING-AOPprovides rich support for slicing-oriented programming. spring-aspectsprovides the integration of the ASPECTJ. Spring-instrument, provides support for class instrumentation and class loaders. Spring-instrument-tomcat, which contains spring's instrumentation proxy for Tomcat.

Messaging The associated jar package , spring-messaging, contains the message processing features of spring, such as Message,messagechannel,messagehandler.

data access and Integration (access/integaration)The Data access/integration layer contains the JDBC/ORM/OXM/JMS and transaction modules. The associated jar package , Spring-jdbc, provides a JDBC abstraction layer.
Spring-tx, supports programmatic and declarative transaction management classes.
Spring-orm, provides a collection of popular object-relational mapping APIs, such as Jpa,jdo,hibernate.
SPRING-OXMprovides an abstraction layer to support the implementation of object/xml mappings, such as JAXB,CASTOR,XMLBEANS,JIBX and XStream.
SPRING-JMS, which includes the ability to produce and consume messages.

Web Related jar Packages spring-webprovides basic web-oriented features, such as multi-file uploads, and the initialization of an IOC container using a servlet listener. A web-oriented application layer context.

Spring-webmvc, including MVC and Rest Web service implementations.

Spring-webmvc-portlet, provides an image of the MVC implementation and SPRING-WEBMVC functionality in the Protlet environment.

Testing (Test) related jar package spring-test, with JUnit and testng to support unit testing and integration testing of spring components.

Back to Top SPRINGMVC Working principle

In the previous chapter [spring]01_ environment Configuration, we demonstrated a simple HelloWorld project.

This chapter focuses on the basic work principle of SPRINGMVC, so that we further deepen the understanding.

SPRINGMVC Work Flow description

(1) Send an HTTP request to the server and the request is captured by the front controller dispatcherservlet .

(2) Dispatcherservlet The requested resource Identifier (URI) according to the configuration of the <servlet-name>-servlet.xml to parse the URL of the request.

Then, based on the URI, call handlermapping to get all the related objects of the handler configuration (including the handler object and the interceptor corresponding to the handler object), and finally returns the form of the Handlerexecutionchain object.

(3) Dispatcherservlet according to the obtained handler, choose a suitable handleradapter. (Note: If Handleradapter is successfully obtained, the Interceptor Prehandler (...) will start executing. method).

(4) Extract the model data from the request, populate the handler entry, and start the execution of the handler (Controller). In the process of populating the handler, depending on your configuration, spring will do some extra work for you:

    • Httpmessageconveter: Converts a request message (such as JSON, XML, and so on) into an object, converting the object to the specified response information.
    • Data transformation: Data conversion for a request message. such as string conversion to Integer, double, and so on.
    • Data is initialized: Data is formatted for the request message. such as converting a string into a formatted number or a formatted date.
    • Data validation: Verify the validity of the data (length, format, etc.), and verify that the results are stored in Bindingresult or error.

(5) when the Handler (Controller) executes, returns a Modelandview object to Dispatcherservlet;

(6) According to the returned Modelandview, select a suitable viewresolver(must be a viewresolver already registered in the spring container) to return to Dispatcherservlet.

(7) Viewresolver combines model and view to render the view.

(8) The view is responsible for returning the rendered result to the client.

Back to Top References Spring Combat (Third edition)

Spring Official Document--spring-framework-reference

http://blog.csdn.net/xtu_xiaoxin/article/details/8796499

Category: EE Tags: springmvc

Spring Framework and MVC principles

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.