Spring source code analysis begins: What is Spring?

Speaking before the source code, let us review the basic concepts of Spring, of course, before you need to look at the source code used spring or spirngmvc.

What is Spring

Spring is an open source lightweight Java SE (Java Standard Edition) / Java EE (Java Enterprise Edition) application development framework, which aims to simplify enterprise application development. Application is a set of objects that work together. In the traditional application development, a complete application consists of a set of objects that work together. Therefore, development of an application in addition to developing business logic, most are concerned about how to accomplish the desired function of these objects collaboration, but also to low coupling, high cohesion. Business logic development is inevitable, and that if there is a frame out to help us to create objects and manage dependencies between these objects. Some people may say, such as "Abstract Factory, Factory Method design pattern " can not help us create an object, "generator mode" to help us deal with the dependencies between objects, can not perform these functions do? But these need us to create some other factory class, builder class, we have to manage out of these classes, increase our burden, if we can create an object by a kind of configuration, management of dependencies between objects, we do not We need to be created by the plant and generator and manage dependencies between objects, so that we are not reducing a lot of work, speed up development, can save a lot of time to do other things. Spring Framework is mainly to complete this function just came out.

Spring Framework in addition to helping us manage objects and their dependencies, like General also provides logging, performance statistics, security control, exception handling aspect-oriented capabilities, but also help me manage the most troublesome database transaction itself provides a simple the JDBC access implementation, and provide access to third-party data integration framework (such as Hibernate, JPA), with a variety of Java EE technology integration (such as Java Mail, task scheduling, etc.), providing a layer of its own web framework Spring MVC, and but also very simple integration with third-party web framework. From here we can think Spring is a super adhesive platform, in addition to providing their own functions, the ability of other technologies and frameworks also provide adhesion, so that we can more freedom of choice in the end what technology to use for development. And whether JAVA SE (C / S architecture) application or JAVA EE (B / S architecture) applications can use this platform for development. Let's take a deep look at the Spring in the end help us to do something?

Spring What can we do

Spring addition to not help us write the business logic, the rest almost anything can help us simplify the development of:

First, the traditional application development, create dependencies between objects and object control in the assembly by our internal procedures, this will increase the coupling between various objects, if we want to modify the dependencies between objects must modify the source code, recompile, deployment; and if the use of Spring, by Spring to create dependencies between objects and assembled according to the configuration file, you only need to change the configuration file, without recompiling. So, Spring can help us create dependencies between the configuration file and assemble objects.

Second, when we want to do some logging, access control, performance statistics and so on, we may be in need of an object or a method in which legacy applications, such as access control and performance statistics mostly repetitive, so the code there is a lot of duplicate code, even though some people say I have a generic part is extracted, and that there must be invoked or repeat, we might just as performance statistics only when necessary were present, after the diagnosis is complete to remove this code; there is logging, Some methods such as records access log, access log data, etc., which will permeate all want to access methods; as well as access control, must begin to review the implementation of the method, which is a terrible and think how boring is working. If Spring, these log records, access control, performance statistics separated from the business logic out by Oriented Programming Spring supports dynamically add these features where needed these functions, without penetration into the individual in need thereof or a subject; Some people might have said, we can use "proxy design pattern" or "wrapper design mode", you can use these, but still need to create the proxy objects programmatically, or to couple these proxy objects, while the use of Spring Aspect oriented programming to provide a better way to accomplish the above functions, generally through configuration, and does not require any additional code to the existing code in the code focused existing business logic. So, Spring aspect-oriented programming can help us achieve logging without coupling, performance statistics, security control.

Third, in traditional applications which, how do we complete the database transaction management? Requires a series of "Get connected, execute SQL, commit or roll back the transaction, the connection is closed", but also to ensure that the final must close the connection, terrible thing, but also very boring; if using Spring, we only need to get connected , execute SQL, others were handed over to Spring to manage, and simple. So, Spring can be very simple to help us manage the database transaction.

Four, Spring also provides seamless integration with third-party data access framework (such as Hibernate, JPA), but also provides its own set of JDBC access templates to facilitate database access.

Five, Spring also provides seamless integration with third-party Web (such as Struts, JSF) framework, but also provides its own set of Spring MVC framework to facilitate web layer structures.

Six, Spring can easily and Java EE (such as Java Mail, task scheduling) integration, and more technology integration (such as caching framework).

