
Spring 的核心
Spring之中最重要的当属Bean了,Spring实际上就是面向Bean的编程,Bean对于Spring的意义就好比Object对于OOP的意义一样。那么,三个核心组件之间是如何协同工作的呢?如果把Bean比作一场演出中的演员,那么Context就是这场演出的舞台,Core就是演出的道具,至于演出的节目,就是Spring的一系列特色功能了。
我们知道Bean包裹的是Object,而Object中必然有数据,Context就是给这些数据提供生存环境,发现每个Bean之间的关系,为他们建立并维护好这种关系。这样来说,Context就是一个Bean关系的集合,这个关系集合就是我们所说的IOC容器。那么Core又有什么作用呢?Core就是发现、建立和维护每个Bean之间的关系所需的一系列工具,就是我们经常说的Util。
Bean 组件
SpringBean的创建是典型的工厂模式,其工厂的继承层次关系如图所示:
public interface BeanFactory {
//...
Object getBean(String name);
}
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
/** Map of bean definition objects, keyed by bean name */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>;
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition){
//...
}
}
public class BeanDefinition {
private Object bean;
private Class<?> beanClass;
private String beanClassName;
// Bean 属性字段的初始化值
private BeanPropertyValues beanPropertyValues;
//...
}
Spring Bean 工厂生产 Bean 时
-
先将实例的类型参数保存到 beanClass 和 beanClassName,将需要初始化的字段名和值保存到 beanPropertyValues 中,这个过程 Spring 通过控制反转来实现,本文第二小节将予以简要说明 -
生成 bean 实例,并利用反射机制将需要初始化的字段值写入 bean 实例,将实例保存在 bean 中,完成 BeanDefinition 的构建。
假设我们已经完成了步骤 1) 的操作,之后的过程用代码表述如下所示。
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition){
//生成 bean 实例,并完成初始化
Object bean = createBean(beanDefinition);
//将 bean 实例保存在 beanDefinition 中
beanDefinition.setBean(bean);
//将 beanDefinition 实例保存在 Spring 容器中
beanDefinitionMap.put(beanName, beanDefinition);
}
protected Object createBean(BeanDefinition beanDefinition) {
try{
Object bean = beanDefinition.getBeanClass().newInstance();
try {
setBeanPropertyValues(bean, beanDefinition);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException e) {
e.printStackTrace();
}
return bean;
}catch(InstantiationException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
return null;
}
protected void setBeanPropertyValues(Object bean, BeanDefinition beanDefinition) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
for(PropertyValue pv : beanDefinition.getBeanPropertyValues().getBeanPropertyValues()){
Field beanFiled = bean.getClass().getDeclaredField(pv.getName());
beanFiled.setAccessible(true);
beanFiled.set(bean, pv.getValue());
}
}
Context 组件
前面说到,Context组件的作用是给Spring提供一个运行时的环境,用以保存各个对象的状态,我们来看一下与Context相关的类结构图。
从图中可以看出,Context类结构的顶级父类是ApplicationContext,它除了能标识一个应用环境的基本信息以外,还继承了5个接口,这5个接口主要是扩展了Context的功能。ApplicationContext的子类主要包含两个方向,图中已作说明。再往下就是构建Context的文件类型,接着就是访问Context的方式。
public class SampleService {
private String service;
public String getService() {
return service;
}
public void setService(String service) {
this.service= service;
}
}
<beans>
<bean name="sampleService " class="com.service.SampleService ">
<property name="service" value="This is a service"></property>
</bean>
</beans>
public class ResourceLoader {
public Resource getResource(String location){
URL resource = this.getClass().getClassLoader().getResource(location);
return new UrlResource(resource);
}
}
// UrlResource 的功能代码
public class UrlResource implements Resource {
private final URL url;
public UrlResource(URL url){
this.url = url;
}
public InputStream getInputStream() throws IOException {
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
return urlConnection.getInputStream();
}
}
public class SampleController {
/**
* 3\. 注解注入
**/
/* @Autowired */
private SampleService sampleService;
/**
* 1\. 构造器注入
**/
public SampleController(SampleService sampleService){
this.sampleService = sampleService;
}
//无参构造函数
public SampleController(){}
// 类的核心功能
public void process(){
System.out.println(sampleService.getService());
}
/**
* 2\. setter 注入
**/
/*public void setService(SampleService service) {
this.service= service;
}*/
}
<beans>
<bean name="sampleService " class="com.service.SampleService ">
<property name="service" value="This is a service"></property>
</bean>
<!-- 1\. 构造器注入方式为SampleContorller 的 bean 注入 SampleService -->
<bean name="sampleContorller" class="com.controller.SampleContorller">
<!-- index 是构造方法中相应参数的顺序 -->
<constructor-arg index="0" ref="sampleService"></constructor-arg>
</bean>
<!-- 2\. setter 注入方式为SampleContorller 的 bean 注入 SampleService -->
<!--
<bean name="sampleContorller" class="com.controller.SampleContorller">
<property name="sampleService " ref="sampleService"></property>
</bean>
-->
<!-- 3\. 注解注入方式为SampleContorller 的 bean 注入 SampleService -->
<!--
<bean name="sampleContorller" class="com.controller.SampleContorller">
<!-- 不需要配置,Spring 自动按照类型注入相应的 bean -->
</bean>
-->
</beans>
Core组件
Core组件一个重要的组成部分就是定义了资源的访问方式。Core组价把所有的资源都抽象成一个接口,这样,对于资源使用者来说,不需要考虑文件的类型。对资源提供者来说,也不需要考虑如何将资源包装起来交给别人使用(Core组件内所有的资源都可以通过InputStream类来获取)。另外,Core组件内资源的加载都是由ResourceLoader接口完成的,只要实现这个接口就可以加载所有的资源。
那么,Context和Resource是如何建立关系的呢?通过前面Context的介绍我们知道,Context组件里面的类或者接口最终都实现了ResourcePatternResolver接口,ResourcePatternResolver接口的作用就是加载、解析和描述资源。这个接口相当于Resource里面的一个接头人,它把Resource里的资源加载、解析和定义整合到一起,便于其他组件使用。
前面介绍了三大核心组件的结构与相互关系,那么,这三大组件是如何让Spring完成诸如IOC和AOP等各种功能的呢?敬请期待下一篇文章!
最后留一个问题:Spring 为什么能够长盛不衰?


本文分享自微信公众号 - JAVA高级架构(gaojijiagou)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/3999152/blog/4434671