spring源码分析环境的搭建和demo

1.spring源码分析环境的搭建和demo

1.1下载源码

环境搭建:sts/eclipse + jdk1.7 + windows + gradle

https://github.com/spring-projects/spring-framework/tree/3.2.x下载下来

 

1.2运行import-into-eclipse.bat

可能会遇到的问题

1.2.1 问题1

:spring-oxm:compileTestJava FAILED

 

FAILURE: Build failed with an exception.

 

* Where:

Script 'F:\tech-workspace\spring-framework-3.2.x\spring-oxm\oxm.gradle' line: 123

 

* What went wrong:

Execution failed for task ':spring-oxm:compileTestJava'.

> JiBXException in JiBX binding compilation

 

* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

 

BUILD FAILED

 

解决方案:

修改

spring源码包中 build.gradle

repositories属性添加 阿里云的仓库 加快jar包下载速度

maven { url "http://maven.aliyun.com/nexus/content/groups/public" }

 

1.2.2 问题2

[ant:jibx] Error running binding compiler

[ant:jibx] java.lang.IllegalStateException: Error loading class java.lang.CharSequence: Error reading path java/lang/CharSequence.class for class java.lang.CharSequence

[ant:jibx]      at org.jibx.binding.classes.ClassCache$ClassCacheLocator.getClassInfo(ClassCache.java:291)

[ant:jibx]      at org.jibx.binding.model.ClassHierarchyContext.accumulateInterfaces(ClassHierarchyContext.java:95)

[ant:jibx]      at org.jibx.binding.model.ClassHierarchyContext.addTypedComponent(ClassHierarchyContext.java:121)

[ant:jibx]      at org.jibx.binding.model.DefinitionContext.addFormat(DefinitionContext.java:527)

[ant:jibx]      at org.jibx.binding.model.BindingElement.defineBaseFormat(BindingElement.java:843)

[ant:jibx]      at org.jibx.binding.model.BindingElement.runValidation(BindingElement.java:865)

[ant:jibx]      at org.jibx.binding.model.BindingElement.runValidation(BindingElement.java:899)

[ant:jibx]      at org.jibx.binding.model.BindingElement.validateBinding(BindingElement.java:969)

[ant:jibx]      at org.jibx.binding.Utility.validateBinding(Utility.java:226)

[ant:jibx]      at org.jibx.binding.Utility.loadBinding(Utility.java:269)

[ant:jibx]      at org.jibx.binding.Utility.loadFileBinding(Utility.java:420)

[ant:jibx]      at org.jibx.binding.Compile.compile(Compile.java:217)

[ant:jibx]      at org.jibx.binding.ant.CompileTask.execute(CompileTask.java:248)

[ant:jibx]      at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)

[ant:jibx]      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[ant:jibx]      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

[ant:jibx]      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

[ant:jibx]      at java.lang.reflect.Method.invoke(Method.java:498)

[ant:jibx]      at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)

[ant:jibx]      at groovy.util.AntBuilder.performTask(AntBuilder.java:319)

[ant:jibx]      at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:264)

[ant:jibx]      at org.gradle.api.internal.project.ant.BasicAntBuilder.nodeCompleted(BasicAntBuilder.java:77)

[ant:jibx]      at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:147)

[ant:jibx]      at groovy.util.AntBuilder.doInvokeMethod(AntBuilder.java:203)

[ant:jibx]      at org.gradle.api.internal.project.ant.BasicAntBuilder.doInvokeMethod(BasicAntBuilder.java:92)

[ant:jibx]      at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:64)

[ant:jibx]      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeOnDelegationObjects(ClosureMetaClass.java:428)

[ant:jibx]      at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:369)

[ant:jibx]      at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1015)

[ant:jibx]      at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)

[ant:jibx]      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)

 

解决方案:

将jdk换成1.7

 

1.2.3 问题3

* What went wrong:

Execution failed for task ':spring-context-support:eclipseClasspath'.

