前言java
最近因为一些工做的须要,还有本身知识的匮乏再次翻开spring。正好整理了一下相关的知识,弥补了以前对spring的一些错误认知。这一次学习,更加深刻的理解了Ioc和AOP的思想,并对其架构模块有了更深一步的理解。web
刚开始翻看spring技术内幕,虽然有了一点看源码的经验,可是直接看如此深的源码,仍是很头疼。spring因为业务的扩展,以及用户群的增长,对于某些模块的类封装的很深!所以追溯源码是个很头疼的问题,而直接看这本书,也是压力山大。正则表达式
因而回去复习一下spring的基本知识,先学会走,在学跑。spring
先说说spring的核心模块相关的概念,IOC和AOP。编程
IoC(inversion of control)控制反转,也就是说,控制权由对象转移,对象本身变成了被动的接受。架构
DI(dependency injection)依赖注入app
AOP(aspect oriented programming)面向切面编程框架
AOP中利用面向切面的编程理论,把每一个方法或者函数当作一个切面,在这个切面先后均可以执行不一样的方法。ide
1 <bean id="service"> 2 <property name="interceptorNames" value="advisor"></property> 3 <property name="target">目标</property> 4 </bean>
这里定义了一个拦截器,一个目标bean。函数
1 <bean id="advisor" class="..."> 2 <property name="advice">//拦截器对象 3 </property> 4 <property name="mappedName">//拦截的方法 5 </property> 6 </bean>
这个目标bean在执行时,会根据配置来肯定切面,精确到某个方法。而拦截的方法由上面的mappedName肯定,能够经过名字或者经过正则表达式来肯定。拦截器对象肯定了拦截时触发的函数。
能够进行方法前拦截、方法后拦截、异常拦截。
能够看到Spring Core是spring框架中最低层核心的模块。提供了最基本的Ioc思想框架。
因为spring是把一个类配置在xml配置文件中,从而进行一些相关的依赖或者注入等等。那么这个配置文件,确定要被本地化存储而且进行读取。
所以在咱们使用前,要进行三步:
读取spring配置文件
bean定义
注入到Ioc容器
以后咱们就能够经过bean工厂的getBean获取指定的bean了。
1 最常见的读取配置文件的三种方法:
1 利用ClassPathResource读取资源文件,传入XmlBeanFactory中
1 ClassPathResource res = new ClassPathResource("application.xml") 2 XmlBeanFactory factory - new XmlBeanFactory(res); 3 factory.getBean("testBean");
2 利用输入流,传入到XmlBeanFactory中
1 InputStream is = new FileInputStream("C:\\ApplicationContext.xml"); 2 XmlBeanFactory factory - new XmlBeanFactory(is);
3 直接经过ClassPathXmlApplicationContext读取资源文件
ClassPathXmlAppliationContext appContext = new ClassPathXmlApplicationContext("aaplicationContext.xml");
4 经过在web.xml中指定配置文件的目录,自动读取。关于配置文件的路径有不少种写法,经过指定ClassPath也能够。
1 <listener> 2 <listener-class> 3 org.springframework.web.context.ContextLoaderListener 4 </listener-class> 5 </listener> 6 7 <context-param> 8 <param-name> contextConfigLocation</param-name> 9 <param-value> 10 /WEB-INF/classes/bean.xml 11 </param-value> 12 </context-param>
2 Bean的定义这部分就没什么好说的了,就是读取配置文件,解析里面的文件定义,抽取成数据模型。
3 而注入到Ioc容器中,就是把这个数据模型读取到一个总的BeanFactory中,统一的进行管理。
咱们就能够经过这个BeanFactory进行一些操做,好比GetBean()或者containsBean()等等。
下面简单的罗列了一些配置的内容:
<bean id="examples.MyFactoryBean" factory-method="createInstance"/>
2 构造函数
<bean id="testBean" class="examples.ExampleBean"> <constructor-arg><ref bean="testArgs1"/></construtor-arg> <constructor-arg><ref bean="testArgs2"/></construtor-arg> </bean> 至关于 class testBean{ public testBean(testArgs1,testArgs2); }
3 单例模式
<bean id="testBean" class="test.testBean" singleton="false"> 每次请求生成的都是同一个bean
4 <idef> 与 <ref>做用相同,可是idef会在xml加载时 检查 bean是否存在
destroy-method 销毁,一般都是配置数据源、JDBC、输入输入流时,须要在close时销毁对象 depends-on 前后顺序实例化 init-method 初始化调用方法
BeanNameAware 让类知道本身bean在xml中的id名字 BeanFactoryAware 得到beanFactory InitializingBean接口执行初始化方法 Disposablebean 执行销毁方法
9 属性覆盖,这个一般是在配置了一些属性,可是有其余的一些文件修改了这个属性时,使用。
好比说,咱们在项目中经过spring的配置文件配置了数据源的相关信息,可是额外有一个xxx.properties进行信息的重写。当有这个文件时,可让客户本身修改其链接信息,而不用每次都去修改繁琐的spring配置文件。
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer