Set out
1 Introduction
The next period of time, I will learn my spring source of relevant experience to collate, publish on my blog. For themselves, able to record their growth journey, be able to learn new things. For others, to share my understanding and relevant experience, the students want to dig deep spring of hope can play a little help.
spring the whole system is very large, need to learn the method. My approach is summarized here:
- 1, to find the entrance, that is, looking for the point to start learning. This spring is the core process for entry point: IOC, DI and AOP, MVC is how to achieve the body.
- 2, drawing. After finding the entry, each key of the class and its methods of recording, eventually carded a timing chart. For some key classes, class diagrams to draw a clear inheritance relationship between them or implement to help our whole spring inheritance system.
- 3, Duokanjibian, go a few times, and learn new things. According to his summary of the timing chart, Duokanjibian source, according to debug the process go a few times in general, understand what made each core process.
- 4, stick to it. Look at the source code of the process is very boring, very strange for the code, often have to give up the idea, but please keep, keep to that you can gain something.
- 5, learn to summarize. Through a period of study, please summarize implement the principles of the core functions of the spring are used to summarize spring design patterns and design ideas. Because by learning the source, one can understand spring, and second, to understand their design ideas by predecessors wrote the code, and after practice to their own projects.
I hope that after learning spring, can reach know these know why realm. (I know that it is so, but know why it is like this.)
2, designed
When learning any language or framework, we need to know what the object of our study it was originally designed, the next series of the code is to achieve and continue to go around this iteration of the original intention. So what Spring is designed yes.
Spring is to address the complexity of enterprise application development and design, it can do many things, but ultimately only support Spring
is only a few basic ideas, and all of these basic idea can be traced back to one of the most fundamental mission : simplify the development of (the existence of any fact a framework to simplify development).
This is a solemn promise, in fact, many frameworks have claimed in some ways made simplified. The Spring is determined to simplify all aspects of Java development.
In this regard, she has taken the four key strategies
- 1, based POJO lightweight and minimally invasive programming;
- 2, through the interface for loose coupling and dependency injection;
- 3, based on the declarative programming section and inertia;
- 4, by reducing the template and the boiler-section of code;
And he was mainly through: for Bean (BOP), dependency injection (DI) and aspect-oriented (AOP) of these three ways to achieve.
Here probably introduced to these ideas.
3, BOP program beginning
Spring is the Bean-oriented programming (Bean Oriented Programming, BOP), Bean in Spring is the real
protagonist. Bean Object as the significance of OOP in the same role in Spring, Spring Spring is not Bean would be no
meaning of existence. Spring IOC container provided by way of the configuration file or annotations to manage dependencies between objects.
Inversion of Control (the most common implementation is called dependency injection (Dependency Injection, DI), there is a way called
"dependent lookup" (Dependency Lookup, DL), she used in C ++, Java, PHP, and .NET. in the earliest
Spring is included dependency injection method and rely query, but because that queries use frequency is too low, Spring soon to be removed
, so in Spring inversion of control, also known as direct dependency injection), she the basic concept is: do not create objects, but to create a description of
the way they are. Not directly connected with the object and the service code, but which is described in the configuration file which requires a service component. Container
device (IOC container is in the Spring Framework) responsible for these together.
IOC In a typical scenario, the container creates all the objects, and set the necessary properties to connect them together, decide what time to call the method.
4, the basic concept of dependency injection
Spring Design's core org.springframework.beans package (core architecture is org.springframework.core
package), it is designed for use with JavaBean components. This package typically is not used directly by the user, but by a server to
use as the underlying interposer most other functions. The next highest level of abstraction is the BeanFactory interface, it is the realization of the factory design pattern,
it allows to create and retrieve objects by name. BeanFactory relationships between objects can also manage.
BeanFactory supports two lowest level object model.
- 1, the single Example: provides a global shared instance of an object with a particular name, it can be retrieved in the query. Singleton is the default
and most common object model identified. - 2, Prototype: to ensure that each retrieval will create a separate instance of an object. When each user needs its own object, using the prototype model.
The concept is to plant Spring Bean as a basis for IOC container. IOC will be the responsibility of handling things is transferred from the application code into
the framework.
5, AOP programming concept
Aspect Oriented Programming, namely AOP, is a programming idea, it allows programmers to crosscutting concerns or cross boundaries of responsibility typical
behavior (such as logging and transaction management) modular. The core structure is the aspect of the AOP (cut), it will affect those rows plurality of classes
is packaged into reusable modules.
AOP and IOC are complementary technologies, they all use a modular approach to solve complex problems in enterprise application development. In a typical
type of object-oriented development approach, you may want to log all statements in the Java classes and methods to achieve logging. In the AOP
mode, you can turn the log service modularity, and declaratively apply them to the components need to log in. Of course, the advantage
is that Java class does not know the existence of the log service does not need to consider the relevant code. So, with Spring AOP should be written in
the application code are loosely coupled.
AOP functionality is fully integrated into the Spring transaction management, context logs and various other characteristics.
AOP programming of commonly used scenes have: Authentication (certification authority), Auto Caching (automatic caching), Error Handling
(Unified error handling), Debugging (debug information output), Logging (logging), Transactions (transaction processing)
and so on.
6, Spring5 System Architecture
Spring total of about 20 modules, a plurality of files of different configuration 1300. These components are respectively integrated in the core container (Core
Container), AOP (Aspect Oriented Programming) and device support (Instrmentation), data access
and integration (Data Access / Integeration), Web , message transmission (Messaging), Test, 6 collection modules. In
case of FIG. 5 is a block configuration Spring:
Composition Spring framework set each module or modules may be present alone, may be combined to achieve one or more modules. Each molding
composition and function block is as follows:
6.1, the core container
A spring-beans, spring-core, spring-context and-expression The Spring (the Spring the Expression Language,
of SpEL). 4 modules.
- spring-core and spring-beans module is the core module of the Spring Framework, comprising the inversion of control (Inversion of
Control, the IOC) and dependency injection (Dependency Injection, DI). BeanFactory interface framework is the Spring core
center interface, which is embodied in the factory model. BeanFactory inversion of configuration using the control and application dependent specification and the actual
application code were separated. However BeanFactory container instantiated does not automatically instantiate Bean, only if the Bean is
BeanFactory Bean the container will be instantiated and is assembled using dependencies. - spring-context module architecture on top of the core module, he extends BeanFactory, adding the Bean for her life cycle control
system, system event framework and resource loading features such as transparency. In addition, the module also provides a number of enterprise-level support, such as e-mail access, far
away visit, task scheduling, ApplicationContext is the core interface of the module, she superclass is BeanFactory. And
BeanFactory different container instances of the ApplicationContext automatically instantiated with all single instance by Bean
assembly depends relations, so that in the standby state. - spring-context-support module is an extension support for Spring IOC containers, sub-containers and IOC.
- spring-context-indexer module Spring class management component and Classpath scanning.
- spring-expression modules are unified expression language (EL) expansion module, you can query, management and operation of the object, the same
time also may be convenient to call the object methods manipulate arrays, collections and so on. Its syntax is similar to the conventional EL, but provides additional features, the most
template function well to the number of function calls and a simple string. Characteristics of the language is based on the needs of the Spring product design, he
can easily interact with Spring IOC.
6.2, AOP and device support
A spring-aop, spring-aspects and spring-instrument 3 pieces of module
- Spring is spring-aop another core module, AOP is the main implementation module. As Following OOP, a programmer shadow of
one of the biggest programming ideas sound, AOP has greatly opened up people's ideas for programming. In Spring, he is the JVM dynamic
agent technology, and then design a series of crosscutting AOP implementations, such as pre-notification, return, throws advice etc., with
time, Pointcut the interface to match the starting point, you can existing pointcut designed cross-section, it may be extended on demand related method
for cutting. - spring-aspects module integrates self AspectJ frame, mainly to provide a variety of implementations of AOP Spring AOP.
- spring-instrument module is based on the JAVA SE "java.lang.instrument" design, the AOP should be considered
when a support module, the main role is in the JVM is enabled, you generate a proxy class, the programmer run through the proxy class change the class of
bytes, thereby changing the functionality of a class, to achieve AOP function.
6.3, Data Access and Integration
A spring-jdbc, spring-tx, spring-orm, spring-jms and spring-oxm 5 modules.
- spring-jdbc implementation module is the main module JDBC abstraction framework provided by Spring, Spring JDBC for simplified operation.
The main way to provide JDBC template, relational database object-oriented way, SimpleJdbc way, transaction management to simplify JDBC programming,
mainly the implementation class is JdbcTemplate, SimpleJdbcTemplate and NamedParameterJdbcTemplate. - spring-tx Spring JDBC transaction control module is a module implemented. Using the Spring framework, it has done a very good package Affairs,
through its AOP configuration, flexible configuration at any level; but in many needs and applications, direct JDBC transaction control
system still has its advantages. In fact, business is business logic-based; a complete business should correspond to a method in the business layer
if the business fails, the entire transaction is rolled back; therefore, it is absolutely transaction control should be placed in the service layer; however, long-lasting layer design
should follow a very important principle: atomicity operations to ensure that the persistence layer in each method should not be divided. The
order, in the use of Spring JDBC transaction control, should pay attention to its particularity. - spring-orm ORM framework support module module is mainly integrated Hibernate, Java Persistence API (JPA) and
Java Data Objects (JDO) for resource management, Data Access Objects (DAO) implementation and transaction strategies. - spring-oxm module is mainly to provide a layer of abstraction to support OXM (OXM is an abbreviation for Object-to-XML-Mapping, and
it is an O / M-mapper, mapping java object into XML data, or to map the XML data into a java object ), for example:
JAXB, the Castor, the XMLBeans, XStream the JiBX and the like. - spring-jms module (Java Messaging Service) to send and receive information to from the Spring Framework 4.1
, he also provides a support for the spring-messaging module
6.4, Web Components
The spring-web, spring-webmvc, spring-websocket and spring-webflux 4 modules.
- spring-web module provides support for most basic Web Spring, mainly built on top of the core container, Servlet or by
those who Listeners to initialize the IOC container, also includes some Web-related support. - spring-webmvc module is a well-known of Web-Servlet module, MVC implements the Spring
(Model-View-the Controller) Web applications. - spring-websocket module is mainly full-duplex communication protocol with the Web front-end.
- spring-webflux is a new non-clogging functional Reactive Web framework that can be used to create an asynchronous, non-blocking,
event-driven service, and scalability is very good.
6.5, communication 报文
That spring-messaging module, from the beginning Spring4 added a new module, the main responsibility for the Spring Framework set
into some basic messaging applications
6.6, integration testing
That spring-test module, providing support primarily for testing, after all, does not need to release (program) to your application server or connection
can perform some integration testing or other testing is very important for any business to other companies in the case of facilities of
6.7, compatible integration
I.e., spring-framework-bom module, Bill of Materials. Spring address different modules of different versions dependency problems
6.8, dependencies between modules
Learning basic order: starting from the spring-core, followed by spring-beans and spring-aop, followed spring-context, and then followed by spring-tx and spring-orm, and finally the spring-web and other parts.