> Could not resolve all dependencies for configuration ':spring-context-support:compile'.

   > Could not resolve javax.mail:mail:1.4.7.

     Required by:

         org.springframework:spring-context-support:3.2.19.BUILD-SNAPSHOT

      > Could not resolve javax.mail:mail:1.4.7.

         > Could not get resource 'https://repo.spring.io/libs-release/javax/mail/mail/1.4.7/mail-1.4.7.pom'.

            > Could not HEAD 'https://repo.spring.io/libs-release/javax/mail/mail/1.4.7/mail-1.4.7.pom'.

               > peer not authenticated

   > Could not resolve javax.cache:cache-api:0.5.

     Required by:

         org.springframework:spring-context-support:3.2.19.BUILD-SNAPSHOT

      > Could not resolve javax.cache:cache-api:0.5.

         > Could not get resource 'https://repo.spring.io/libs-release/javax/cache/cache-api/0.5/cache-api-0.5.pom'.

            > Could not HEAD 'https://repo.spring.io/libs-release/javax/cache/cache-api/0.5/cache-api-0.5.pom'.

               > peer not authenticated

   > Could not resolve net.sf.ehcache:ehcache-core:2.0.1.

     Required by:

         org.springframework:spring-context-support:3.2.19.BUILD-SNAPSHOT

      > Could not resolve net.sf.ehcache:ehcache-core:2.0.1.

         > Could not get resource 'https://repo.spring.io/libs-release/net/sf/ehcache/ehcache-core/2.0.1/ehcache-core-2.0.1.pom'.

            > Could not HEAD 'https://repo.spring.io/libs-release/net/sf/ehcache/ehcache-core/2.0.1/ehcache-core-2.0.1.pom'.

               > peer not authenticated

   > Could not resolve org.quartz-scheduler:quartz:1.7.3

 

解决方案:

将gradle.properties中所有的https改成http

 

出现这样就成功了

1.3导入eclipse

直接import

 

出现这样就导入成功了

 

各个模块的作用:

org.springframework.aop

Spring的面向切面编程,提供AOP(面向切面编程)实现

org.springframework.aspects

Spring提供对AspectJ框架的整合

org.springframework.beans

SpringIoC(依赖注入)的基础实现

org.springframework.context.support

Spring-context的扩展支持,用于MVC方面

org.springframework.context

Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等

org.springframework.core

Spring3.x的核心工具包

org.springframework.expression

Spring表达式语言

org.springframework.instrument.tomcat

Spring3.x对Tomcat的连接池的集成

org.springframework.instrument

Spring3.x对服务器的代理接口

org.springframework.jdbc

对JDBC的简单封装

org.springframework.jms

为简化JMS API的使用而作的简单封装

org.springframework.orm

整合第三方的ORM框架,如hibernate,ibatis,jdo,以及 spring的JPA实现

org.springframework.oxm

Spring 对Object/XMl的映射支持,可以让Java与XML之间来回切换

org.springframework.test

对Junit等测试框架的简单封装

org.springframework.transaction

为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理

org.springframework.web.portlet

基于protlet的MVC实现

org.springframework.web.servlet

基于servlet的MVC实现

org.springframework.web.struts

整合Struts的时候的支持

org.springframework.web

SpringWeb下的工具包

 

 

1.4搭建demo测试ioc

新建一个maven工程,目录结构如下:

创建文件

TestIoc.class

public class TestIoc {  
    public static void main(String[] args){  
        System.setProperty("beanFile", "bean.xml");  
          
        ApplicationContext ctx = new ClassPathXmlApplicationContext("${beanFile}") ;//读取bean.xml中的内容  
        Person p = ctx.getBean("person", Person.class);//创建bean的引用对象  
        p.info();  
    }  
}  

 

Person.class

public class Person {  
      
    private String name;  
    private int age;  
      
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
      
    public void say(){  
        System.out.println("hello");  
    }  
      
    public void info(){  
        System.out.println("name:"+getName()+" age:"+getAge());  
    }  
} 

bean.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"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:p="http://www.springframework.org/schema/p"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd">  
    <bean id="person" class="com.rzzx.bean.Person">  
        <property name="name" value="laowang"/>  
        <property name="age" value="32"/>  
    </bean>  
</beans>

 

项目导入:

运行出现:

就ok了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李文区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值