Spring study notes

 
 
 
 
Spring 's Ioc container
All components are passive, and the container is responsible for all component initialization and invocation. Components are in a container, which is managed by the container.
BeanFactory determines the implementation of beans in the container according to the configuration file, and manages the dependencies between beans.
 
In general, it is recommended to use the ref element instead of the value element when the attribute value is another bean in the container.
 
 
Introduction to Spring Development Kit – Core Development Kit
When building a Spring project, it is necessary to introduce the Spring development kit, otherwise the development and operation environment of Spring cannot be established. The following briefly introduces the basic purpose of Spring's core development kit:
Spring Core
Spring Beans
Spring AOP
Spring Context
 
The following development kit files are not Spring core development kits, but provide various enterprise-level services:
Spring Aspects
Spring Context Support
Spring Expression
Spring Framework Bom
Spring Instrument
Spring Instrument Tomcat
Spring JDBC
Spring JMS
 
Spring orm
Spring oxm
Spring Struts
Spring test
Spring tx
Spring web
Spring webmvc
Spring webmvc portlet
 
IoC Overview – Injection Types of IoC
From the point of view of injection method, it is mainly divided into three types:
Constructor injection
property injection
interface injection
 
Resource access tool class
The classes for accessing resources provided by JDK cannot meet the access requirements of various underlying resources. Therefore, Spring has designed a Resource interface, which provides applications with a stronger ability to access underlying resources:
 
Main method:
boolean exists()
boolean isOpen()
URL getURL()
File getFile()
InputStream getInputStream()
 
具体实现类:
ByteArrayResource
ClassPathResource
FileSystemResource
InputStreamResource
ServletContextResource
UrlResource
 
 
为了访问不同类型的资源,必须使用相应的Resource实现类,这是比较麻烦的,Spring提供了一个强大的加载资源的机制,能够自动识别不同的资源类型:
资源类型地址前缀:
classpath classpath:com/jike/bean.xml
File file:/com/jike/bean.xml
http:// http://www.jike.com/bean.xml
ftp ftp://www.jike.com/bean.xml
无前缀 com/jike/bean.xml
 
Ant风格的匹配符:
?:匹配文件名中的一个字符
*:匹配文件名中的任意字符
**:匹配多层路径
Ant风格的资源路径示例:
Classpath:com/t*st.xml
File:D:/conf/*.xml
Classpath:com/**/test.xml
Classpath:org/springframework/**/*.xml
 
 
BeanFactory和ApplicationContext的介绍
BeanFactory是Spring框架最核心的接口,它提供了高级 IoC的配置机制。ApplicationContext建立在BeanFactory基础之上,提供了更多面向应用的功能,它提供了国际化支持和框架事件体系,更易于创建实际应用一般称BeanFactory为IoC容器,而称ApplicationContext为应用上下文:
 
BeanFactory和ApplicationContext的介绍 – BeanFactory的介绍
BeanFactory是一个类工厂,可以创建并管理各种类的对象,Spring称这些创建和管理的Java对象为Bean。在Spring中,Java对象的范围更加宽泛。接下来我们对BeanFactory的类体系结构以及装载初始化顺序进行说明:
类体系结构:
XmlBeanFactory
ListableBeanFactory
HierarhicalBeanFactory
ConfigurableBeanFactory
AutowireCapableBeanFactory
SingletonBeanFactory
BeanDefinitionRegistry
 
初始化顺序:
1、创建配置文件
2、装载配置文件
3、启动IoC容器
4、获取Bean实例
 
BeanFactory和ApplicationContext的介绍 – ApplicationContext的介绍
ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用的功能。在BeanFactory中,很多功能需要以编程的方式方式实现,而在ApplicationContext中则可以通过配置的方式实现。接下来介绍一下ApplicationContext的实现类以及类体系结构:
具体实现类:
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
ConfigurableApplicationContext
 
扩展接口:
ApplicationEventPublisher
MessageSource
ReaourcePatternResolver
LifeCycle
 
和BeanFactory初始化相似,ApplicationContext的初始化也很简单,根据配置文件路径不同可以选择不同的实现类加载:
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
Bean的实例化问题
 
 
Bean的生命周期
Spring容器中的Bean拥有明确的生命周期,由多个特定的生命阶段组成,每个生命阶段都允许外界对Bean施加控制。在Spring中,我们从Bean的作用范围和实例化Bean时所经历的一系列阶段来描述Bean的生命周期:
BeanFactory中的Bean的生命周期
ApplicationContext中的Bean的生命周期
 
Bean的生命周期 – BeanFactory中的Bean的生命周期
 
如下通过一个图形化的方式进行描述BeanFactory中的Bean的生命周期:
 
 
Spring的AOP容器
 
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
自动把map转换为son字符串
 
 
ApplicationContext: 实用的Bean工厂
如果说BeanFactory是 spring的心脏,那么Application就是完整的身躯。ApplicationContext就是由BeanFactory派生出来的。
 
代理:
静态代理
jdk动态代理 项目中要有 一个拦截器实现 InvocationHandler
cglib动态代理 项目中要有 一个拦截器实现 MethodInterceptor
 
切面: log类、权限类、安全类
通知:切面中的方法
连接点:客户端调用哪个方法, 哪个方法就是连接点。
切入点:相当于条件,
 
可以通过切入点表达式,指定拦截哪些类的哪些方法; 给指定的类在运行的时候植入切面类代码。
切入点表达式:
 
 
织入:形成代理对象的方法的过程就称为织入。
 
什么是AOP(Aspect Oriented Programming),就是把反复使用的、非核心的代码提取出来。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325011038&siteId=291194637