Spring Framework (a) reading the source environment building entry Spring maven spring framework dependencies between modules and internal architecture jar

I. Introduction

Spring Framework to simplify enterprise application development for us. Spring can use the simple JavaBean EJB implement the functions previously only be achieved in.
Spring Framework is an IOC (DI) container and AOP framework

DETAILED Spring Framework Description:

  • Lightweight: Spring is non-invasive - based application development Spring objects may not be in dependence on the Spring API
  • Dependency injection (DI --- dependency injection, IOC)
  • Aspect Oriented Programming (AOP --- aspect oriented programming)
  • Container: Spring is a container because it contains life cycle management and application objects
  • Framework: Spring implements a complex application to use a combination of simple component configuration can use XML and Java annotations combination of these objects in the Spring.
  • One-stop: Based on the IOC and AOP can integrate various open-source framework for enterprise applications and excellent third-party libraries (in fact SpringMVC Spring itself provides the presentation layer and the Spring JDBC persistence layer)

 

Java pain points after the first 10 years of glorious

Java from the date of birth in 1994, it was by means of  a simple syntax, object-oriented, cross-platform, robust security features such as rapid welcomed by the industry, and soon rose to the position of No1 usage. Various applications based on Java implementation of the various services, third-party frameworks and toolkits began exploding. Although, in this regard on behalf of the Java certainly been in the industry (at that time already a first development language) and hot, but on the other hand has also increased the complexity and cost of programmer time to develop, and the more body mass code to the greater, which brought a lot of problems. The most prominent are a few below:

In a layered architecture, the upper and lower layers to achieve the caller is formed by the association between the code level, which will increase the degree of coupling design; most third party frameworks specified parent class inherits implement a given interface or by forcing the user, resulting in the application binding rigid frame, the formation of "invasive" programming; scattered in the various modules in the application of non-functional repeating modules, has not been fully and flexible multiplexing; these problems lead to doing Java development time (especially Web developers) become increasingly bloated cumbersome, it will be "lightweight" to be consistent with the voice of the industry. To solve this problem, inline proposed a variety of solutions, including the SUN the company has been doing on a JavaEE platform, a wide range of improvements, optimization, new push, but ultimately chose the history of "Spring".

Spring appears

Part of the beginning of the Spring framework is written in providing independent financial advisory business in London Rod Johnson in 2000. In the "Expert One-On-One J2EE Development Without EJB" a book, he further expanded his code to illustrate "how to make applications to go beyond ease of use and stability was accustomed the public to accept and J2EE views on the different components of cooperation platform ".

A number of voluntary expansion of the Spring framework program development staff team, in February 2003 to build a project on Sourceforge. After working for a year on the Spring Framework, the team in March 2004 released the first version (1.0). After this version, Spring Framework Java community becomes abnormal popular, of course, thanks to this it should be part of good documentation capabilities and detailed references, in particular, is especially true for an open source project concerned. Spring is an important design goal is to frame more easily with existing J2EE (now called JavaEE or JEE) standard tools and commercial integration. Simply put, Spring new framework (at least initially Uncle Rod says so), not to build another large and the initial target. But to the Spring framework is like a large container, like, can quickly and easily integrate a variety of applications ( keep in mind: not only Web application, even if a normal Java application can also be) in a variety of prior art, to support them, It allows developers to feel in the implementation process as simple as JavaBean, but also easy to complete replacement of similar frameworks and tools when necessary. The Spring reach this goal means that two key: Inversion of Control / Dependency Injection (IOC / DI), and Aspect Oriented Programming (AOP). Wherein, IOC / DI is the basic underlying Spring; Spring AOP is strong and highlights. They are also the starting point we started to learn the Spring.

Spring development

In the beginning of the birth, Spring is to simplify the growing alternative heavyweight enterprise Java technology. But as time goes on, JavaEE itself is also in constant evolution, for the simple JavaBean model, dependency injection, aspect-oriented also gradually integrated into, no doubt inspired by the success of the Spring.

While it is imitated, Spring and did not stop. It continued development in other areas: mobile development, social integration API, it is involved in all areas of security and innovation management, NoSQL databases, cloud computing and big data, etc., its prospects even brighter, and even has been formed with the traditional JavaEE potential rival platform.

我们可以看到Spring已经打造出了一个自己专属的Spring生态帝国(Spring全家桶)。在这里,我们可以找到几乎所有Web开发所需要的一切解决方案,所以Spring是每个Java程序员都必须要掌握学习的重点内容!

二、Spring技术体现结构

Spring 技术以Spring Framework为基础,在此基础上构建了一个庞大的生态圈Spring Portfolio。整个Spring Portfolio包括多个构建于核心Spring框架之上的框架和类库,几乎为每一个Java领域的开发都提供了Spring编程模型。

2.1 Spring 框架

体系结构

