spring -Spring source depth analysis of the overall architecture and built environment

Outline

Spring is an open source framework for the design level, he addressed a business logic layer and the other layers of loosely coupled problems, so it will be an interface-oriented programming ideas throughout the system application. Spring is a lightweight rise in 2003 Java development framework, created by Rod Johnson. Briefly, Spring is a layered JavaSE / EE full-stack (stop) lightweight open source framework.

spring overall architecture

Spring Framework is a layered architecture, which contains a series of functional elements, and is divided into about 20 modules, as shown below: 

You can see from the chart view of the overall framework spring framework, these modules are summarized in the following sections:

1. Core Container

Container Core (core container) with Core, Beans, the Context and Expression Language modules
Core module and Beans is a fundamental part of the frame, providing the IoC (rotation speed control) and dependency injection characteristics. The basic concept here is the BeanFactory, which provides for the Classical Factory pattern to eliminate the need for programmatic singletons and allows you really separated dependencies and program logic from the configuration.

Basic core tools Core module mainly contains the Spring Framework
Beans module is to be used in all applications, which includes access to the configuration files, create and manage bean as well as Inversion of Control / Dependency Injection (Ioc / DI) operations related to all classes
Context modules are built on the basis of Core and Beans modules, object access method provides a similar frame of formula JNDI registrar. Context module inherits the properties of Beans, Spring provides a number of extensions to the core, adds support for internationalization (such as resource bundles), event propagation, resource loading and support for Context of transparency created. ApplicationContext interface module is a key Context
Context Core module is built on the basis module and Beans, provides a method for accessing an object similar to a Frame JNDI registrar. Context module inherits the properties of Beans, Spring provides a number of extensions to the core, adds support for internationalization (such as resource bundles), event propagation, resource loading and support for Context of transparency created. Context is key ApplicationContext interface module
provides a powerful expression language for querying and manipulating objects at runtime Expression Language module, supported by the language set / get the value of property, call distribution properties, methods, and access an array of context, container and indexers, logical and arithmetic operators, and the name of the variable name from the search target according Spring IoC container

2. Data Access/Integration

JDBC module provides a JDBC abstraction layer, which may eliminate the error code tedious JDBC coding and parsing database vendor specific, this encompasses all classes Spring a JDBC data access encapsulation
ORM module popular object - relational mapping the API, as JPA, JDO, Hibernate, iBatis etc., to provide an interactive layer, using ORM package can be mixed with all the features provided by the Spring O / R mapping, such as a simple declarative transaction management feature mentioned previously
OXM module provides a Object / XML mapping achieve abstraction layer, Object / XML mapping achieve abstraction layer includes JAXB, Castor, XMLBeans, JiBX and XStream
JMS (the Java the message Service) module mainly contains a number of manufacturing and consumption of news features
Transaction module supports programming and declarative management of things, these matters must implement a specific interface, and are applicable to all POJO

3. Web

Web context module builds on the application context module, providing a context for the Web-based application, so the Spring framework supports integration with Jakarta Struts is. Web processing module also simplifies the job of the multi-part request and the request object bound to the domain parameter. Web layer includes the Web, Web-Servlet, Web- Struts and Web, Porlet module
Web module: provides a Web-oriented integration features based, e.g., file upload, using the Servlet
the Listeners initialization IoC container and a Web-oriented application context it also includes a remote support Spring Web relevant portions
Web-Servlet module web.servlet.jar: Spring this module contains the model-view-controller (MVC) implemented, Spring MVC framework so that the code within the model domain and web It can be clearly divided between the forms to leave, and other properties of the base frame together Spring
Web-Struts module: this module provides support for the Struts such type layer can be a typical Struts Web application integration in the Spring with
Web-Porlet module: provides an implementation for the Portlet environment and Web-Servlet module of MVC

4. AOP

AOP module provides one realization AOP Alliance standard Oriented Programming, which allows you to define a method, for example, interceptors and tangent point, thereby separating the logic code, to reduce the coupling between them, using a source-level metadata function, but also can be a variety of behavioral information into your code
Spring AOP module provides transaction management services for objects Spring-based applications by using Spring AOP, without relying on EJB components, you can declarative transaction management integrated into the application

