Bean属性配置

Spring在读取配置文件中bean的metadata后会构造一个个BeanDefination对象。后面Spring会根据这些BeanDefinition创建对象。在配置一个bean的时候我们可以配置下面这些元素:

元素 备注
class 设定类的全限定名
name 设置Bean的名称,可以设置多个用逗号分隔
id/name Bean的唯一标识符,全局只能有一个
scope 设置Bean是单例还是原型,默认单例
constructor arguments 构造方法入参,进行依赖注入
properties 进行依赖注入
autowiring mode 自动注入模式
lazy-initialization mode 只对scope为单列的bean生效,设置为true会在getBean()时才创建bean实例
initialization method 设定Bean的初始化方法
destruction method 设定Bean的销毁方法

除了通过配置信息来创建Bean,Spring还允许通过接口的方式创建用户在容器外部创建的对象。(通过DefaultListableBeanFactory类的registerSingleton方法和registerBeanDefinition方法)

In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContextimplementations also permit the registration of existing objects that are created outside the container (by users). This is done by accessing the ApplicationContext’s BeanFactory through the getBeanFactory() method, which returns the BeanFactory DefaultListableBeanFactory implementation. DefaultListableBeanFactory supports this registration through the registerSingleton(..) and registerBeanDefinition(..) methods. However, typical applications work solely with beans defined through regular bean definition metadata.

虽然Spring容器还提供了registerSingleton(..)方法和registerBeanDefinition(..)来注册单例Bean,但是不建议自己使用这个方法,因为我们可能在依赖注入之后再注册了这个Bean。推荐使用配置BeanDefinition的方式来配置Bean。(其实这两个方法更多是Spring框架自己使用,在配置文件以外的一些地方再注册一些Bean到容器中)

关于上面表格中的id属性和name属性这边多说一句。

id和name属性都可以用来当做一个bean的标识符,他们两个的区别是id只能给这个bean指定一个标识符,而name属性可以同时给这个bean指定多个标识符(多个标识符之间用,隔开)。下面给出一个配置的列子

<!--经过下面的配置,bean1,bean2、name1、name2、alias1、alias2、alias3其实是一个Bean-->
<bean id="bean1,bean2" name="name1,name2" class="com.csx.demo.springdemo.service.MyBean1">
</bean>
<alias name="bean1,bean2" alias="alias1"/>
<alias name="name1" alias="alias2"/>
<alias name="name2" alias="alias3"/>

Bean命名

如果没有给Bean指定一个标识符,Spring容器会默认给这个Bean设置一个标识符。用户可以通过id和name两个属性来设置Bean的标识符,这些标识符需要在整个容器范围内唯一,同时name可以指定多个用逗号分隔。

实例化Bean

实例化Bean一般有四种方式:默认构造函数、待参数的构造函数、静态工厂类、实例方法创建Bean。需要我们注意的是Bean的实例化和依赖注入的区别。

  1. 默认构造函数方式
    <bean id="exampleBean" class="examples.ExampleBean"/>
  1. 带参数的构造函数
    <bean id="Hello2" class="com.csx.personal.web.services.HelloImpl2">
<constructor-arg index="0" value="chensongxia"/>
<constructor-arg index="1" value="zhaoru"/>
<constructor-arg index="2" ref="beanid"/>
</bean>
  1. 使用静态工厂类(不推荐使用)
    <bean id="bean3" class="cn.javass.spring.chapter2.HelloApiStaticFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello Spring!"/>
</bean>
<!-- 使用examples.ClientService这个类的createInstance方法创建bean -->
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/> public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
  1. 使用实例方法进行Bean创建
    <bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean>
<!-- 使用serviceLocator这个Bean的createClientServiceInstance方法创建Bean -->
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
<bean id="accountService" factory-bean="serviceLocator" factory-method="createAccountServiceInstance"/>
//类定义
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
public AccountService createAccountServiceInstance() {
return accountService;
}
}

如果我们要通过xml的形式配置一个静态内部类,可以参考下面的例子

For example, if you have a class called SomeThing in the com.example package, and this SomeThing class has a static nested class called OtherThing, the value of the class attribute on a bean definition would be com.example.SomeThing$OtherThing.

Bean的Scope

Spring默认支持的Scope

