spring是个顶级的框架,这话没毛病。很多人想把它征服,想去阅读它的源码,弄懂它的设计思想,从中学到精粹。

但是很多次打开后,看到庞大的体系结构,就懵逼了,不知从何入手。

我在这里总结下学习spring的切入点:

  • IOC

    控制反转,是spring的核心吧,对于bean的生面周期的管理。

  • AOP

    面向切面编程,基于JDK动态代理和cglib字节码实现。

首先从IOC开始吧,控制反转,就是将对象的创建转移给框架,不需要你去new,你只需要通过配置或者注解来让它知道从哪入手。这样做有什么好处?

  1. 不用再编写创建对象的代码和维持其复杂的依赖关系。
  2. 定义一个接口,可以方便更改其实现类,或者注入需要的属性。

然后就是自己写个简单的接口和实现,开始debug,通过xml配置的方式来注入bean:

<bean id="test" class="com.fcs.xmls.TestBean">
<!-- 配置属性phone -->
<property name="phone" value="15507516532"></property>
<!-- 配置属性age,虽然此处是字符串“25”,但是Spring会识别age的类型,然后把字符串“14”转变后赋值给age -->
<property name="age" value="25"></property>
</bean>

用下面的方式获取bean:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
TestBean t = (TestBean) ac.getBean("test");

用idea弄个类继承关系图:

可以看到BeanFactory,从命名可以看出来这是个工厂模式,所以从这里你需要了解工厂模式(工厂方法模式)。

在跟踪的过程中,很容易就跳到了AbstractApplicationContext 中的refresh 方法,在这个方法做了一系列操作:

    public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory); try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
initMessageSource(); // Initialize event multicaster for this context.
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
onRefresh(); // Check for listener beans and register them.
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
} // Destroy already created singletons to avoid dangling resources.
destroyBeans(); // Reset 'active' flag.
cancelRefresh(ex); // Propagate exception to caller.
throw ex;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}

同样,用注解来一波:

@Component
public class MessagePrinter { final private MessageService service; @Autowired
public MessagePrinter(MessageService service) {
this.service = service;
} public void printMessage() {
System.out.println(this.service.getMessage());
} }
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);

也可得到下图:

两者的上层结构基本相同,分离点也在AbstractApplicationContext ,各自有了不同的扩展。所以重点还是在AbstractApplicationContext 的refresh 方法。

这里可以看到第二个模式:模板方法模式。在AbstractApplicationContext 中定义算法簇,子类做不同的具体实现。

这就是切入点了。只是简单的分析下,细节还得去看,如果看不下去,还得学习设计模式。此外思想可以参考spring-tiny,一个简单的ioc容器实现。

Spring学习的切入点的更多相关文章

  1. Spring学习之切入点表达式

    链接地址:http://jinnianshilongnian.iteye.com/blog/1415606

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  4. 我的Spring学习记录(二)

    本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...

  5. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  6. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  7. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  8. spring学习 8-面试(事务,解决线程安全)

    1.介绍一下Spring的事物管理 参考:Spring 学习7 -事务 2.Spring如何处理线程并发问题    Spring使用ThreadLocal解决线程安全问题 参考:Spring学习11- ...

  9. spring 学习(五):spring 事务

    spring 学习(五):spring 事务 事务概要 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: 为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同 ...

随机推荐

  1. 使用jquery.qrcode生成二维码及常见问题解决方案

    转载文章  使用jquery.qrcode生成二维码及常见问题解决方案 一.jquery.qrcode.js介 jquery.qrcode.js 是一个纯浏览器 生成 QRcode 的 jQuery ...

  2. vijos 1002 简单压缩+DP

    描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上 ...

  3. 上下文管理器 contextlib

    from contextlib import contextmanager @contextmanager def tag(name): print "<%s>" % ...

  4. Parallel

    介绍 C# 4.0 的新特性之并行运算 Parallel.For - for 循环的并行运算 Parallel.ForEach - foreach 循环的并行运算 Parallel.Invoke - ...

  5. 【CodeForces】788E New task

    [题意]n个数,每个数有附加属性0或1,初始全为1.m个操作,每个操作可以改变一个数字的属性为0或1.对于每次操作后的序列求有多少子序列满足要求:5个数字,中间3个数相等且属性为1,左右两个数小于等于 ...

  6. 联系博主 Contact

    李莫 / Ray OI 蒟蒻一只 / A Player of Olympiad in Informatics QQ:740929894 邮箱 / Email :rayking2017@outlook. ...

  7. Eureka服务续约(Renew)源码分析

    主要对Eureka的Renew(服务续约),从服务提供者发起续约请求开始分析,通过阅读源码和画时序图的方式,展示Eureka服务续约的整个生命周期.服务续约主要是把服务续约的信息更新到自身的Eurek ...

  8. HTTP响应码摘自apach官网

    HTTP状态列表 响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示: 1xx:信息,请求收到,继续处理 2xx:成功,行为被成功地接 ...

  9. 坐标转换——GCJ-02

    WGS84(World Geodetic System 1984),是为GPS 全球定位系统 使用而建立的坐标系统GCJ-02,我国在WGS84的基础上加密得到BD-09,百度坐标在GCJ-02基础上 ...

  10. Dubbo 用户手册学习笔记 —— Dubbo架构

    Dubbo的架构 节点角色说明 节点 角色说明 Provider 服务提供方 Consumer 服务消费方 Registry 服务注册与发现的注册中心 Monitor 统计服务的调用次数和调用时间的监 ...