Spring 有可能成为所有企业应用程序的一站式服务点,然而,Spring 是模块化的,允许你挑选和选择适用于你的模块,不必要把剩余部分也引入。下面的部分对在 Spring 框架中所有可用的模块给出了详细的介绍。

Spring 框架提供约 20 个模块,可以根据应用程序的要求来使用。这些模块可划分为六大功能。

Spring 体系结构

核心容器

核心容器由spring-core,spring-beans,spring-context,spring-context-support和spring-expression(SpEL,Spring表达式语言,Spring Expression Language)等模块组成,它们的细节如下:

  • spring-core模块提供了框架的基本组成部分,包括 IoC 和依赖注入功能。

  • spring-beans 模块提供 BeanFactory,工厂模式的微妙实现,它移除了编码式单例的需要,并且可以把配置和依赖从实际编码逻辑中解耦。

  • context模块建立在由core和 beans 模块的基础上建立起来的,它以一种类似于JNDI注册的方式访问对象。Context模块继承自Bean模块,并且添加了国际化(比如,使用资源束)、事件传播、资源加载和透明地创建上下文(比如,通过Servelet容器)等功能。Context模块也支持Java EE的功能,比如EJB、JMX和远程调用等。ApplicationContext接口是Context模块的焦点。spring-context-support提供了对第三方库集成到Spring上下文的支持,比如缓存(EhCache, Guava, JCache)、邮件(JavaMail)、调度(CommonJ, Quartz)、模板引擎(FreeMarker, JasperReports, Velocity)等。

  • spring-expression模块提供了强大的表达式语言,用于在运行时查询和操作对象图。它是JSP2.1规范中定义的统一表达式语言的扩展,支持set和get属性值、属性赋值、方法调用、访问数组集合及索引的内容、逻辑算术运算、命名变量、通过名字从Spring IoC容器检索对象,还支持列表的投影、选择以及聚合等。

它们的完整依赖关系如下图所示:

Spring 体系结构

数据访问/集成

数据访问/集成层包括 JDBC,ORM,OXM,JMS 和事务处理模块,它们的细节如下:

(注:JDBC=Java Data Base Connectivity,ORM=Object Relational Mapping,OXM=Object XML Mapping,JMS=Java Message Service)

  • JDBC 模块提供了JDBC抽象层,它消除了冗长的JDBC编码和对数据库供应商特定错误代码的解析。

  • ORM 模块提供了对流行的对象关系映射API的集成,包括JPA、JDO和Hibernate等。通过此模块可以让这些ORM框架和spring的其它功能整合,比如前面提及的事务管理。

  • OXM 模块提供了对OXM实现的支持,比如JAXB、Castor、XML Beans、JiBX、XStream等。

  • JMS 模块包含生产(produce)和消费(consume)消息的功能。从Spring 4.1开始,集成了spring-messaging模块。。

  • 事务模块为实现特殊接口类及所有的 POJO 支持编程式和声明式事务管理。(注:编程式事务需要自己写beginTransaction()、commit()、rollback()等事务管理方法,声明式事务是通过注解或配置由spring自动处理,编程式事务粒度更细)

Web

Web 层由 Web,Web-MVC,Web-Socket 和 Web-Portlet 组成,它们的细节如下:

  • Web 模块提供面向web的基本功能和面向web的应用上下文,比如多部分(multipart)文件上传功能、使用Servlet监听器初始化IoC容器等。它还包括HTTP客户端以及Spring远程调用中与web相关的部分。。

  • Web-MVC 模块为web应用提供了模型视图控制(MVC)和REST Web服务的实现。Spring的MVC框架可以使领域模型代码和web表单完全地分离,且可以与Spring框架的其它所有功能进行集成。

  • Web-Socket 模块为 WebSocket-based 提供了支持,而且在 web 应用程序中提供了客户端和服务器端之间通信的两种方式。

  • Web-Portlet 模块提供了用于Portlet环境的MVC实现,并反映了spring-webmvc模块的功能。

其他

还有其他一些重要的模块,像 AOP,Aspects,Instrumentation,Web 和测试模块,它们的细节如下:

  • AOP 模块提供了面向方面的编程实现,允许你定义方法拦截器和切入点对代码进行干净地解耦,从而使实现功能的代码彻底的解耦出来。使用源码级的元数据,可以用类似于.Net属性的方式合并行为信息到代码中。

  • Aspects 模块提供了与 AspectJ 的集成,这是一个功能强大且成熟的面向切面编程(AOP)框架。

  • Instrumentation 模块在一定的应用服务器中提供了类 instrumentation 的支持和类加载器的实现。

  • Messaging 模块为 STOMP 提供了支持作为在应用程序中 WebSocket 子协议的使用。它也支持一个注解编程模型,它是为了选路和处理来自 WebSocket 客户端的 STOMP 信息。

  • 测试模块支持对具有 JUnit 或 TestNG 框架的 Spring 组件的测试。

 