Scopes 描述
singleton 整个IOC容器中只有一个Bean
prototype 每次请求都会生成新的Bean
request 每个HTTP request都会生成一个新的Bean,只对web系列的ApplicationContext生效
session 每个session范围内生成一个新的Bean,只对web系列的ApplicationContext生效
Application ServletContext范围内生成一个新的Bean,只对web系列的ApplicationContext生效
websocket --

singleton类型的bean定义,在一个容器中只存在一个实例,所有对该类型bean的依赖都引用这一单一实例。

scope为prototype的bean,容器在接受到该类型的对象的请求的时候(调用getBean方法),会每次都重新 生成一个新的对象给请求方,虽然这种类型的对象的实例化以及属性设置等工作都是由容器负责的,但是只要准备完毕,并且对象实例返回给请求方之后,容器就不 在拥有当前对象的引用,请求方需要自己负责当前对象后继生命周期的管理工作,包括该对象的销毁。也就是说,容器每次返回请求方该对象的一个新的实例之后, 就由这个对象“自生自灭”了。

单例Bean依赖原型Bean

当一个单例的Bean依赖一个原型Bean时,由于单例只初始化一次,所以拿到的原型Bean也只是我们第一次初始化时拿到的Bean,并不能达到我们想要的效果。此时我们可以使用Spring提供的look-up方式的注入来解决这个问题。

Request, Session, Application, and WebSocket Scopes

这三种Scope只对Web系列的ApplicationContext的生效。可以视同@RequestScope、@SessionScope和@ApplicationScope使之生效。

自定义Scope

Spring还支持自定义scope,需要时可以学些下这个特性。

Bean的扩展点(注意和容器扩展点的区别)

Spring提供了一系列接口让用户来自定义Bean的属性(注意和容器扩展点那一个章节的区别),主要的接口有:

  • Lifecycle Callbacks;
  • ApplicationContextAware and BeanNameAware;
  • Other Aware Interfaces;

生命周期回调

所谓生命周期交互就是指在容器创建Bean和销毁Bean之前做某些操作。在Spring中我们可以通过让Bean实现InitializingBean和DisposableBean接口,使用@PostConstruct和@PreDestroy以及通过如下的配置实现。

    <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>

如果我们给一个Bean同时配置了上面多种的初始化和销毁机制,那么他们的执行顺序如下:

初始化顺序

  1. Methods annotated with @PostConstruct (这个方法在构造函数之后执行,是最佳实践,可以和Spring API解耦)
  2. afterPropertiesSet() as defined by the InitializingBean callback interface(不建议使用,会耦合Spring API)
  3. A custom configured init() method(普通的xml配置文件中配置的初始化方法)

    析构顺序
  4. Methods annotated with @PreDestroy(建议使用)
  5. destroy() as defined by the DisposableBean callback interface (不建议使用)
  6. A custom configured destroy() method (普通的xml文件中配置的销毁方法)

Spring完整的初始化顺序

  1. 容器启动,实例化所有实现了BeanFactoyPostProcessor接口的类;这步会在任何普通Bean实例化之前加载;
  2. 实例化剩下的单例Bean,对这些Bean进行依赖注入;
  3. 如果Bean有实现BeanNameAware的接口那么对这些Bean进行调用;
  4. 如果Bean有实现BeanFactoryAware接口的那么对这些Bean进行调用;
  5. 如果Bean有实现ApplicationContextAware接口的那么对这些Bean进行调用;
  6. 如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessBeforeInitialization方法;
  7. 调用@PostConstruct注解的方法;
  8. 如果Bean有实现InitializingBean接口那么对这些Bean进行调用;
  9. 如果Bean配置有init属性,那么调用它属性中设置的方法;
  10. 如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessAfterInitialization方法;
  11. Bean正常的使用;
  12. 调用@PreDestroy标注的方法;
  13. 调用DisposableBean接口的destory方法;
  14. 调用Bean定义是指定的destroy-method方法;

LifeCycle和SmartLifeCycle接口

Spring提供了LifeCycle接口,实现了这个接口的Bean在Spring容器调用start()和stop()方法的时候能收到Spring的回调信息,分别调用这个Bean的start()和stop()方法。

Aware接口

Spring提供了很多aware接口让Bean来实现,提示IOC容器,这个Bean需要得到容器的某些组件或元素。

  • ApplicationContextAware
  • ApplicationEventPublisherAEwvaenrte
  • BeanClassLoaderAware
  • BeanFactoryAware
  • BeanNameAware
  • BootstrapContextAware:Typically available only in JCA aware ApplicationContext instances.
  • LoadTimeWeaverAware
  • MessageSourceAware
  • NotificationPublisherAwareSpring:Spring JMX notification publisher.
  • ResourceLoaderAware
  • ServletConfigAware
  • ServletContextAware