Spring can help us do so many things, offers so many features and integration with so many mainstream technology, but also help us to do development in a troublesome and difficult thing, and that some people may ask, is this just a Spring framework, no other options? Of course there are, for example, need to rely on EJB application server, development of low efficiency in the development of small and medium sized projects is to take slaughtered chicken chopper, although developed to EJB relatively easy to use, but it is quite bulky but also need to rely on application servers. Why that requires the use of Spring, rather than the other frameworks do? Let's read on.

Why do we need Spring

A first elaborated several concepts

1, Application: It is finished we need to complete the function, such as shopping sites, OA system.

2, frame: is able to complete certain functions of semi-finished products, for example, we can use the framework to develop a shopping site; do the framework part of the function, do our own part of the function, so that applications created out. And the framework sets out the overall architecture of the time you're developing an application, provide some basic functions, but also specifies how to create classes and objects, and how collaboration, we have developed to simplify, let us focus on business logic development.

3, non-intrusive design: Can be understood from the perspective of the frame without inheriting the class framework provided by this design can be seen as a non-intrusive design, if inherited these framework classes, it is intrusive design, if you later want to change the frame before written almost impossible to reuse the code, if the non-intrusive design is written before the code can continue to use.

4, Lightweight and Heavyweight: Heavyweight lightweight relative terms, lightweight general is non-invasive, rely on something very small, resource consumption is very small, simple to deploy, and so, in fact, easier to use, while the opposite is true heavyweight.

5, POJO: POJO (Plain Old Java Objects) simple Java objects, which can contain business logic or persistence logic, but does not play any special role does not inherit or implement any other Java class or interface framework.

6, the container: in everyday life is a kind of container containing assembly of things, from the perspective of programming objects is loaded object, because there into, out of, etc, so also life-cycle management of container objects.

7, the control reverse: i.e. Inversion of Control, abbreviated as the IoC, there is a name called inversion of control dependency injection (Dependency Injection), a relationship between the containers is a control program, rather than the traditional implementation, program code directly control.

8, Bean: management object generally refers to a container, Spring IoC container Spring managed objects in the middle.

Why do we need two advantages of Spring and Spring

● very lightweight container: a centralized, automated way to create application objects and assembly, and the assembly is responsible for object creation, object lifecycle management, can be combined into complex applications. (Does not depend on any particular class of Spring) Spring container is non-invasive, and completely POJOs development, make the application easier to test, easier to manage. And the core JAR package is very small, Spring3.0.5 less than 1M, and does not depend on any application server that can be deployed in any environment (Java SE or Java EE).

● AOP: AOP Aspect Oriented Programming is the acronym meaning Oriented Programming, from another point of view to provide the program to improve the structure of object-oriented programming (with respect of OOP), i.e., during implementation at compile time or run through during loading without modifying the source code of a technique to dynamically add functionality to the program. Popular point that is to extract reusable function out, then these generic functions at the right time woven into the application; such as security, logging, these are common features, we can extract them, then suitable woven into the local program executing code and executes them, thereby completing the required functions and multiplexes these features.

● simple database transaction management: the applications that use the database among their own management database transaction is a very vexing thing, and very prone to error, Spring supports pluggable transaction management support, and without JEE environment support, We can liberate us through Spring managed transactions from a transaction management out to focus on business logic.

● JDBC abstraction framework and ORM support: Spring JDBC make it easier to use; provide DAO (Data Access Object) support, very easy to integrate third-party ORM framework, such as Hibernate and so on; and fully supports the consistent exception hierarchy Spring transaction and use Spring offers .

● Flexible Web tier support: Spring itself provides a very powerful MVC framework, and can be easily integrated with third-party MVC frameworks such as Struts and so on.

● simplify the integration of various technologies: to provide integrated Java Mail, task scheduler, JMX, JMS, JNDI, EJB, dynamic languages, remote access, Web Service, etc.

Spring can help us simplify application development, help us create and assemble objects for our management services, simple MVC framework, Spring can be seen as a super-adhesive platform, we can integrate many technologies together to form a whole, the system architecture better, more superior performance to accelerate our development program, like the advantage, we have no reason not to consider it.

How to Learn Spring

To learn Spring, we must first clear Spring is something, help us to do something, and then to be aware of these simple examples, so you basically know how to use the Spring. Spring is the core IoC container, so be sure to thoroughly understand what IoC container, and how to configure and use the container, the container all the other techniques are based on implementation; after a good understanding of IoC, followed Oriented Programming, above all, a clear concept, the basic configuration, and finally the realization of the principle, the next step is database transaction management, transaction management is in fact Spring aspect-oriented programming by, so the foundation is very important, IoC container and post-oriented programming done, the rest are realized maybe something based , to learn even more relaxed. Spring can not be anxious to learn, must take lay a solid foundation, a solid foundation, this is the work may quicken.