5. Test

Test modules support the use Junit and TestNG test components for Spring

Environment to build

gradle installation

Gradle is a JVM-based build tool, is a versatile and flexible building tools, support maven, Ivy warehouse, supports transitive dependencies management, without the need for a remote repository or pom.xml and ivy.xml profile, based on Groovy, build scripts written in Groovy, you need to gradle official website to download the corresponding version, http://services.gradle.org/distributions
need to modify the system under windows environment variables, adding GRADLE_HOME system variables, point gradle root directory. After successful configuration environment, enter the command line: gradle -v displays the following screenshot: 

 

Source link for

1. Go to the official website https://projects.spring.io/spring-framework/ , click on the image of the kitten into github download the appropriate version of spring source.

 
2. Switch to the latest Spring 5.1. X version of the source code, get Git branch link
 

Source download and import IDEA 

1. IDEA Spring Git pulling branch
2. Local installation directory
3. Pull wait

 

You can also manually download the source to the local

4. Perform gradlew.bat

Simply double-click to perform gradlew.bat. After execution is completed, there will be prompted to build success, but this time is not finished, in order to be able to import the idea to import, the need to use gradlew command to compile the spring-oxm package. 

That is, execute the following command: gradlew.bat cleanIdea: spring-oxm: compileTestJava
When finished, see the following results, it is far away from success.

5.IDEA Import sources

 

gradle here uses version 4.3.1 before we installed yourself, do not use IDE comes;

将jdk版本升级到1.8,因为在最新的Spring中使用到了jdk1.8的新特性,如果不升级到1.8会报错;

最后idea会自动编译spring源码,编译成功以后,所有的模块都会有个小小的蓝色图标,表示编译成功,至此spring源码导入完成。 

导入项目遇见问题

1.No such property: immutableValues for class: org.gradle.api.internal.tasks.DefaultTaskDependency

修改:spring-beans.gradle

注释掉这两行代码
//def deps = compileGroovy.taskDependencies.immutableValues + compileGroovy.taskDependencies.mutableValues
//compileGroovy.dependsOn = deps - 'compileJava'

2.Could not find method useJUnitPlatform()

注释:spring-test.gradle中这行代码
//  useJUnitPlatform {
//    includeEngines 'junit-jupiter'
//    excludeTags 'failing-test-case'
// }

创建测试项目

推荐博客

  程序员写代码之外,如何再赚一份工资?

导入成功之后可以在project下创建一个自己的测试module

1.项目根目录右键 -> new -> module -> gradle

2.在新项目的build.gradle下添加对spring模块的依赖,这里我先添加了spring-beans 和spring-core的依赖。

dependencies {
    compile(project(":spring-beans"))
    compile(project(":spring-core"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

3.在新项目的src/main/resource下添加spring-config.xml文件

<?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="myTestBean" class="com.chenhao.spring.MyTestBean"/>

</beans>

4.创建测试bean和启动类

package com.chenhao.spring;

/**
 * @author: ChenHao
 * @Description:
 * @Date: Created in 10:35 2019/6/19
 * @Modified by:
 */
public class MyTestBean {
    private String name = "ChenHao";

    public MyTestBean(String name) {
        this.name = name;
    }

    public MyTestBean() {
    }

    @Override
    public String toString() {
        return "MyTestBean{" +
                "name='" + name + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import com.chenhao.spring.MyTestBean;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
 * @author: ChenHao
 * @Description:
 * @Date: Created in 10:36 2019/6/19
 * @Modified by:
 */
public class AppTest {
    @Test
    public void MyTestBeanTest() {
        BeanFactory bf = new XmlBeanFactory( new ClassPathResource("spring-config.xml"));
        MyTestBean myTestBean = (MyTestBean) bf.getBean("myTestBean");
        System.out.println(myTestBean.getName());
    }
}

 

Then you can stay in the source code debugging spring.

Reproduced in: https: //www.cnblogs.com/java-chen-hao/p/11046190.html

Guess you like

Origin blog.csdn.net/weixin_33775572/article/details/93340731