Spring系列.Bean简介的更多相关文章

  1. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  2. Spring 系列: Spring 框架简介(转载)

    Spring 系列: Spring 框架简介 http://www.ibm.com/developerworks/cn/java/wa-spring1/ Spring AOP 和 IOC 容器入门 在 ...

  3. Spring系列__02IOC模块简介

    Spring的两大核心功能就是IOC和AOP,这篇文章主要介绍IOC. 简单来说,在面向对象思想下,A类中有一个B类的属性, 那么我们在创建A类时往往需要同时创建一个B类的对象,以便A类对其进行调用. ...

  4. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  5. 深入理解Spring系列之六:bean初始化

    转载 https://mp.weixin.qq.com/s/SmtqoELzBEdZLo8wsSvUdQ <深入理解Spring系列之四:BeanDefinition装载前奏曲>中提到,对 ...

  6. Spring核心技术(一)——IoC容器和Bean简介

    IoC容器和Bean简介 这章包括了Spring框架对于IoC规则的实现.Ioc也同DI(依赖注入).而对象是通过构造函数,工厂方法,或者一些Set方法来定义对象之间的依赖的.容器在创建这些Bean对 ...

  7. [JavaEE] IBM - Spring 系列: Spring 框架简介

    Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级的.强壮的 J2EE 应用程序.develop ...

  8. Spring源码系列 — Bean生命周期

    前言 上篇文章中介绍了Spring容器的扩展点,这个是在Bean的创建过程之前执行的逻辑.承接扩展点之后,就是Spring容器的另一个核心:Bean的生命周期过程.这个生命周期过程大致经历了一下的几个 ...

  9. java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式

    Spring系列之 配置文件的操作 写在文章前面: 本文带大家掌握Spring配置文件的基础操作以及带领大家理清依赖注入的概念,本文涉及内容广泛,如果各位读者耐心看完,应该会对自身有一个提升 Spri ...

随机推荐

  1. 通过Android studio手动触发Android 上层GC(垃圾回收)的方法

    1.打开android Studio, 2.菜单栏中点击"View"--"Tools Window"--"Profiler",可以看到对应的 ...

  2. (Java实现) 友好城市

    1263:[例9.7]友好城市 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 1867 通过数: 1032 [题目描述] Palmia国有一条横贯东西的大河,河有笔直的南北两岸, ...

  3. JQuery实现对html结点的操作(创建,添加,删除)

    效果图: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  4. 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件

    写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...

  5. CMAKE工具学习

    最近在学习各大物联网平台的SDK包,发现其工程都使用了一种叫cmake的工具在管理代码.于是花了一天时间简单学习了解了cmake工具,其目的是让自己能读懂使用该工具管理的代码,并能简单使用该工具管理我 ...

  6. 温故知新-Mysql索引结构&页&聚集索引&非聚集索

    文章目录 摘要 索引 索引概述 索引优势劣势 索引结构 BTREE 结构 B+TREE 结构 页 索引分类 索引语法 索引设计原则 聚触索引 & 非聚触索引 你的鼓励也是我创作的动力 Post ...

  7. 深入理解PHP原理之Opcodes

    Opcode是一种PHP脚本编译后的中间语言,就像Java的ByteCode,或者.NET的MSL. 举个例子,比如你写下了如下的PHP代码: <?php echo "Hello Wo ...

  8. Py中去除列表中小于某个数的值

    ### Py去除列表中小于某个数的值 print('*'*10,'Py去除列表中小于某个数的值','*'*10) nums = [2,3,4,10,9,11,19,14] print('*'*10,' ...

  9. TensorFlow从0到1之TensorFlow优化器(13)

    高中数学学过,函数在一阶导数为零的地方达到其最大值和最小值.梯度下降算法基于相同的原理,即调整系数(权重和偏置)使损失函数的梯度下降. 在回归中,使用梯度下降来优化损失函数并获得系数.本节将介绍如何使 ...

  10. 重装conda

    重装Conda是因为想重装tensorflow,结果竟然报了各种各样稀奇古怪的错误:我贴一下自己见过的报错 The environment is inconsistent, please check ...