Spring Past and Present

 

 

Spring Past and Present

  I believe having gone through not using a framework for developing web projects 70, 80, there will be such a perception, today's programmers to develop projects too easy, basically only need to care about how to achieve business, technology can only integrated framework. Back in 2007, a framework based on open-source Java language officially released, took a very dynamic and beautiful name, called Spring. It is an open source lightweight Java SE (Java Standard Edition) / Java EE (Java Enterprise Edition) application development framework, its purpose is to simplify enterprise application development. In traditional application development, a complete application consists of a set of objects that work together. Therefore, development of an application in addition to the development of business logic, the more concern is how to accomplish these objects cooperate desired function, and is coupled to a low, a high polymerization. Business logic development is inevitable, but if there is a framework to help us to create objects out and manage the dependencies between these objects would be easy to develop a lot. Some people will say: "Abstract Factory, Factory Method pattern can not also help us create objects?" Generator mode "to help us deal with the dependencies between objects, you can not perform these functions do?" But we need to create another of these Some factory class, builder class we have to manage these extra classes, increase our burden, if we can create an object by a kind of configuration, management of dependencies between objects, we do not need to be created by the plant and generator and manage dependencies between objects, so we can not reduce a lot of work, speed up development, can save a lot of time to do other things, Spring framework is with this mission in front of us.

  Spring Framework in addition to helping us manage objects and their dependencies, but also provides many useful features, like the ability to cut for generic logging, performance statistics, security control, exception handling, but also help us manage the most troublesome of database transactions, itself provides a simple JDBC access implementation, and provide access to third-party data integration framework (such as Hibernate \ JPA), with a variety of Java EE technology integration (Java Mail, task scheduling Quartz, etc.), provides its own set of Web layer framework Spring MVC, but also very simple integration with third-party Web framework, from here we can say that Spring is a super adhesive platform, commonly known as adhesive, in addition to providing its own function, but also provides bonding and other techniques competency framework, so that we can more freedom of choice in the end what technology to use for development. And whether it is Java SE (C / S architecture) application or Java EE (B / S architecture) applications can use this platform for development. Today is no longer a Spring framework, it has already become a kind of ecology. SpringBoot development of portable achieved convention over configuration, micro-ecological SpringCloud services, provides a very convenient solution (gateway configuration center, registration center, service coordination and governance, etc.). Next, let's explore in depth Spring in the end bring us what?

It all starts Bean

  This concept comes Bean, have to start from the origin of Java, as early as 1996, Java is just a new, fledgling programming language, because of their own cross-platform, cross-system characteristics, with several threat to Microsoft's position. Java for interested students can view the SUN companies and Netscape Netscape and Microsoft MicroSoft war history. https://www.jianshu.com/p/b35601c9d7cd and recommended Li's "Silicon Valley business war" a book. At that time people that the interest java, just because you can use the Applet java to develop Web applications, as the browser component, but developers soon discovered that this new language can do more things. With all the different languages before, Java allows modular building complex system possible (at the time of the software industry, although sudden Mengjin in the business, but it was the traditional development efficiency of process-oriented development with the development of ideas OP, the software has not hesitated before. with the deepening complexity of the business, development has become more difficult. in fact, he was also the object-oriented development period of rapid development of OOP thinking, he was raised in the late 1980s, maturity and 1990s, is now most programming languages are already object-oriented.)

  In December, Sun has released at that time also ordered without a pass but then everyone knows the JavaBean 1.00-A specification. Early in the JavaBean specification for Java, she defines a software component model. This specification defines a set of coding strategy, not only the simple Java objects can be reused, but also can easily build more complex applications. Although JavaBean originally designed to reuse application components, then when they are mainly used for building form controls, after all, that is the mainstream in the PC era. When compared to the time is flourishing of Delphi, VB and C ++, she still looks too easy, and that they can not fill any "real" work needs.

  Support complex applications often require transactions, security, distributed services such as, but not directly JavaBean provided. So to March 1998, Sun released the EJB1.0 specification, which is designed to extend the concept of Java components to the server, and provides a much needed enterprise-class service, but he is no longer as simple as early JavaBean a. In fact, in addition to name EJB Bean, other JavaBean relations and insignificant.

  Although there are many systems it is based on reality EJB built, but EJB never achieved its original vision: to simplify development. EJB declarative programming model really simplifies a lot of the infrastructure level of development, such as transactions and security; on the other hand is complicated by EJB in the deployment descriptor and code supporting realization. Over time, many developers have already disenchanted with EJB, he began to seek a more concise way.

  The current development of the concept of Java components back on track again. The new programming techniques AOP and DI are emerging, they offer powerful features before EJB can have as JavaBean. These techniques provide a statement for the POJO programming model similar to EJB, without introducing the complexity of any EJB. When a simple JavaBean competent enough, people will not want to write a bulky EJB components.

  Objectively speaking, EJB development contributes to the POJO-based programming model. The introduction of new ideas, norms have seen an unprecedented simplified compared to the previous latest EJB specification, but for many developers, it all came too late. To the EJB3 specification was released, the other POJO based development architecture has become the de facto standard, while the Spring framework that is present in such a large environment.

