• IDEA阅读spring源码并调试


    目标:搭建起Spring源码阅读和代码调试跟踪的环境,顺便建立一个简单的Demo,能够调试Spring的源代码

    本节,主要介绍一下Spring源码阅读和调试的相关环境搭建,并使用MVN创建一个非常简单的Demo,以便可以跟踪和调试Spring的内核。

    1、源码的下载

    Spring的源码可以从GitHub上下载:https://github.com/spring-projects/spring-framework

    2、Spring源码的编译

    Spring源码下载下来后,我这里比较习惯,先编译出来,而不是直接导入到相关的 IDE。大型的开源项目都是借助于自动化构建工具实现编译打包的,因此先编译有个好处,就是编译过程中,会自动的下载相关的依赖。现在大部分项目编译过程中出现的问题,首先都归结到项目依赖的问题。

    ①、编译环境

    A、操作系统:我的源码和相关开发环境,都是在 mac 下的

    B、JDK:安装好JDK1.8

    C、由于Spring源码是采用Gradle这种自动化构建工具构建的,因此需要安装 Gradle ,安装过程就不多说了。

    D、我使用的IDE是 IntelliJ IDEA,因此,需要安装 IntelliJ IDEA 旗舰版,免费30天

    ②、源码编译

    在源码编译环境准备好后,编译源码之前,请先仔细看一下 Spring源码目录下的 README.md、import-into-idea.md  这两个文件。 README.md中,告知了整个编译过程和需要注意的地方。如果你使用Eclipse作为IDE的开发工具,那直接运行./import-into-eclipse.sh  或者 ./import-into-eclipse.bat ,就可以把源码导入到Eclipse中,如果使用IDEA作为开发工具,那么需要认真阅读 import-into-idea.md,它告诉了你导入IDEA的步骤。

    A、先进入 …/spring-framework 目录,执行 ./gradlew :spring-oxm:compileTestJava  先对 Spring-oxm 模块进行预编译。

    B、还是在 …/spring-framework 目录 ,执行 ./gradlew build -x test  编译,整个Spring的源码。 后面的 -x test  是编译期间忽略测试用例,需要加上这个,Spring的测试用例,有些是编译不过的。编译过程时间,会随着网络的畅通程度而不同。

    ③、源码导入IDEA

    编译通过后,就可以把源码导入到 IDEA中了

    在IDEA中 File -> New -> Project from Existing Sources -> Navigate to directory ,选择Spring源码目录,导入,然后IDEA会自动的使用Gradle进行构建。构建完成之后,需要做如下设置:

    排除 spring-aspects  项目,这个是Spring 的AOP体系集成了 aspects ,但在IDEA中无法编译通过,原因可以参见:

    http://youtrack.jetbrains.com/issue/IDEA-64446

    选中  spring-aspects  项目 右键,选择“Load/Unload Moudules” 在弹出的窗体中进行设置(如下图所示):

    3、简单的Demo的搭建

    这里可以创建一个简单的Demo,该Demo依赖于Spring的源码,这样,就可以从外部,运行Demo,跟踪到Spring的内部源码了。为不失一般性,这里的Demo采用MVN进行构建,只不过,相关的Spring的源码依赖需要在IDEA中设置成本地源码

    ①、使用IDEA 在Spring的源码的Project目录下,创建一个Demo,可以直接使用MVN的骨架

    ②、Demo的相关设置和简单的代码测试

    A、设置一下pom.xml 中的 junit 依赖版本,修改为 4.12 否则Spring的单元测试,编译不通过

    B、在IDEA设置Spring的项目依赖(设置Spring-core、Spring-beans、Spring-context、Spring-expression这几个module就可以了):

    C、pom.xml中需要引入commons-logging的依赖,否则编译报找不到LogginFactory的错误.配置静态资源路径(否则读取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.hxl</groupId>
    <artifactId>spring-debug</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>spring-debug</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>

    <!-- 引入commons-logging依赖 -->
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
    </dependency>
    </dependencies>

    <build>
    <!-- 静态资源路径配置 -->
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    </resource>
    </resources>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
    <plugins>
    <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>3.0.0</version>
    </plugin>
    <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
    <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    </plugin>
    <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    </plugin>
    <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    </plugin>
    <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    </plugin>
    <plugin>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    </plugin>
    <plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    </plugin>
    </plugins>
    </pluginManagement>
    </build>
    </project>
     

    D、设置Spring的配置和编写简单的Spring代码

    创建一个简单的 登录接口 ILogin:

    public interface ILogin {
    String loginCheck(String userName,String password);
    }
     

    它有个实现类:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    @Component
    public class LoginImpl implements ILogin {

    String id = "";

    @Autowired(required = false)
    public void setId(String id) {
    this.id = id;
    }

    @Override
    public String loginCheck(String userName, String password) {
    System.out.println("boy登录...");
    return "success";
    }
    }
     

    然后,把该bean 注册到配置中(路径spring-debug/src/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="loginService" class="com.boy.login.LoginImpl"/>
    </beans>
    最后,编写调用的代码:

    import com.boy.login.ILogin;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    /**
    * Hello world!
    */
    public class App {
    public static void main(String[] args) {

    // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-config.xml");
    String XMLPath = "//Users/sky/Java/spring-framework/spring-debug/src/spring-config.xml";
    ApplicationContext applicationContext = new FileSystemXmlApplicationContext(XMLPath);
    ILogin login = (ILogin) applicationContext.getBean("loginService");
    login.loginCheck("boy", "123");
    }
    }
     

    然后就可以 进行Debug了,并且可以Debug到Spring源码内部。

    原文:https://blog.csdn.net/u013310075/article/details/80707098

  • 相关阅读:
    B
    I
    C
    判断线段之间的关系(D
    C
    求矩形的周长(线段树+扫描线) Picture POJ
    面积并+扫描线 覆盖的面积 HDU
    线段树->面积并 Atlantis HDU
    E1. Array and Segments (Easy version)(暴力) && E2. Array and Segments (Hard version)(线段树维护)
    Python File writelines() 方法
  • 原文地址:https://www.cnblogs.com/shijiaoyun/p/10244080.html
Copyright © 2020-2023  润新知