Spring学习的切入点
spring是个顶级的框架,这话没毛病。很多人想把它征服,想去阅读它的源码,弄懂它的设计思想,从中学到精粹。
但是很多次打开后,看到庞大的体系结构,就懵逼了,不知从何入手。
我在这里总结下学习spring的切入点:
IOC
控制反转,是spring的核心吧,对于bean的生面周期的管理。AOP
面向切面编程,基于JDK动态代理和cglib字节码实现。
首先从IOC开始吧,控制反转,就是将对象的创建转移给框架,不需要你去new,你只需要通过配置或者注解来让它知道从哪入手。这样做有什么好处?
- 不用再编写创建对象的代码和维持其复杂的依赖关系。
- 定义一个接口,可以方便更改其实现类,或者注入需要的属性。
然后就是自己写个简单的接口和实现,开始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学习的切入点的更多相关文章
- Spring学习之切入点表达式
链接地址:http://jinnianshilongnian.iteye.com/blog/1415606
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- 我的Spring学习记录(二)
本篇就简单的说一下Bean的装配和AOP 本篇的项目是在上一篇我的Spring学习记录(一) 中项目的基础上进行开发的 1. 使用setter方法和构造方法装配Bean 1.1 前期准备 使用sett ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- spring学习 8-面试(事务,解决线程安全)
1.介绍一下Spring的事物管理 参考:Spring 学习7 -事务 2.Spring如何处理线程并发问题 Spring使用ThreadLocal解决线程安全问题 参考:Spring学习11- ...
- spring 学习(五):spring 事务
spring 学习(五):spring 事务 事务概要 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: 为数据库操作序列提供了一个从失败中恢复到正常状态的方法,同 ...
随机推荐
- [Luogu 3958] NOIP2017 D2T1 奶酪
题目链接 人生第一篇题解,多多关照吧. 注意事项: 1.多组数据,每次要先初始化. 2.因为涉及到开根,所以记得开double. 整体思路: 建图,判断「起点」与「终点」是否连通. 方法可选择搜索(我 ...
- pythonweb框架
https://www.cnblogs.com/sss4/p/8097653.html
- sscanf的用法
sscanf也太好用了8我竟然一直都不知道qaq #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- 【bzoj3648】环套树+点分治+树状数组
tree 1s 128M by hzw czy神犇种了一棵树,他想知道地球的质量 给定一棵n个点的树,求树上经过点的个数≥K的路径数量ans 对于部分数据,树上某两点间会多出最多一条无向边 输入数据 ...
- CCC2018游记
day (-1) 晚上睡觉没盖被子 day 0 2018年2月13日 下午放学回来感到一阵头痛,一量体温结果发烧了,感觉很蓝瘦,居然在CCC前一天生病. 本来注册了账号想打practise的,结果又 ...
- js localtion.href 数据传输
1.今天发现的一种数据发送 如下标红 <script> <%--测试juquery的代码如下操作.我们可以看出使用juquery 进行选择标签的属性可以更加方便--%> con ...
- BigDecimal的用法详解
BigDecimal 由任意精度的整数非标度值 和32 位的整数标度 (scale) 组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以 10 的负scale 次幂. f ...
- HTML5 Canvas时间效果
Canvas 时间效果: function clockTest() { var canvas = document.getElementById('canvas'); if (!(canvas &am ...
- hdu 3371(prim算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...
- input标签获取焦点时文本框内提示信息清空背景颜色发生变化
<input type="text" id="username" onfocus="myFocus(this,'#f4eaf1')" ...