Spring is designed to

  Spring is to address the complexity of enterprise application development and design, she can do a lot of things. But ultimately supported the basic concept Spring is just a little, and all of these ideas can be traced back to a fundamental mission: to simplify development. This is a solemn promise, in fact, many have claimed in the framework of certain ways of doing 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 section and templates to reduce boilerplate code.

Mainly through: Bean (BOP) for dependency injection (DI) and aspect-oriented (AOP) of these three ways to achieve.

BOP programming begins

  Spring is the Bean-oriented programming (Bean Oriented Programming, BOP), Bean is the real protagonist in Spring. Role as Bean in Spring Object significance of OOP, like, Spring Bean did not exist there would be no sense of Spring. 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. Spring is in the earliest includes dependency injection method and rely on the 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 an object, but described the way they were created. Not directly connected with the object and the service code, but which is described in the configuration file which requires a service component. Container (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.

Dependency Injection

  Spring Design's core org.spring.framework.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 be used as the base mediation most other functions. The next highest level of abstraction is the BeanFactory interface, it is the realization of the factory design pattern. Allow to create and retrieve objects by name. BeanFactory relationships between objects can also manage. BeanFactory relationships between objects can also manage.

BeanFactory supports two lowest level object model.

Example 1. Single: provides a global shared instance of an object with a particular name, it can be retrieved in the query. Singlenton is the default and most common object model.

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.

AOP programming

  Aspect Oriented Programming, namely AOP, is a programming idea, it allows the programmer to conduct cross-cutting concerns or cross the boundary of the typical duties (such as logging and transaction management) modular. The core structure is the aspect of the AOP (section), which will influence the behavior of those packages to a plurality of classes of 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 object-oriented development mode, 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. Therefore, application code written using Spring AOP is 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.

 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. The following is a block configuration diagram of Spring5:

Spring framework composed of a set of modules or each module may be present alone, may be combined to achieve one or more modules. Composition and function of each module are as follows:

The core container

A spring-beans, spring-core, spring-context and spring-expression (Spring Expression Language, 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, IOC), and dependency injection (Dependency Injection, DI). BeanFactory interface is the interface to the core of the Spring Framework, 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 when the Bean BeanFactory container will instantiate dependency is assembled with the Bean is used. spring-context module architecture on top of the core module, he extends BeanFactory, adding the Bean life cycle control for her, an event framework and system resources to load transparency and other functions. In addition, the module also provides a number of enterprise-level support, such as e-mail access, remote access, task scheduling, ApplicationContext is the core interface of the module, she superclass is BeanFactory. BeanFactory different and, after the container is instantiated ApplicationContext automatically instantiated with all dependencies for the assembly of a single instance Bean, 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, it can query, object management operation, but also can easily call the object methods manipulate arrays, collections and so on. Its syntax is similar to the conventional EL, but provides additional features, the best template function 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.

AOP and device support

A spring-aop, spring-aspects and spring-instrument 3 modules.

Spring is spring-aop another core module, AOP is the main implementation module. As Following OOP, affecting one of the biggest programming ideas for programmers, AOP has greatly opened up people's ideas for programming. In Spring, he is the JVM dynamic proxy technology, and then design a series of crosscutting AOP implementations, such as pre-notification, return, throws advice etc., at the same time, Pointcut the interface to match the starting point, you can use existing pointcut designed cross-section, may be extended according to the needs associated cutting method. spring-aspects module integrates self AspectJ frame, mainly to provide a variety of implementations of AOP Spring AOP. When the spring-instrument module is based on the JAVA SE "java.lang.instrument" design, the AOP should be considered a support module, the main role is in the JVM is enabled, generates a proxy class, the programmer run through the proxy class modify byte type, thereby changing the functionality of a class, to achieve AOP function. In the classification, I put him in a minute under AOP module Spring official documentation in this place a little vague, here are purely personal point of view.

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 still has its advantages. In fact, business is business logic-based; a method in the business layer should correspond to a complete business; if the business fails, the entire transaction is rolled back; therefore, it is absolutely transaction control should be placed in the service layer; however, persistence layer design should follow a very important principle: atomicity operations to ensure that the persistence layer in each method should not be divided. Therefore, when using the Spring JDBC transaction control, it should pay attention to its particularity. Foam cushions produced, will be fine www.gupaoedu.com 9 spring-orm module ORM framework support module, the main integrated Hibernate, Java Persistence API (JPA) and Java Data Objects (JDO) for resource management, data access object (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, which is an O / M-mapper, mapping java object into XML data, or to map the XML data into a java object ), for example: JAXB, Castor, XMLBeans, JiBX and XStream and so on. spring-jms module (Java Messaging Service) to send and receive information from Spring Framework 4.1 later, he also provided support for the spring-messaging module.

Web Components

The spring-web, spring-webmvc, spring-websocket and spring-webflux 4 modules.
spring-web module Spring provides the most basic Web support, mainly built on top of the core container, or by Listeners Servlet container to initialize the IOC, also includes some Web-related support. spring-webmvc module is a well-known of Web-Servlet module, the Spring MVC (model-view-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.

Communication 报文

That spring-messaging module, from the beginning Spring4 added a new module, the main responsibility is to integrate some basic messaging applications for the Spring Framework.

Integration Testing

Be able to perform some integration testing or other testing is very important for any business case that is spring-test module, providing support primarily for testing, after all, you do not need to release (program) to your application server or connect to other business facilities of

Integration compatible

I.e., spring-framework-bom module, Bill of Materials. Spring address different modules of different versions dependency problems.

Dependencies between modules

Spring official website of the relationship between the various modules Spring5 a detailed description:

 

 I am also Spring5 modules summarized a system of dependencies between modules description, hoping to help small partners.

 

 

接下来的博文中,我们将深入了解 Spring 的核心模块功能。基本学习顺序为:从 spring-core 入手, 其次是 spring-beans 和 spring-aop,随后是 spring-context,再其次是 spring-tx 和 spring-orm, 最后是 spring-web 和其他部分。

Spring 版本命名规则

描述方式 说明 含义
Snapshot 快照版 尚不不稳定、尚处于开发中的版本
Release 稳定版 功能相对稳定,可以对外发行,但有时间限制
GA 正式版 代表广泛可用的稳定版(General Availability)
M 里程碑版 (M 是 Milestone 的意思)具有一些全新的功能或是具有里程碑意义 的版本。
RC 终测版 Release Candidate(最终测试),即将作为正式版发布。

 

Spring 源码下载及构建技巧

Spring5 源码下载注意事项

首先你的 JDK 需要升级到 1.8 以上。Spring3.0 开始,Spring 源码采用 github 托管,不再提供官网下载 链接。这里不做过多赘述,大家可自行去 github 网站下载,我们使用的版本下载链接为: https://github.com/spring-projects/spring-framework/archive/v5.0.2.RELEASE.zip,下载完成后, 解压源码包会看到以下文件目录:

 

基于 Gradle 的源码构建技巧

由于 Spirng5 以后都是采用 Gradle 来编译,所以构建源码前需要先安装 Gradle 环境。Gradle 下载地 址:https://gradle.org/releases,我使用的是 Spring5 官方推荐的版本 Gradle4.0,下载链接为: https://gradle.org/next-steps/?version=4.0&format=bin , 下 载 完 成 后 按 以 下 步 骤 操 作 , 以 Windows 操作系统为例:

第一步:配置环境变量

第二步:添加环境变量:Path:%GRADLE_HOME%\bin

第三步:检测环境,输入 gradle -v 命令,得到以下结果:

 

第四步:编译源码,cmd 切到 spring-framework-5.0.2.RELEASE 目录,运行 gradlew.bat

第五步:转换为 eclipse 项目,执行 import-into-eclipse.bat 命令,构建前,请确保网络状态良好,按 任意键继续。

第六步:等待构建成功(若中途出现错误,大部分情况是由于网络中断造成的,重试之后一般都能解决 问题),构建成功后,会出现如下界面:

到这一步为止,还在使用 Eclipse 的小伙伴已经可以将项目导入到 Eclipse 中了。而我们推荐使用的 IDEA 也比较智能,可以直接兼容 Eclipse 项目。接下来看下面的步骤:

第七步:导入 IDEA。打开 IntelliJ IDEA,点击 Import Project,弹出如下界面,选择 spring-framework-5.0.2.RELEASE 文件夹:

第八步:等待构建完成,在网络良好的情况下大约需要 10 分钟便可自动构建完成,你会看到如下界面:

第九步:在 IDEA 中,如果 Project 下的子项目文件夹变成粗体字之后,说明已经构建成功。还有一种 验证方式是:找到 ApplicationContext 类,按 Ctrl + Shift + Alt + U,出现类图界面说明构建成功。

Gradle 构建过程中的坑

如果项目环境一直无法构建,项目文件夹没有变粗体字,类图无法自动生成。那么你一定是踩到了这样一个坑。

第一步:首先打开 View->Tool Windows -> Gradle

然后,点击右侧 Gradle 视图中的 Refresh,会出现如下的错误:

 

第二步:看错误,显然跟 Gradle 没有任何关系,解决办法:

1.关闭 IDEA,打开任务管理器,结束跟 java 有关的所有进程。

2.找到 JAVA_HOME -> jre -> lib 目录,将 tools.jar 重命名 tools.jar.bak。

3.重启 IDEA,再次点击 refresh,等待构建完成。

 

Guess you like

Origin www.cnblogs.com/create-my-future/p/11233940.html