Spring basis

Spring Chart

 

Figure 1-1 Spring Chart

Core container: including Core, Beans, Context, EL module.

● Core module: encapsulates the bottom portion of the frame-dependent, including resource access, and cast some common tools.

● Beans module: provides a base portion of the frame, including inversion control and dependency injection. Bean Factory is a container wherein the core is essentially to achieve "factory design pattern", and without programming "singleton design pattern" singleton entirely controlled by the container, and to promote oriented programming interface, rather than to achieve oriented programming; all applications the relationship between objects and objects managed by the framework, which really put you to maintain dependencies between objects extracted from the program logic, all these dependencies by BeanFactory to maintain.

● Context module: The Core and Beans is based on an integrated module features and add resources Beans Binding, data validation, internationalization, Java EE support, container life cycle event dissemination; core interface is ApplicationContext.

● EL module: Provides powerful expression language support, support access and modify property values, method calls, access and modify the array support, container and indexer, named variables, arithmetic and logic operations support, support access from Bean Spring container, it also supports the projection list, select the list and the general polymerization.

AOP, Aspects module:

● AOP modules: Spring AOP module provides aspect-oriented programming AOP Alliance-compliant in (aspect-oriented programming) implemented, there is provided such as logging, access control, performance statistics and other general functions and business logic in the art, and can dynamically these features add to the code you need thereof; such that the tertiary level, to reduce the coupling and general business logic functionality.

● Aspects Module: provides for the integration of AspectJ, AspectJ provides more powerful than Spring ASP functionality.

Data Access / Integration Module: This module contains JDBC, ORM, OXM, JMS and transaction management.

● Transaction Module: This module is used in Spring managed transactions, as long as the benefits of managed objects can get Spring Spring managed transactions without transaction control in your code, and support the management of things and declarative programming.

● JDBC Module: provides a JBDC sample template, using these templates can eliminate the tedious JDBC coding as well as traditional transaction control must, and can enjoy the benefits of Spring managed transactions.

● ORM module: provides popular - seamless integration "object relation" mapping framework, including Hibernate, JPA, Ibatiss like. And you can use Spring transaction management, without additional control transactions.

● OXM module: it provides a mapping to achieve Object / XML, java mapped object to an XML data, or XML data into mapped java object, Object / XML mapping implementations include JAXB, Castor, XMLBeans and XStream.

● JMS Module: for JMS (Java Messaging Service), providing a "message producers, message consumers" template for simpler use JMS, JMS used for between two applications, or distributed message transmission system, asynchronous communication.

● Web / Remoting modules: Web / Remoting module contains Web, Web-Servlet, Web-Struts, Web-Porlet module.

● Web module: provides basic web functions. Such as multi-file upload, integrated IoC container, remote access process (RMI, Hessian, Burlap) and Web Service support, and provide a RestTemplate class to provide easy access Restful services.

● Web-Servlet module: Provides a Spring MVC Web Framework implementation. Spring MVC framework provided upon request resource injection annotations, simpler data binding, data validation and a very easy to use JSP tags, completely seamlessly with other technologies Spring.

● Web-Struts module: Provides seamless integration with Struts, Struts1.x and support Struts2.x

Test Module: Spring support Junit and TestNG testing framework, but also provides some additional functions Spring-based tests, such as when testing Web framework, analog functions Http request.

Typical application scenarios

Spring can be applied to many scenarios, from the most simple standard Java SE program to enterprise applications can be built using Spring. Here are a few of the more popular scenarios:

● typical Web application scenarios:

 

FIG. 1-2 web application scenarios

In a Web application scenarios, the typical three-tier architecture: data model layer implementation domain objects; data access layer for data access; logic layer implement business logic; web layer provides page display; all of these layers by Spring components to manage, enjoy Spring transaction management, the AOP and other benefits, but the request is the only entrance DispachterServlet, it functions to implement the corresponding request by the request is mapped to the respective web layer assembly.

● remote access scenarios:

Spring can be very convenient to provide exposure RMI services, remote access services, such as Hessian, Burlap, etc., to achieve a very simple just by configuring the appropriate address and the service needs to be exposed in Spring can be easily achieved, behind will be introduced;

● EJB application scenarios:

Spring can also be easily integrated with EJB, behind will be detailed.

Note: If you want more in-depth understanding of IoC and DI, please refer to the masters Martin Fowler's a classic article "Inversion of Control Containers and the Dependency Injection pattern", the original address: http://www.martinfowler.com/ Articles / injection.html .

Guess you like

Origin www.cnblogs.com/xll1025/p/11407665.html