Spring框架
概念
什么是Spring
`Spring`在不同的场景下表示不同的内容,它可以用来表示`Spring Framework项目`本身,是所有的`Spring项目`的基础。
`Spring框架`和`Spring项目系列`通常简称为`Spring`。
其核心基础是`IoC`(Inversion of Control:控制反转)容器和`AOP`(Aspect Oriented Programming:面向切面编程)。
什么是JavaBean
在Java
编程当中,可重用的Java组件
作用
使得
Java
编程更快、更容易、更安全。
方便解耦简化开发
AOP
编程的支持
声明式事务的支持
方便程序的测试
Spring Framework
框架体系
使用步骤
- 声明
JavaBean
- 书写
Spring Config
配置文件 - 使用时创建
IoC
容器 - 使用容器的
getBean()
方法
Spring
配置分析
Bean
标签基本配置
用于配置
Java
中的对象并交由Spring
来创建
默认情况下调用无参构造,如果没有则不能成功
属性 | 描述 |
---|---|
id | Bean 实例在 Spring 容器中的唯一标识 |
class | Bean 的全路径限定名称 |
Bean
标签作用范围
scope
属性:指 Spring
创建的 bean
对象的作用范围
取值范围 | 描述 |
---|---|
singleton | 默认 单例 |
prototype | 多实例 每次调用容器 getBean() 都会创建一个新的实例 |
request | WEB 项目中,Spring 创建一个 Bean 对象,将对象存入到 request 域中 |
session | WEB 项目中,Spring 创建一个 Bean 对象,将对象存入到 session 域中 |
global session | WEB 项目中,应用在 Portlet 环境,如果没有 Portlet 环境那么相当于 session |
取值 | 数量 | 创建 | 运行 | 销毁 |
---|---|---|---|---|
singleton | 1 | 当 Spring 核心文件被加载时,实例化配置的 Bean 实例当应用加载,创建容器时,对象就被创建 | 只要容器存在,对象就存在 | 当应用卸载时,销毁容器时,对象被销毁 |
prototype | n | 当调用 getBean() 方法时实例化 Bean | 只要对象在使用中,就一直存活 | 当对象长时间不用时,会被 Java 的垃圾回收器回收 |
Bean
标签生命周期配置
属性 | 描述 |
---|---|
init-method | 指定类中在对象创建后需要初始化内容而执行的方法名称 |
destroy-method | 指定类中在对象销毁时需要执行的方法名称 |
Bean
对象实例化的三种方式
- 使用无参构造
它会根据默认无参构造方法来创建类对象,如果bean中没有默认无参构造函数,将会创建失败
<!--使用无参构造实例化bean-->
<bean id="sectionDao" class="demo.dao.impl.SectionDaoImpl"/>
- 工厂静态方法实例化
工厂的静态方法返回Bean实例
<bean id="sectionDao" class="demo.factory.StaticBeanFactory" factory-method="createSectionDao"/>
- 工厂实例方法实例化
工厂的非静态方法返回Bean实例
<!--创建工厂-->
<bean id="factoryBean" class="demo.factory.DynamicBeanFactory"/>
<!--通过工厂创建bean-->
<bean id="sectionDao" factory-bean="factoryBean" factory-method="createSectionDao"/>
Bean
对象的依赖注入
依赖注入:它是
Spring
框架核心IoC
的具体实现
依赖:一个对象的某个功能,需要借助另一个对象来实现
在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。
IoC 解耦只是降低他们的依赖关系,但不会消除。
- 通过有参构造依赖注入
<!--配置Service-->
<bean id="sectionService"
class="demo.service.impl.SectionServiceImpl">
<!-- 构造方法 -->
<constructor-arg name="sectionDao" ref="sectionDao"/>
</bean>
- 使用
property
标签调用setter
方法
<!--配置Service-->
<bean id="sectionService" class="demo.service.impl.SectionServiceImpl">
<!--调用属性对应的setter方法-->
<property name="sectionDao" ref="sectionDao"/>
</bean>
这里的 name
属性实际是调用 setXxxx
的方法
- 使用
p
命名空间 调用属性的setter
方法
添加命名空间标识:xmlns:p=“http://www.springframework.org/schema/p”
<bean id="sectionService"
class="demo.service.impl.SectionServiceImpl"
p:sectionDao-ref="sectionDao"
>
</bean>
Bean
可注入依赖的数据类型
名称 | 属性或标签 | 对应注入数据类型或作用 |
---|---|---|
bean | 标签 | 创建对象 |
value | 属性 | 基本数据类型和字符串 |
ref | 属性 | 以定义对象 |
list | 标签 | 数组或List集合 |
array | 标签 | 数组或List集合 |
map | 标签 | Map集合 |
props | 标签 | Properties类型的数据 |
多配置文件的使用
实际开发中,
Spring
的配置内容非常多,这就导致Spring
配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,而在Spring
主配置文件通过import
标签进行加载
<import resource="spring-xxx.xml"/>
Spring
相关 API
ApplicaitionContext
的继承体系结构
ApplicationContext
接口类型,代表应用上下文,可以通过其实例获得 Spring 容器中的 Bean 对象
ApplicationContext
的实现类
ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件
AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建
Spring
容器。它用来读取注解
FileSystemXmlApplicationContext
可以从磁盘的任何路径读取配置文件
getBean
方法的重载
源码:
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>This method allows a Spring BeanFactory to be used as a replacement for the
* Singleton or Prototype design pattern. Callers may retain references to
* returned objects in the case of Singleton beans.
* <p>Translates aliases back to the corresponding canonical bean name.
* <p>Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to retrieve
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no bean with the specified name
* @throws BeansException if the bean could not be obtained
*/
Object getBean(String name) throws BeansException;
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Behaves the same as {@link #getBean(String)}, but provides a measure of type
* safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the
* required type. This means that ClassCastException can't be thrown on casting
* the result correctly, as can happen with {@link #getBean(String)}.
* <p>Translates aliases back to the corresponding canonical bean name.
* <p>Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to retrieve
* @param requiredType type the bean must match; can be an interface or superclass
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanNotOfRequiredTypeException if the bean is not of the required type
* @throws BeansException if the bean could not be created
*/
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
* overriding the specified default arguments (if any) in the bean definition.
* @param name the name of the bean to retrieve
* @param args arguments to use when creating a bean instance using explicit arguments
* (only applied when creating a new instance as opposed to retrieving an existing one)
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanDefinitionStoreException if arguments have been given but
* the affected bean isn't a prototype
* @throws BeansException if the bean could not be created
* @since 2.5
*/
Object getBean(String name, Object... args) throws BeansException;
/**
* Return the bean instance that uniquely matches the given object type, if any.
* <p>This method goes into {@link ListableBeanFactory} by-type lookup territory
* but may also be translated into a conventional by-name lookup based on the name
* of the given type. For more extensive retrieval operations across sets of beans,
* use {@link ListableBeanFactory} and/or {@link BeanFactoryUtils}.
* @param requiredType type the bean must match; can be an interface or superclass
* @return an instance of the single bean matching the required type
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @since 3.0
* @see ListableBeanFactory
*/
<T> T getBean(Class<T> requiredType) throws BeansException;
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
* overriding the specified default arguments (if any) in the bean definition.
* <p>This method goes into {@link ListableBeanFactory} by-type lookup territory
* but may also be translated into a conventional by-name lookup based on the name
* of the given type. For more extensive retrieval operations across sets of beans,
* use {@link ListableBeanFactory} and/or {@link BeanFactoryUtils}.
* @param requiredType type the bean must match; can be an interface or superclass
* @param args arguments to use when creating a bean instance using explicit arguments
* (only applied when creating a new instance as opposed to retrieving an existing one)
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanDefinitionStoreException if arguments have been given but
* the affected bean isn't a prototype
* @throws BeansException if the bean could not be created
* @since 4.1
*/
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;