2.2 Spring Portfolio

Spring portfolio 包括多个构建于核心 Spring 框架之上的框架和类库。也就是说,整个 Spring portfolio 几乎为每一个领域的 Java 开发都提供了 Spring 编程模型。

Spring Web Flow

建立于 Spring MVC 框架之上,它为基于流程的会话式 Web 应用提供支持。
相关网址:http://projects.spring.io/spring-webflow/

Spring Web Service

提供了契约优先的 Web Service 模型,服务的实现都是为了满足服务的契约而编写的。
相关网址:http://docs.spring.io/spring-ws/site/

Spring Security

为 Spring 应用提供了声明式的安全机制。
相关网址:http://projects.spring.io/spring-security/

Spring Integration

提供了多种通用应用集成模式的 Spring 声明式风格实现。
相关网址:http://projects.spring.io/spring-integration/

Spring Batch

如果需要开发一个批处理应用,可以通过 Spring Batch,使用 Spring 强大的面向 POJO 的编程模型。
相关网址:http://projects.spring.io/ spring-batch/

Spring Data

不管你使用文档数据库,如 MongoDB;图数据库,如 Neo4j;还是传统的关系型数据库,Spring Data 都为持久化提供了一种简单的编程模型。这包括为多种数据库类型提供了一种自动化的 Repository 机制,它负责为你创建 Repository 的实现。

Spring Social

Spring 的一个社交网络扩展模块。
它更多的是关注连接,而不是社交。它能够帮助你通过 REST API 连接 Spring 应用,其中有些 Spring 应用可能原本并没有任何社交方面的功能目标。

入门指南:https://spring.io/guides/gs/accessing-facebook/ 和 https://spring.io/guides/gs/accessing-twitter/

Spring Mobile

是 Spring MVC 新的扩展模块,用于支持移动 Web 应用开发。

Spring for Android

与 Spring Mobile 相关的是 Spring Android 项目。这个项目,旨在通过 Spring 框架为开发基于 Android 设备的本地应用提供某些简单的支持。
相关网址:http://projects.spring.io/spring-android/

Spring Boot

Spring Boot 大量依赖于自动配置技术,它能消除大部分 Spring 配置。它还提供了多个 Starter 项目,不管你使用 Maven 还是 Gradle,这都能减少 Spring 工程构建文件的大小。

2.3 Spring Boot和Spring Cloud

通过名字就能有所了解,Spring Boot,看名字就知道是Spring的引导,就是用于启动Spring的,使得Spring的学习和使用变得快速无痛。

Spring Cloud,就是云相关的,把我们的应用在云端进行部属,专注于微服务。

Spring 框架的使用还是毕竟麻烦的,于是为了简化Spring框架的使用,提高效率,就有了Spring Boot,Spring Boot是对Spring框架的封装。在Spring Boot 基础上又开发了一个专注于微服务的框架Spring Cloud。

Spring Cloud基于Spring Boot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件,除了基于NetFlix的开源组件做高度抽象封装之外,还有一些选型中立的开源组件。

而SpringBoot并没有重复制造轮子,它将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过SpringBoot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

Spring Cloud提供了全家桶式技术解决方案,对我们使用者来说是极其简单的。但是要学会Spring Cloud的前提那必定要学会SpringBoot。

Spring Cloud技术体系

  • 我的菜单,以及我的配方,就叫做配置中心
  • 所有的菜都要摆在桌子上。我的桌子,就是注册中心。
  • 有的菜非常好吃,我就多做了一盘,这就是负载均衡
  • 吃的菜有的用筷子,有的像印度佬一样直接下手抓,筷子和手,就叫做网关
  • 黄瓜要蘸酱才好吃,但它们是两道菜,这就叫分布式事务

三、环境配置

源码阅读环境

Spring源码阅读环境搭建

Eclipse

手把手教你安装Spring+搭建Spring开发环境

Maven

spring framework体系结构及内部各模块jar之间的maven依赖关系

 

四、入门示例

使用IDEA

1.新建Maven项目

2.pom.xml 添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aidata</groupId>
    <artifactId>springlearning</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

3.新建一个包,包下新建类

public class HelloWorld {
    
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void hello() {
        System.out.print("hello:" + name);
    }

}

4.新建Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloworld" class="com.aidata.spring.HelloWorld">
        <property name="name" value="spring"></property>
    </bean>
</beans>

5.新建一个运行Spring的类Main

class Main{
    
    public static void main(String[] args) {
        System.out.println("hello");
        //1.创建IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从IOC容器中获取Bean实例
        HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloworld");
        //3.调用hello方法
        helloWorld.hello();    
    }
}

6.运行,控制台显示结果

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/aidata/p/12290688.html