Spring详解(一):简介
Spring Framework创始人:Rod Johnson. 计算机专业本科,音乐学博士。有着相当丰富的C/C++技术背景的Rod早在1996年就开始了对Java服务器端技术的研究。
轮子理论推崇者:
- 轮子理论:不用重复发明轮子
- IT 行业:直接使用写好的代码
Spring框架宗旨:不重新发明技术,让原有技术使用起来更加方便。
Spring优点:
- 方便解耦,简化开发,通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码造成的程序耦合度高。
- AOP编程的支持,通过Spring提供的AOP功能,方便进行面向切面编程。
- 声明式事务的支持,在Spring中,我们可以从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活地进行事务的管理,提高开发效率和质量。
- 方便程序的测试,可以用非容器依赖的编程方式进行几乎所有的测试工作。
- 方便集成各种优秀框架,Spring提供了对各种优秀框架的直接支持。
1. Spring 核心功能
- IOC/DI:控制反转/依赖注入
- AOP:面向切面的编程
- 声明式事务
2. Spring Framework Runntime
整个Spring框架按其所属功能可以划分为五个主要模块,这五个模块几乎为企业应用提供了所需的一切,从持久层、业务层到表现层都拥有相应的支持
2.1 Spring Framework Test
Spring可以用非容器依赖的编程方式进行几乎所有的测试工作,支持JUnit和TestNG等测试框架。
2.2 Spring Framework Core Container
Spring Framework 核心容器。它是Spring启动最基本的条件。
- Spring-Beans:模块下的所有类主要解决了三件事:Bean 的定义、Bean 的创建以及对 Bean 的解析。
- Spring-Core:Spring的核心模块实现了IoC的功能,它将类和类之间的依赖从代码中脱离出来,用配置的方式进行依赖关系描述。 由IoC容器负责类的创建,管理,获取等。BeanFactory接口是Spring框架的核心接口,实现了容器很多核心的功能。
- Spring-Context:Context模块构建于核心模块之上,扩展了BeanFactory的功能,包括国际化,资源加载,任务调度,管理注解等多项功能。ApplicationContext是Context模块的核心接口。
- Spring-SpEL:表达式语言(Expression Language)是统一表达式语言(EL)的一个扩展,支持设置和获取对象属性,调用对象方法,操作数组、集合等。使用它可以很方便的通过表达式和Spring IoC容器进行交互。
2.3 Spring Framework AOP
Spring AOP模块提供了满足AOP Alliance规范的实现,还整合了AspectJ这种AOP语言级的框架。通过AOP能降低耦合。
2.4 Spring Framework Aspects
切面AOP依赖的包
2.5 Spring Framework Data Access/Integration
Spring封装数据访问层相关内容,该模块包括了JDBC、ORM、OXM、JMS和事务管理
- 事务模块:该模块用于Spring管理事务,只要是Spring管理对象都能得到Spring管理事务的好处,无需在代码中进行事务控制了,而且支持编程和声明性的事务管理。
- JDBC模块:提供了一个JBDC的样例模板,使用这些模板能消除传统冗长的JDBC编码还有必须的事务控制,而且能享受到Spring管理事务的好处。
- ORM模块:提供与流行的“对象-关系”映射框架的无缝集成,包括Hibernate、JPA、MyBatis等。而且可以使用Spring事务管理,无需额外控制事务。
- OXM模块:提供了一个对Object/XML映射实现,将java对象映射成XML数据,或者将XML数据映射成java对象,Object/XML映射实现包括JAXB、Castor、XMLBeans和XStream。
- JMS模块:用于JMS(Java Messaging Service),提供一套 “消息生产者、消息消费者”模板用于更加简单的使用JMS,JMS用于用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。
2.6 Spring Framework Web
需要Spring 完成Web相关功能时需要使用的模块.
- Spring-Web模块提供基本的面向Web的集成功能,如多部分文件上传功能,使用Servlet侦听器的IoC容器的初始化以及面向Web的应用程序上下文。它还包含一个HTTP客户端和Spring远程处理支持的Web相关部分。
- Spring-Webmvc模块(也称为Web-Servlet模块)包含Spring的模型 - 视图 - 控制器(MVC)和Web应用程序的REST Web服务实现。Spring的MVC框架提供了域模型代码和Web表单之间的清晰分离,并且与Spring框架的所有其他功能集成在一起。
- Spring-WebSocket:Spring对WebSocket封装的包
- Spring-Portlet
3. Spring 概念
从Spring3开始把Spring框架的功能拆分成多个jar,然而Spring2及以前都是一个jar包。
3.1 容器(Container)
实际上,容器是Spring框架实现功能的核心,容器不只是帮我们创建了对象那么简单,它负责了对象整个的生命周期的管理——创建、装配、销毁。关于Spring的这个容器你最常听闻的一个术语就是IOC容器。所谓IOC,是一种叫控制反转的编程思想,网上有很通俗易懂的总结,我就不胡乱阐述了。总之一句话,我的应用程序里不用再过问对象的创建和管理对象之间的依赖关系了,都让IOC容器给代劳吧,也就是说,我把对象创建、管理的控制权都交给Spring容器,这是一种控制权的反转,所以Spring容器才能称为IOC容器。不过这里要澄清一点:并不是说只有Spring的容器才叫IOC容器,基于IOC容器的框架还有很多,并不是Spring特有的。
3.2 BeanFactory接口(老版本)
BeanFactory,这是最简单的容器,只能提供基本的DI功能:对Bean的定义、创建、解析功能.
我们来看一看BeanFactory接口:
package org.springframework.beans.factory;
public interface BeanFactory {
/**
* 用来引用一个实例,或把它和工厂产生的Bean区分开,就是说,如果一个FactoryBean的名字为a,那么,&a会得到那个Factory
*/
String FACTORY_BEAN_PREFIX = "&";
/*
* 四个不同形式的getBean方法,获取实例
*/
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
boolean containsBean(String name); // 是否存在
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;// 是否为单实例
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;// 是否为原型(多实例)
boolean isTypeMatch(String name, Class<?> targetType)
throws NoSuchBeanDefinitionException;// 名称、类型是否匹配
Class<?> getType(String name) throws NoSuchBeanDefinitionException; // 获取类型
String[] getAliases(String name);// 根据实例的名字获取实例的别名
}
具体:
- 4个获取实例的方法。getBean的重载方法。
- 4个判断的方法。判断是否存在,是否为单例、原型,名称类型是否匹配。
- 1个获取类型的方法、一个获取别名的方法。根据名称获取类型、根据名称获取别名。一目了然!
3.3 ApplicationContext接口(新版本)
ApplicationContext是BeanFactory的子接口,BeanFactory的功能在ApplicationContext中都有。继承了BeanFactory后派生而来的ApplicationContext,它能提供更多企业级的服务,例如解析配置文本信息等等,这也是ApplicationContext最常见的应用场景。有了上下文对象,我们就能向容器注册需要Spring管理的对象了。
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
/**
* Return the unique id of this application context.
* @return the unique id of the context, or {@code null} if none
*/
String getId();
/**
* Return a name for the deployed application that this context belongs to.
* @return a name for the deployed application, or the empty String by default
*/
String getApplicationName();
/**
* Return a friendly name for this context.
* @return a display name for this context (never {@code null})
*/
String getDisplayName();
/**
* Return the timestamp when this context was first loaded.
* @return the timestamp (ms) when this context was first loaded
*/
long getStartupDate();
/**
* Return the parent context, or {@code null} if there is no parent
* and this is the root of the context hierarchy.
* @return the parent context, or {@code null} if there is no parent
*/
ApplicationContext getParent();
/**
* Expose AutowireCapableBeanFactory functionality for this context.
* <p>This is not typically used by application code, except for the purpose of
* initializing bean instances that live outside of the application context,
* applying the Spring bean lifecycle (fully or partly) to them.
* <p>Alternatively, the internal BeanFactory exposed by the
* {@link ConfigurableApplicationContext} interface offers access to the
* {@link AutowireCapableBeanFactory} interface too. The present method mainly
* serves as a convenient, specific facility on the ApplicationContext interface.
* <p><b>NOTE: As of 4.2, this method will consistently throw IllegalStateException
* after the application context has been closed.</b> In current Spring Framework
* versions, only refreshable application contexts behave that way; as of 4.2,
* all application context implementations will be required to comply.
* @return the AutowireCapableBeanFactory for this context
* @throws IllegalStateException if the context does not support the
* {@link AutowireCapableBeanFactory} interface, or does not hold an
* autowire-capable bean factory yet (e.g. if {@code refresh()} has
* never been called), or if the context has been closed already
* @see ConfigurableApplicationContext#refresh()
* @see ConfigurableApplicationContext#getBeanFactory()
*/
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}