Spring框架

Spring框架

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 来创建
默认情况下调用无参构造,如果没有则不能成功

属性描述
idBean 实例在 Spring 容器中的唯一标识
classBean 的全路径限定名称
Bean 标签作用范围

scope 属性:指 Spring 创建的 bean 对象的作用范围

取值范围描述
singleton默认
单例
prototype多实例
每次调用容器 getBean() 都会创建一个新的实例
requestWEB 项目中,Spring 创建一个 Bean 对象,将对象存入到 request 域中
sessionWEB 项目中,Spring 创建一个 Bean 对象,将对象存入到 session 域中
global sessionWEB 项目中,应用在 Portlet 环境,如果没有 Portlet 环境那么相当于 session
取值数量创建运行销毁
singleton1Spring 核心文件被加载时,实例化配置的 Bean 实例
当应用加载,创建容器时,对象就被创建
只要容器存在,对象就存在当应用卸载时,销毁容器时,对象被销毁
prototypen当调用 getBean() 方法时实例化 Bean只要对象在使用中,就一直存活当对象长时间不用时,会被 Java 的垃圾回收器回收
Bean 标签生命周期配置
属性描述
init-method指定类中在对象创建后需要初始化内容而执行的方法名称
destroy-method指定类中在对象销毁时需要执行的方法名称
Bean 对象实例化的三种方式
  1. 使用无参构造

它会根据默认无参构造方法来创建类对象,如果bean中没有默认无参构造函数,将会创建失败

<!--使用无参构造实例化bean-->
<bean id="sectionDao" class="demo.dao.impl.SectionDaoImpl"/>
  1. 工厂静态方法实例化

工厂的静态方法返回Bean实例

<bean id="sectionDao" class="demo.factory.StaticBeanFactory" factory-method="createSectionDao"/>
  1. 工厂实例方法实例化

工厂的非静态方法返回Bean实例

<!--创建工厂-->
<bean id="factoryBean" class="demo.factory.DynamicBeanFactory"/>
<!--通过工厂创建bean-->
<bean id="sectionDao" factory-bean="factoryBean" factory-method="createSectionDao"/>
Bean 对象的依赖注入

依赖注入:它是 Spring 框架核心 IoC 的具体实现
依赖:一个对象的某个功能,需要借助另一个对象来实现

在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。
IoC 解耦只是降低他们的依赖关系,但不会消除。

  1. 通过有参构造依赖注入
<!--配置Service-->
<bean id="sectionService" 
      class="demo.service.impl.SectionServiceImpl">
    <!-- 构造方法 -->
    <constructor-arg name="sectionDao" ref="sectionDao"/>
</bean>
  1. 使用 property 标签调用 setter 方法
<!--配置Service-->
<bean id="sectionService" class="demo.service.impl.SectionServiceImpl">
    <!--调用属性对应的setter方法-->
    <property name="sectionDao" ref="sectionDao"/>
</bean>

这里的 name 属性实际是调用 setXxxx 的方法

  1. 使用 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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值